1

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.

Aaron
  • 757
  • 5
  • 18
  • Make sure the ui update method is running in the main thread? – Mark Jul 11 '15 at 00:17
  • Where are you updating `wayPoints`? If it's in the `update` method, then you should move the code in camera updating code (currently in `update`) to `didFinishUpdate`. Also, make sure that `wayPoints.last` is the point you want the camera to follow. – 0x141E Jul 11 '15 at 03:40
  • @Mark I'm rather sure that SKScene: update runs in the main thread, so I believe it is in the main thread. – Aaron Jul 11 '15 at 05:18
  • @0x141E My bad, I should have mentioned this in the original question. I'm actually updating wayPoints in a separate function that is ran/updated every 0.05 seconds. I would assume this would be the problem (Since update is only called once per frame), however even if I update the cameras location at the end of the function that updates the way points, the camera is still "slow" – Aaron Jul 11 '15 at 05:23
  • @0x141E Yes, wayPoints.last will always be the Y point I want to center on in this case. – Aaron Jul 11 '15 at 05:59
  • 1
    Looking at the video I don't think it is an issue of not updating fast enough. It looks like your math is wrong on the y position of either the Camera or the parent you are moving. My best guess is that you shouldn't be calling `self.centerOnNode(self.camera!)` at all. – Skyler Lauren Jul 12 '15 at 10:08
  • @SkylerLauren I think you are right, as I believe it can update fast enough. I'm starting to think my math is wrong as well, but not sure where. Only thing I can think of is the fact that the node I'm centering on is scaled in by 10. – Aaron Jul 14 '15 at 19:32
  • @Aaron it does appear to be moving at a 10th of the speed of should. – Skyler Lauren Jul 14 '15 at 22:31

1 Answers1

0

I'm not sure what the "Proper" way to do this is. but I set the camera nodes velocity equal to the centered node's velocity AND I set the camera's position equal to the node's position. For Example:

camera?.position = player!.position
camera?.physicsBody?.velocity = player!.physicsBody!.velocity

In order to do this, you will need to give the camera node a physics body. When you do, I recommend making sure it's on it it's own collision, field, and contact masks.

GTG101
  • 149
  • 13