Animate the Player with Actions

One of the more powerful features in SpriteKit is actions. Actions in SpriteKit are handled using the SKAction class, and they allow you to perform many types of operations on your nodes. For example, you can:

  • Change a node’s position and orientation.
  • Change a node’s size or scale properties.
  • Change a node’s visibility or make it translucent.
  • Change a sprite node’s contents so it animates through a series of textures.
  • Colorize a sprite node.
  • Play simple sounds.
  • Remove a node from the node tree.
  • Run a code block.
  • Invoke a selector on an object.

You’ll use most—if not all—of these types of actions in your games. However, to animate the sprite and move the player, you’ll use only a handful of these actions to change the node’s position, scale, and texture.

Open the SpriteKitHelper.swift file and add another method to your SKSpriteNode extension:

 // Start the animation using a name and a count (0 = repeat forever)
 func​ ​startAnimation​(textures: [​SKTexture​], speed: ​Double​, name: ​String​,
  count: ​Int​, resize: ​Bool​, restore: ​Bool​) {
 
 // Run animation only if animation key doesn't already exist
 if​ (​action​(forKey: name) == ​nil​) {
 let​ animation = ​SKAction​.​animate​(with: textures, timePerFrame: speed,
  resize: resize, restore: restore)
 
 if​ count == 0 {
 // Run animation until stopped
 let​ repeatAction = ​SKAction​.​repeatForever​(animation)
 run​(repeatAction, withKey: name)
  } ​else​ ​if​ count == 1 {
 run​(animation, withKey: name)
  } ​else​ {
 let​ repeatAction = ​SKAction​.​repeat​(animation, count: count)
 run​(repeatAction, withKey: name)
  }
  }
 }

With this code, you’re creating an extension method to start an animation. The function parameters include an array of textures, the speed at which to cycle through the textures, a key name for the animation (so you can identify it later), a count, and two parameters that control how to handle the images.

Looking closer at this extension method, you’ll notice that three SKAction methods are used:

  • SKAction.animate(with:timePerFrame:resize:restore:)
  • SKAction.repeatForever(_:)
  • SKAction.repeat(_:count:)

The first action sets up the frame-by-frame animation using the supplied textures. The code then cycles through these textures based on the value of timePerFrame. The timePerFrame is the amount of time, in seconds, each texture is displayed. The other parameters, resize and restore, indicate how the action should handle the size and final texture for the node. When resize = true, the sprite’s size matches the image size. When restore = true, the original texture is restored when the action completes.

The other two actions, repeatForever(_:) and repeat(_:count:), determine how many times to repeat the action. You can either repeat forever or repeat the number of times specified in the count parameter.

With this extension method, you’re using the count parameter to determine how often to repeat the action: when a value of 0 is passed into the method, the action repeats forever, whereas if any other number is passed in, the action repeats the specified number of times.

Once you have the action set up, you use the run(_:withKey:) method on the node to execute that action. Excellent, you’re ready to animate Blob using your new animation extension.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset