我做了一个平台游戏,并将相机指向主节点播放器.
我已经通过了可以在这里找到的苹果文档:Apple
大约有6个平台,它们之间有self.frame.size.height / 4.5空格,每次在屏幕顶部的dy = -15时都应该重新生成一个平台,使它成为一个无限循环.
我得到了一个Player节点,每当屏幕被触摸时,它都会得到一个冲动(玩家跳起来).整个游戏只能在y轴上移动.
我创建的摄像机专注于播放器,每当播放器跳转平台向下移动并且播放器向上移动时.但是,这只是视野,而不是现实世界的立场.这意味着,SpawnPlatforms中给定的位置总是相同的,因为它们不移动,只有坚持使用Player的View上下移动.
我想让我的播放器节点上的相机,它在y轴上移动,而在它进一步向上,平台也应该向下移动,只有视图改变,我不能使用更新:重建平台的方法.我该怎么改呢?
编辑:我的代码使用更新:方法中的更新,但这仍然不能回答我的初始问题,如何移动整个世界与相机,不仅相机.
- 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
- }
解决方法
创建一个SKNode,让我们称之为map.添加地图作为这个场景的孩子.您添加每个其他游戏节点作为此地图的一个孩子(平台,播放器等).
然后,移动node.parent,而不是移动地图.这将导致平台始终保持相同的位置,即使您移动相机.
例如,下一个代码将以线性速度移动update:method中的map:
kScrollScenarioFactor可能就像200.f
- -(void)update:(CFTimeInterval)currentTime {
- //Calculating time for realistic scroll speed
- float scrollTime = currentTime - self.lastTime;
- self.lastTime = currentTime;
- self.map.position = CGPointMake(map.position.x - (scrollTime * self.kScrollScenarioFactor),map.position.y);
- }
或者如果要根据播放器在update:方法中移动地图,可以查看此方法:
- -(void)update:(CFTimeInterval)currentTime {
- //Calculating playerPosition difference
- float diffX = self.lastPlayerPosition.x - self.player.position.x;
- float diffY = self.lastPlayerPosition.y - self.player.position.y;
- self.lastPlayerPosition = self.player.position;
- //Updating map position
- self.map.position = CGPointMake(map.position.x + diffX,map.position.y + diffY);
- }