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 want to have my camera pinned to central node, e.g. the world should move and the camera should stay stationary.
How do I achieve that?
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
}
Using SKCameraNode
with SKConstraint
in Swift 5.1
let camera = SKCameraNode()
camera.constraints = [.distance(.init(upperLimit: 10), to: node)]
scene.addChild(camera)
scene.camera = camera
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With