Spawning the Power-up Star at random
We still need to add the Power-p Star into the world. We can randomly spawn a Star every ten encounters to add some extra excitement. Follow these steps to add the Star logic:
Add a new instance of the
Star
class as a constant on theGameScene
class:Â Â Â Â Â Â Â let powerUpStar = Star()
Anywhere inside the
GameScene didMove
function, add the Star as a child of theGameScene
and position it:Â Â Â Â Â Â Â // Place the star out of the way for now: Â Â Â Â Â Â self.addChild(powerUpStar) Â Â Â Â Â Â powerUpStar.position = CGPoint(x: -2000, y: -2000)
Inside the
GameScene didSimulatePhysics
function, update your new encounter code as follows (new code in bold):Â Â Â Â Â Â Â // Check to see if we should set a new encounter: Â Â Â Â Â Â if player.position.x > nextEncounterSpawnPosition { Â Â Â Â Â Â encounterManager.placeNextEncounter( Â Â Â Â Â Â currentXPos: nextEncounterSpawnPosition) Â Â Â Â Â Â nextEncounterSpawnPosition += 1200 ...