In continuing with my recent posts about , I thought I’d go through another useful aspect of the : SpriteKit actions. A SpriteKit action, represented by the class, allows you to animate certain properties on an object, such as position and velocity. SpriteKit framework SKAction SKNode Since there are numerous types of SpriteKit actions ( ), let’s take a more practical route to using actions on nodes, rather than going through different types of actions. see the [SKAction](https://developer.apple.com/reference/spritekit/skaction) reference Creating an action Unlike a lot of different SpriteKit objects, you don’t create an using an initializer. Instead, you call one of the class methods corresponding to the action you want to perform. SKAction In this example, the method creates an action that moves a node 10 pixels to the right and 15 pixels down over the course of 800 milliseconds. Other action methods include , , , and . moveBy rotate scale fadeOut applyForce Running an action To run this action on a node, call the method on the node you want the action to apply to. run(SKAction) Based on the previous example, this will update the player’s position over 800 ms by adding the values passed to the call. A variant of the method takes a completion callback that is called after the action has been completed. moveBy run One useful thing about is they are “copy-on-write”, which means that whenever the action is changed, the value is copied, therefore the same can be used on multiple nodes. SKActions SKAction Groups and Sequences Sometimes, multiple actions need to be run at the same time or in sequence. To accomplish this, the methods and create groups and sequences, respectively, of actions. For example, we need to both move and rotate our player node at the same time. SKAction.group([SKAction]) SKAction.sequence([SKAction]) In the above example, we could change the call to , which would run the first, wait until it completes, and then run the . group sequence moveAction rotateAction Repeat Running an action multiple times is just as easy: call passing in the action to be repeated and the number of times it should be run. This is used just like any other action. SKAction.repeat(SKAction, count: Int) Now our player node moves right and down five times. To repeat an action forever, you can call with the action which is to be repeated infinitely. SKAction.repeatForever(SKAction) Timing Almost every type of action has a duration parameter that is a (a typealias for ) in seconds. This tells the engine to run the action for a certain number of seconds (or fraction of a second). There is also another type of action, , which will wait for the duration of the action. This is useful in action sequences to have time in between actions. TimeInterval Double wait(forDuration: TimeInterval) Blocks and Custom Actions There is also another class method that executes a block of code. The parameter to is a function that does not take any arguments and returns . This allows you to do things that are out of the scope of the regular , such as update UI or change scores, for example. run(_: () -> Void) run Void SKAction A custom action is a little bit trickier. The method creates an action that runs over a specified duration, while continually calling the with the node ( ) the action is running on and the elapsed time ( ) until the duration time. customAction(withDuration: TimeInterval, actionBlock: @escaping (SKNode, CGFloat) -> Void) actionBlock SKNode CGFloat This code snippet creates an action that changes a label node’s text counting up from to . 0s 5s Stopping Actions There are two basic ways to stop actions while they are running: calling or on a node. The former will stop all actions currently running on the node, while the latter will remove an action based on a unique key given to the action when the is called. removeAllActions() removeAction(forKey: String) run(SKAction, withKey: String) In my recent SpriteKit game, , I used heavily to make tiles move around. Check it out in the App Store! Also, check out my other SpriteKit posts, the and . If you have any questions, comment here or tweet or PM me on Twitter. Happy SpriteKit-ing! Blueshift SKActions basics physics _Read reviews, compare customer ratings, see screenshots, and learn more about Blueshift Tiles. Download Blueshift Tiles…_itunes.apple.com Blueshift Tiles on the App Store