Search code examples
sprite-kitactionremoveall

How to remove all actions except forkey in SpriteKit?


I have a player with a few parts (arms, hands, weapon) and whenever the player changes move mode (running, standing, aiming), all actions on these parts are removed and new ones are run.

Now, when the player is firing, i run a "shooting" action on the weapon node, and when the player stops firing, i run weapon.removeAction(forKey: "shooting")

The problem is: this "shooting" action obviously gets removed when the player changes move mode (as the weapon's actions are all removed).

Is there any way to remove all actions EXCEPT (forKey: "shooting") ?

I tried giving all other actions the key "moving", but quickly realized that multiple actions cannot have the same key (it overrides the previous action with that key and that previous action is never run). (any fix for this btw? just curious)

the obvious solution is to run the "shooting" action on a separate node from weapon, but that would be less elegant.


Solution

  • Personally, I would add a Gun Node to your sprite, and put the shooting action on that node so that you could remove all actions

    There is no way to enumerate over all your actions, you can remove by key, or remove all, that is it.

    If you extract your action, remove all, then add it back it, it will restart your action, which you also do not want.