I'm trying to use an SKAction to move my sprites. However, I noticed that after moving the sprites, their position values still remained the same. With some research, I learned that (correct me if I'm wrong) the sprites' position values are actually their location relative to the parent view, and that it was the parent view's position value that had actually changed, and not the sprites'. Only when I checked, the parent view's position value also hadn't changed.
So which position value actually changes?? And how would I go about accounting for the change in position when accessing the sprites' position values? (Since a lot of my game's logic is dependent on their position values)
Here's my code:
let fall = SKAction.moveBy(x: 0, y: -85, duration: 0.2)
print("Original position \(gift.position)")
gift.run(fall)
print("New position \(gift.position)") // Both print the same value
Any help would be greatly appreciated!
Your gift.run(fall)
is scheduling an action that will happen over the course of 0.2 seconds, not waiting for it to finish. At 60 frames/second, that means the sprite will reach its final position only after 12 frames. If you print the gift's position at each step in update(_:)
, you'll see that it is indeed changing.
Since the run
does not wait for the action to complete, the sprite hasn't had a chance to move at all when the second print
is executed.
If you want something to happen only after an action completes, you have various possibilities. A common way is to add a completion block; see Apple's documentation on getting started with actions: https://developer.apple.com/documentation/spritekit/getting_started_with_actions. Or if you want the same node to do multiple things in order, you can make a composite SKAction
, like with the sequence
or repeat
initializers.