I currently am attempting to center a camera on a SKNode in SpriteKit. In the update function I have the following:
override func update(currentTime: NSTimeInterval) {
if self.camera != nil {
self.camera!.position = CGPointMake(CGRectGetMidX(super.frame) / 10, wayPoints.last!.y)
self.centerOnNode(self.camera!)
}
}
This is my centerOnNode function:
func centerOnNode(node: SKNode) {
let cameraPositionInScene: CGPoint = node.scene!.convertPoint(node.position, fromNode: node.parent!)
node.parent!.position = CGPoint(x:node.parent!.position.x - cameraPositionInScene.x, y:node.parent!.position.y - cameraPositionInScene.y)
}
This is how Apple suggests centering a camera on a Node (Note that these are all in a World SKNode). This sort of works; it centers the camera, however it doesn't update fast enough. This produces the following effect (Note that 'CGRectGetMidX(super.frame) / 10' will always be the center of the screen in my case, and 'wayPoints.last!.y' will always be the Y position of the apex of the line): http://gyazo.com/95f02f98e5707b0065e74f90ac547139
As you can see the camera is moving, but the line is moving faster. I'm not sure why this is occurring, because shouldn't the camera move at the same speed as the line?
Edit: I'm adding/updating the wayPoints array in a separate function which is called every 0.5 seconds using this:
NSTimer.scheduledTimerWithTimeInterval(0.05, target: self, selector: "addPoint", userInfo: nil, repeats: true)
However even if I update the camera position in the addPoint function, the same effect still occurs.