我制作了一个平台游戏,并将摄像机对准了主节点玩家。
我已经阅读了苹果文档,可以在这里找到:Apple
有大约6个平台,它们之间的间距为self.frame.size.height / 4.5
,每次当它们再次位于屏幕顶部的dy = -15
下方时,它们都应该重新生成,以形成无限循环。
我得到了玩家节点,每次屏幕被触摸时(玩家上下跳动),它都会获得一个冲量。整个游戏只在y轴
上移动。
我创建的摄像机专注于玩家,每次玩家跳跃时,平台向下移动,玩家向上移动。但是,这只是视图,而不是真实世界的位置。这意味着SpawnPlatforms
中给定的位置始终相同,因为它们不会移动,只有粘附在玩家身上的视图上下移动。
我希望将摄像机放在我的玩家节点上,它在y轴
上移动,而它向上移动时,平台也应该向下移动,目前只有视图改变,我无法使用Update:Method
来重新生成平台。我该如何改变这一点?
编辑:我在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
}