In the documentation for SpriteKit, I found this:
Generally, actions do not call public methods on nodes. For example, if you want to subclass SKNode to respond to a move(to:duration:) action, you might consider overriding its position property to add a didSet observer.
class MovingNode: SKSpriteNode {
override var position: CGPoint {
didSet {
// code to react to position change
}
}
}
However, because a move action that's running on an instance of MovingNode (defined in the code listing above) doesn't set its position, the observer isn't invoked and thus, your code is never executed.
I find this to be very strange because if actions don't use someNode.position = newPosition, then how does the position of the node change? Isn't it impossible to change the value of a property without calling its setter?
However, if you print the value of someNode.position every frame, the new position is reflected. So, evidently, something is calling its setter. If it's not the SKAction then what is? And whatever is, how is it bypassing the property observer?
Does anyone know what behind the scenes work SKAction does to change the value of node properties without calling the setter?
You can achieve this behavior doing something like this:
Then if you do this
You can see the position changed, but you skip the
didSetobserverRemember
runruns directly inside theSKNodewith access to private values.