【发布时间】:2015-09-18 11:25:17
【问题描述】:
我做了一个平台游戏,把相机对准主节点Player。
我已经浏览了可以在此处找到的 Apple 文档:Apple
大约有 6 个平台,它们之间有 self.frame.size.height / 4.5 空间,每次它们在屏幕顶部的 dy = -15 下时都应该重新生成,以使其无限循环。
我有一个播放器节点,每次触摸屏幕时都会产生一个冲动(播放器上下跳跃)。整个游戏确实只在y-Axis上移动。
我创建的相机聚焦在玩家身上,每当玩家跳跃平台时,平台就会向下移动,而玩家则会向上移动。然而,这只是视图,而不是现实世界的位置。这意味着,SpawnPlatforms 中的给定位置始终相同,因为它们不会移动,只有粘着 Player 的 View 会上下移动。
我想在我的播放器节点上安装相机,它在 y 轴上移动,当它进一步向上移动时,平台也应该向下移动,此时只有视图发生变化,我无法使用更新:重生平台的方法。我该如何改变呢?
编辑:我的代码在 Update:Method 中使用更新,但这仍然没有回答我最初的问题,如何使用相机移动整个世界,而不仅仅是相机。
override func didMoveToView(view: SKView) {
/* Setup your scene here */
self.physicsWorld.contactDelegate = self
self.anchorPoint = CGPoint(x: 0, y: 0.30)
backgroundColor = SKColor(red: 81.0/255.0, green: 192.0/255.0, blue: 201.0/255.0, alpha: 1.0)
self.World = SKNode()
self.World.name = "World"
addChild(World)
self.Camera = SKNode()
self.Camera.name = "Camera"
self.World.addChild(Camera)
SpawnPlatforms()
SpawnPlayer()
}
func SpawnPlatforms() {
Platform0 = SKSpriteNode (color: SKColor.greenColor(), size: CGSize(width: self.frame.size.width , height: 25))
Platform0.position = CGPoint(x: self.frame.size.width / 2, y: -36)
Platform0.zPosition = 1
Platform0.physicsBody = SKPhysicsBody(rectangleOfSize:Platform0.size)
Platform0.physicsBody?.dynamic = false
Platform0.physicsBody?.allowsRotation = false
Platform0.physicsBody?.restitution = 0
Platform0.physicsBody?.usesPreciseCollisionDetection = true
Platform0.physicsBody?.categoryBitMask = Platform0Category
Platform0.physicsBody?.collisionBitMask = PlayerCategory
Platform0.physicsBody?.contactTestBitMask = PlayerCategory
World.addChild(Platform0)
....//Same code for the other Platforms1-5
}
func SpawnPlayer(){
Player = SKSpriteNode (imageNamed: "Image.png")
Player.size = CGSize(width: 64, height: 64)
Player.position = CGPoint(x: self.frame.size.width / 2, y: 0)
Player.zPosition = 2
Player.physicsBody = SKPhysicsBody(rectangleOfSize:CGSize(width: 35, height: 50))
Player.physicsBody?.dynamic = true
Player.physicsBody?.allowsRotation = false
Player.physicsBody?.restitution = 0
Player.physicsBody?.usesPreciseCollisionDetection = true
Player.physicsBody?.categoryBitMask = PlayerCategory
Player.physicsBody?.collisionBitMask = Platform0Category
Player.physicsBody?.contactTestBitMask = Platform0Category | Platform1Category | Platform2Category | Platform3Category | Platform4Category | Platform5Category
World.addChild(Player)
}
override func didSimulatePhysics() {
Camera.position = CGPoint(x: Player.position.x, y: Player.position.y)
self.centerOnNode(Camera!)
}
func centerOnNode(node: SKNode) {
let cameraPositionInScene: CGPoint = Camera.scene!.convertPoint(Camera.position, fromNode: World)
World.runAction(SKAction.moveTo(CGPoint(x:World.position.x , y:World.position.y - cameraPositionInScene.y), duration: 1.0))
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
if(Player.position.y > (Platform3.position.y + 30) && Player.position.y < Platform4.position.y){
Platform1.position = CGPointMake(self.frame.size.width / 2, Platform6.position.y + self.frame.size.height / 4.5)
Platform1.color = SKColor.redColor()
}
...//Same code for the other Platforms1-5
}
【问题讨论】:
-
你的问题有点不清楚。请详细说明。
-
我编辑了我的原始帖子,希望现在更清楚。
-
你试过阅读这篇文章吗? stackoverflow.com/questions/20828655/…
-
您没有正确使用“相机”。 SO和Google上有很多很好的答案,展示了如何实现“相机”。 Xcode 7 实际上引入了一个相机节点类以使其更容易。
-
我实际上是从 Apple Document 中得到这个“相机”的。你有新的 XCode 7 相机节点的链接吗,因为我找不到任何东西?
标签: ios swift camera sprite-kit