How to make camera follow SKNode in Sprite Kit?

后端 未结 6 2070
庸人自扰
庸人自扰 2020-12-08 23:21

I want to have background stationary with different objects (sprites and other objects).

Player will be represented by center node that will move around the world. I

6条回答
  •  囚心锁ツ
    2020-12-08 23:33

    The new SKCameraNode seems like a very easy way to do this.

    https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKCameraNode/

    Simply

    1) create an SKCameraNode

    // properties of scene class
    let cam = SKCameraNode()
    let player = SKSpriteNode()
    

    2) add it to your scene camera property

    // In your scene, for instance didMoveToView
    self.camera = cam
    

    3) make the camera follow your player position

    override func update(currentTime: CFTimeInterval)
    {
        /* Called before each frame is rendered */
        cam.position = player.position
    }
    

提交回复
热议问题