I have a simple subclass of SKCameraNode
that I called InteractiveCameraNode
. For now it's very simple: I need things to happen when my camera's position changes. Here is what I did:
class InteractiveCameraNode: SKCameraNode {
// MARK: - Properties
var enableInteraction = true
var positionResponders = [(CGPoint, CGPoint) -> Void]()
/// Calls every closure in the `positionResponders` array
override var position: CGPoint {
didSet {
if enableInteraction {
for responder in positionResponders {
responder(oldValue, position)
}
}
}
}
}
Since I might have multiple things happening when the camera moves, I have an array of closures that are called when the camera's position is changed. So far everything works perfectly except the didSet
observer does not get called if I move the camera using an action. If I use a constraint on the camera to make it track a node and then move that node with an action, it works. If I move the camera by hand, it works. Why won't it work with actions?
my previous answer was incorrect 100%.. Whatever lead me to that answer was some other mistake on my part and I apologize.. I learned something useful today as well :)
It looks like that behavior you're seeing is either A) a bug or B) some deeper mechanism of Swift / SK that I don't understand.
Here is a workaround that worked for me in playground:
to use:
output, showing that the actual position has been updated from an action and
didSet
:But this will probably be too slow for what you want, so you will be better off manually setting
position
ormyPosition
inupdate()
every frame (whichever one will work fordidSet
lol..)(Credit to Confused for this paragraph).
Also, I wonder if a KVO would work here.. I haven't used them, but CameraNode is already an NSObject... something to look into maybe since this is likely a bug of some sort...