3
votes

The run function for SKNode lets you run a block when the action completes, but what if the action is cancelled/removed via removeAllActions?

Cancelling an action doesn't invoke the completion block from the run function.

Is there a callback or way to run code when the action is cancelled/removed?

2
Not that I can think of without a custom .run and .remove functionFluidity
Why would you want to call code when an action is removed? By design choice, You are probably going to want the object that actually does the removal to call the removal action, otherwise you may end up in a situation where you need to remove an action without running code, but are forced to.Knight0fDragon
@Knight0fDragon to reset nodes whose properties have been modified by actions. It would be cleaner to have the completion block get called when the action is cancelled as well as completed (similar to animateWithDuration in UIView)Crashalot
@Crashalot, again this should be the method that causes the removal, kind of like what Simone's answer is like, only slightly modified. It is a complex problem to think about, that is why I do not want to just post an answer.Knight0fDragon

2 Answers

2
votes

Yes, if you remove an action before it has completed, the completion block will not run. Per Docs:

The run(:completion:) method is identical to the run(:) method, but after the action completes, your block is called. This callback is only called if the action runs to completion. If the action is removed before it completes, the completion handler is never called.

1
votes

A work around could be:

class YourSpriteNode: SKNode {

     func doSometingAtCompletionAction() {
          //all your stuff
     }

     override removeAllActions() {
         super.removeAllActions()
         doSometingAtCompletionAction()
     }
}