Chain Actions Together to Create a Sequence

Using a single action in SpriteKit is powerful, but even more powerful are actions that you chain together. When you chain actions together, you can create a complex system of actions that you can run either as a group or a sequence. You can also set up your actions to repeat forever or for a set number of times, as you saw earlier in Animate the Player with Actions.

To make the collectible drops fall from the sky, you’ll use several actions chained together, creating a single sequence of actions. You’ll build this sequence of actions in a new drop(dropSpeed:floorLevel) method.

Open the Collectible.swift file. Below the initialization methods, add the following code:

 // MARK: - FUNCTIONS
 func​ ​drop​(dropSpeed: ​TimeInterval​, floorLevel: ​CGFloat​) {
 let​ pos = ​CGPoint​(x: position.x, y: floorLevel)
 
 let​ scaleX = ​SKAction​.​scaleX​(to: 1.0, duration: 1.0)
 let​ scaleY = ​SKAction​.​scaleY​(to: 1.3, duration: 1.0)
 let​ scale = ​SKAction​.​group​([scaleX, scaleY])
 
 let​ appear = ​SKAction​.​fadeAlpha​(to: 1.0, duration: 0.25)
 let​ moveAction = ​SKAction​.​move​(to: pos, duration: dropSpeed)
 let​ actionSequence = ​SKAction​.​sequence​([appear, scale, moveAction])
 
 // Shrink first, then run fall action
 self​.​scale​(to: ​CGSize​(width: 0.25, height: 1.0))
 self​.​run​(actionSequence, withKey: ​"drop"​)
 }

Here, you’re setting the end position of the drop, in other words, where you want the drop to land. You’re then creating a series of actions to present that drop to the player, making it appear to fall to the floor. Here’s how it works:

  • First, you set the y-position using the input value stored in floorLevel.

  • Then, you add two scale actions to make the drop stretch a little; this gives it a drip-like appearance.

  • Next, you create actions to fade-in and move the drop down the scene.

  • Finally, for some more visual appeal, you tweak the scale of the drop and then run this sequence of actions. You give it a named key so that you can access this action later using its name.

Notice that the first set of actions are grouped, meaning they run at the same time, whereas the second set of actions are individually run as part of the sequence. Also, notice how it’s possible to run grouped actions within a sequence of actions. Ah, yes, the power of actions—this is where SpritKit really shines.

You’re almost done: open the GameScene.swift file and at the end of the spawnGloop() method, add this line of code:

 collectible.​drop​(dropSpeed: ​TimeInterval​(1.0),
  floorLevel: player.frame.minY)

With this code, the collectible node is executing the drop(dropSpeed:floorLevel) method on itself.

For clarity, the updated spawnGloop() method looks like this:

 func​ ​spawnGloop​() {
 let​ collectible = ​Collectible​(collectibleType: ​CollectibleType​.gloop)
  collectible.position = ​CGPoint​(x: player.position.x,
  y: player.position.y * 2.5)
 addChild​(collectible)
 
  collectible.​drop​(dropSpeed: ​TimeInterval​(1.0),
  floorLevel: player.frame.minY)
 }

Build and run the project. Notice how a single drop falls straight down and then stops at the platform. As the drop is making its journey to the platform, notice how it’s slightly elongated—thanks to the SKAction.scaleY(to: 1.3, duration: 1.0) action in the drop(dropSpeed:floorLevel) method as shown in the image.

images/ChainingActionsAndUsingIterativeDesign/spritekit-build-02.png

At this point, your second iteration is complete: a single collectible falls from the sky and lands at the top of the platform. Your next task is to add more collectibles.

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

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