4

During the development of some interactions between different nodes, I realized I need to remove a specific list of actions from a node. The current version of Sprite-Kit framework provides some instance methods as:

Obviously every action that running in my node has a String key for its identifications. So I thought of something that was very similar to removeAllAction, then I made an SKNode extension:

public extension SKNode {
    func removeAllAction(in list:[String]) {
       list.forEach { if self.action(forKey: $0) != nil { self.action(forKey: $0)?.speed = 0.0; self.removeAction(forKey: $0)}}
    }
}

And in my project I can use it as:

let actionList = ["idle_walk_sx","idle_walk_dx","walk_dx","walk_sx","walk_idle_sx","walk_idle_dx","rotate_sx_dx","rotate_dx_sx"]
self.removeAllAction(in: actionList)

The code works well. But I'm not really sure about two factors:

  • the effectiveness of the speed corrections (to zero), I thought it appropriate to introduce it in the face of repeated actions. Should I remove it due to avoid strangness or leave it ?
  • How is it possible to extend this extension (expanding it) to any childrens that have the same list to remove?
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133

1 Answers1

1

I agree with the comments. Probably speed is not necessary so the code + the scanning to the children nodes could be something like:

public extension SKNode {
    func removeAllAction(in list:[String]) {
        list.forEach { if self.action(forKey: $0) != nil { self.removeAction(forKey: $0)}}
        self.children
            .filter { $0.hasActions() }
            .forEach { $0.removeAllAction(in: list) }
    }
}
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133