Book Image

Game Development with Swift

By : Stephen Haney
Book Image

Game Development with Swift

By: Stephen Haney

Overview of this book

Table of Contents (18 chapters)
Game Development with Swift
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Spawning the star power-up at random


We still need to add the star power-up into the world. We can randomly spawn a star every 10 encounters to add some extra excitement. Follow these steps to add the star logic:

  1. Add a new instance of the Star class as a constant on the GameScene class:

    let powerUpStar = Star()
  2. Call the star's spawn function, anywhere inside the GameScene didMoveToView function:

    // Spawn the star, out of the way for now
    powerUpStar.spawn(world, position: CGPoint(x: -2000, y: - 2000))
  3. Inside the GameScene didSimulatePhysics function, update your new encounter code as follows:

    // Check to see if we should set a new encounter:
    if player.position.x > nextEncounterSpawnPosition {
    encounterManager.placeNextEncounter(
        nextEncounterSpawnPosition)
        nextEncounterSpawnPosition += 1400
        
        // Each encounter has a 10% chance to spawn a star:
        let starRoll = Int(arc4random_uniform(10))
        if starRoll == 0 {
            if abs(player.position.x - powerUpStar.position.x) > 1200...