Reputation: 4100
I'm making a SpriteKit game and I'm trying to get an SKCameraNode to follow the player node. However, when I access the position property of the player node after using an SKAction to move it, the position after running the action is the same as the position before running it. Here is my scene code:
func updateMap(playerX: Int, playerY: Int) {
player.coordinates = CGPoint(x: highlightedTile.tileX, y: highlightedTile.tileY)
camera?.position = player.position
}
and the code from my player
class:
var coordinates: CGPoint {
didSet{
let moveAction = SKAction.moveTo(CGPoint(x: (coordinates.x) * 64 - 64, y: (coordinates.y) * 56 - 47), duration: NSTimeInterval(2.0))
self.runAction(moveAction)
}
}
SpriteKit does not update the position
property of the player node until the action is complete. Therefore I want to wait until the animation has completed before updating the camera position. How can this be done?
Upvotes: 0
Views: 1945
Reputation: 13675
You are querying the position
property before the moving is completed...
Try this:
print(playerNode.position)
let moveAction = SKAction.moveTo(CGPoint(x: (coordinates.x) * 64 - 64, y: (coordinates.y) * 56 - 47), duration: NSTimeInterval(2.0))
let waitAction = SKAction.waitForDuration(NSTimeInterval(3))
self.runAction(moveAction, completion: {
print(playerNode.position)
})
Also note that I use playerNode
variable, which should represent the player. Not sure what self
is in your case though ( the player or the scene).
Upvotes: 1