Use Actions to Run Code Blocks

The spawnGloop() method you added in the GameScene.swift file drops a single drop of gloop. Although that’s a great start, what you really need now is a way to drop multiple drops at specific time intervals. For that, you’ll rely on another powerful feature of SpriteKit actions: running a block of code after the action completes.

Open the GameScene.swift file. Above the spawnGloop() method, add the following new method, which you’ll use to spawn multiple drops:

 func​ ​spawnMultipleGloops​() {
 // Set up repeating action
 let​ wait = ​SKAction​.​wait​(forDuration: ​TimeInterval​(1.0))
 let​ spawn = ​SKAction​.run { [​unowned​ ​self​] ​in​ ​self​.​spawnGloop​() }
 let​ sequence = ​SKAction​.​sequence​([wait, spawn])
 let​ repeatAction = ​SKAction​.​repeat​(sequence, count: 10)
 
 // Run action
 run​(repeatAction, withKey: ​"gloop"​)
 }

This new method uses a completion block to call spawnGloop(). It also wraps it up in a nifty sequence of actions that are set to repeat 10 times.

Now that you have a method that repeatedly spawns collectible drops, you need to call it when the game scene launches.

In the didMove(to:) method, replace this line of code:

 spawnGloop​()

with this line of code:

 spawnMultipleGloops​()

The updated didMove(to:) method now looks like this:

 override​ ​func​ ​didMove​(to view: ​SKView​) {
 
 // Set up background
 let​ background = ​SKSpriteNode​(imageNamed: ​"background_1"​)
  background.anchorPoint = ​CGPoint​(x: 0, y: 0)
  background.zPosition = ​Layer​.background.rawValue
  background.position = ​CGPoint​(x: 0, y: 0)
 addChild​(background)
 
 // Set up foreground
 let​ foreground = ​SKSpriteNode​(imageNamed: ​"foreground_1"​)
  foreground.anchorPoint = ​CGPoint​(x: 0, y: 0)
  foreground.zPosition = ​Layer​.foreground.rawValue
  foreground.position = ​CGPoint​(x: 0, y: 0)
 addChild​(foreground)
 
 // Set up player
  player.position = ​CGPoint​(x: size.width/2, y: foreground.frame.maxY)
  player.​setupConstraints​(floor: foreground.frame.maxY)
 addChild​(player)
  player.​walk​()
 
 // Set up game
 spawnMultipleGloops​()
 }

Build and run the project. Notice how all 10 drops spawn from the same location. Kind of boring, right? To fix that problem and enhance gameplay, you’ll add a little randomness.

Locate the spawnGloop() method and modify it so that it matches this:

 func​ ​spawnGloop​() {
 let​ collectible = ​Collectible​(collectibleType: ​CollectibleType​.gloop)
 
 // set random position
 let​ margin = collectible.size.width * 2
 let​ dropRange = ​SKRange​(lowerLimit: frame.minX + margin,
  upperLimit: frame.maxX - margin)
 let​ randomX = ​CGFloat​.​random​(in:
  dropRange.lowerLimit...dropRange.upperLimit)
 
  collectible.position = ​CGPoint​(x: randomX,
  y: player.position.y * 2.5)
 addChild​(collectible)
 
  collectible.​drop​(dropSpeed: ​TimeInterval​(1.0),
  floorLevel: player.frame.minY)
 }

Here, you’re using the SKRange class to define the lower and upper limits for the x-position. You might remember this class from Use Constraints to Limit Movement. Specifically, you’re using the min and max values of frame.x to keep the drops within the visible area of the view. You then use CGFloat.random to grab a random value that falls between that range.

Build and run the project, and watch in awe as 10 glorious gloop drops fall from the sky to the platform below.

images/ChainingActionsAndUsingIterativeDesign/spritekit-build-03-alt.png

Now that you have multiple drops falling and accumulating on the platform, it’s time to move on to the next iteration: using the current game level to add incremental challenge to the game.

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

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