Improve Code with Iterative and Incremental Development

With iterative and incremental development, you can shake out any potential problems with your code and your game’s design. Adding code in smaller chunks makes it much faster and easier to pinpoint code that’s causing issues.

The phrase, “iterative and incremental development” is a combination of iterative design and the incremental build model. With iterative design, you develop your game using a set of repeated cycles. With the incremental build model, you develop your code in smaller chunks. With each iteration and chunk of code you add, you get closer to your end goal.

For the collectible items, you’ll start with a single, stationary collectible—a gloop drop. Once you get that working, you’ll move on to adding more functionality with every iteration.

The first method you’ll add is spawnGloop(). You’ll use this method to spawn a single drop and add it to the scene.

Start by opening the GameScene.swift file. Immediately below the didMove(to:) method, add the following code:

 // MARK: - GAME FUNCTIONS
 
 /* ####################################################################### */
 /* GAME FUNCTIONS START HERE */
 /* ####################################################################### */
 
 func​ ​spawnGloop​() {
 let​ collectible = ​Collectible​(collectibleType: ​CollectibleType​.gloop)
  collectible.position = ​CGPoint​(x: player.position.x,
  y: player.position.y * 2.5)
 addChild​(collectible)
 }

With this code, you initialize a collectible object using the new Collectible class. You then set its position to be directly above the player node. You also set its y-position to be 2.5 times higher than that of the player.position.y. Finally, you add the collectible node to the scene.

Your next step is to call the new spawnGloop() method. Inside the didMove(to:) method, below the line that reads player.walk(), add the following code:

 // Set up game
 spawnGloop​()

This code calls the spawnGloop() method you added earlier.

Build and run the project to ensure everything is working so far. If all is well, you’ll see a single drop of gloop above the player, and your first iteration is complete as shown in the image.

images/ChainingActionsAndUsingIterativeDesign/spritekit-build-01.png

At this point, you’re ready for the next iteration (and to learn how to chain SpriteKit actions together).

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

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