简体   繁体   中英

SKAction Waiting for Completion

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?

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM