相机不会跟随精灵

时间:2018-03-15 20:37:10

标签: swift camera sprite-kit skcameranode

我正在尝试在swift SpriteKit(更具体地说是RPG)上制作游戏。我有一个问题,让相机跟随我的角色。到目前为止,所有发生的事情都是相机出现在屏幕外,你甚至看不到角色。谢谢!

import SpriteKit

class GameScene: SKScene {
     let cam = SKCameraNode()
     let james = SKSpriteNode()
     override func didMove(to view: SKView) {
          self.anchorPoint = .zero
          var background = SKSpriteNode(imageNamed: "image-3")
          background.size = CGSize(width: 1500, height: 1000);
          background.position = CGPoint(x: frame.size.width / 2, y: frame.size.height / 2)
          addChild(background)
          view.ignoresSiblingOrder = false

          self.camera = cam

          let james = Player()
          james.size = CGSize(width: 40, height: 40)
          james.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
          james.zPosition = 1

          self.addChild(james)

          }
    override func didSimulatePhysics() {
         self.camera!.position = james.position
    }
}

2 个答案:

答案 0 :(得分:1)

您需要将相机添加到场景中。我建议让相机成为詹姆斯的孩子,以便它始终跟随詹姆斯

import SpriteKit

class GameScene: SKScene {
     let cam = SKCameraNode()
     let james = SKSpriteNode()
     override func didMove(to view: SKView) {
          self.anchorPoint = .zero
          var background = SKSpriteNode(imageNamed: "image-3")
          background.size = CGSize(width: 1500, height: 1000);
          background.position = CGPoint(x: frame.size.width / 2, y: frame.size.height / 2)
          addChild(background)
          view.ignoresSiblingOrder = false

          self.camera = cam

          let james = Player()
          james.size = CGSize(width: 40, height: 40)
          james.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
          james.zPosition = 1
          James.addChild(cam)
          self.addChild(james)

     }

}

答案 1 :(得分:0)

我没有看到您粘贴的代码中发生任何物理现象。很可能didSimulatePhysics没有被调用。尝试将self.camera!.position = james.position移动到更新功能。

  

修改

最近很多麻烦似乎源于场景默认暂停的事实。尝试设置......

self.isPaused = false

里面

override func didMove(to view: SKView)

并在

上加分
self.camera!.position = james.position

确保它被调用