swift - 相机不会跟随 Sprite

标签 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
    }
}

最佳答案

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

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)

     }

}

关于swift - 相机不会跟随 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49308756/

相关文章:

android - 使用 Android Camera Intent 从同一个按钮拍摄视频或静态图片

math - 四元数的相机旋转

ios - 如何从 SKSpriteNode 获取颜色?

ios - 如何在 Sprite 套件中实现齿轮接头?

ios - QLPreviewController 在共享 iOS 9 时崩溃

ios - 使用swift 3点击单元格时如何在Avplayer中播放来自url的视频

android - 使用相机拍照时如何找到 getThumbnail 的 origID.TakePicture

ios - 为什么在我的 SpriteKit 游戏中,移动逻辑不同?

swift - 在由 Swift 枚举确定的函数中使用一元运算符

ios - 在不丢失原始图像大小的情况下,在 Swift 中合并 2 个图像