0

我正在尝试在 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
    }
}
4

2 回答 2

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)

     }

}
于 2018-03-16T02:04:40.430 回答
0

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

编辑

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

self.isPaused = false

里面

override func didMove(to view: SKView)

并设置一个断点

self.camera!.position = james.position

以确保它被调用

于 2018-03-15T21:07:08.783 回答