Moving Obstacles

Preview the app, and you should see a striped ground. That’s nice, but the stripes are stationary. Your hero deserves a more action-packed environment. To make the stripes whoosh to the left by changing the bar’s position each frame, add the following code to the update method in PlayScene:

override func update(currentTime: NSTimeInterval) {    if self.runningBar.position.x <= maxBarX {        self.runningBar.position.x = self.origRunningBarPositionX    }    //move the ground    runningBar.position.x -= CGFloat(self.groundSpeed)}

The bar.png graphic is wider than the frame so you we can move it to the left without exposing any white space. Just as the bar is sliding out of frame, the game resets ...

Get Learning Swift™ Programming 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.