Adding the Power-up Star at random places

We still need to add the Power-up 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:

  1. Add a new instance of the Star class as a constant on the GameScene class:
            let powerUpStar = Star() 
  2. Anywhere inside the GameScene didMove function, add the Star as a child of the GameScene and position it:
            // Place the star out of the way for now: 
            self.addChild(powerUpStar) 
            powerUpStar.position = CGPoint(x: -2000, y: -2000) 
  3. Inside the GameScene didSimulatePhysics function, update your new encounter code as follows (the new code is in bold):
     // Check to see if we should set a new encounter: if player.position.x > nextEncounterSpawnPosition ...

Get Swift Game Development - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.