How To Make A Game Like Color Switch with SpriteKit and Swift

Learn how to make a game like Color Switch using SpriteKit and Swift. By Brian Broom.

Leave a rating/review
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Add a Different Obstacle

It would be a more interesting game with more than one type of obstacle. There are many different things you can add, but a square is simple to build.

bcb-square-obstacle

Add a new method to GameScene.swift below addCircleObstacle():

func addSquareObstacle() {
  let path = UIBezierPath(roundedRect: CGRect(x: -200, y: -200,
                                              width: 400, height: 40), 
                          cornerRadius: 20)
  
  let obstacle = obstacleByDuplicatingPath(path, clockwise: false)
  obstacles.append(obstacle)
  obstacle.position = CGPoint(x: size.width/2, y: obstacleSpacing * CGFloat(obstacles.count))
  addChild(obstacle)

  let rotateAction = SKAction.rotate(byAngle: -2.0 * CGFloat(M_PI), duration: 7.0)
  obstacle.run(SKAction.repeatForever(rotateAction))
}

Building the square obstacle is very similar to building the circle. You create a path for the bottom section of the square, then rotate it around to create the other three sections. Note that this shape copies around counterclockwise, and rotates at a different speed so that its behavior is different than the circle obstacle.

Next, replace addObstacle() with the following:

func addObstacle() {
  let choice = Int(arc4random_uniform(2))
  switch choice {
  case 0:
    addCircleObstacle()
  case 1:
    addSquareObstacle()
  default:
    print("something went wrong")
  }
}

Here, you generate a random integer that is either 0 or 1, and use that result to decide which obstacle to build.

Build and run the project. You may have to pass several obstacles, or die a few times, to see both types due to the nature of random numbers.

Not just all circles anymore.

Not just all circles anymore.

Not just all circles anymore.

Scoring

A game is much more fun when you can brag to your friends about beating their high scores. To add scores to your game, start by adding properties to the top of GameScene.swift:

let scoreLabel = SKLabelNode()
var score = 0

Next, set up the label at the end of didMove(to:):

scoreLabel.position = CGPoint(x: -350, y: -900)
scoreLabel.fontColor = .white
scoreLabel.fontSize = 150
scoreLabel.text = String(score)
cameraNode.addChild(scoreLabel)

Note that you add the label to cameraNode, not the scene itself, so that the label doesn't scroll off the screen when the player moves up.

Then, replace the // TODO: Update score line in update(_:) with:

score += 1
scoreLabel.text = String(score)

Finally, add the following to the end of dieAndRestart():

score = 0
scoreLabel.text = String(score)

With these pieces of code, you update the score each time the player passes an obstacle, and reset it to 0 when the player dies.

Build and run to see the new score counter.

I'm sure you can beat this...

I'm sure you can beat this...

I'm sure you can beat this...

Where to Go From Here?

Sometimes simple mechanics can make for fun little games. You can download the completed project here.

Now that you've got a complete game, you may want to spruce it up a bit. One thing you could add is more randomness. For example, the player could start as a random color, and the obstacles could rotate in a random direction, at random speeds.

There are also many different obstacles you could add, such as circles inside circles or plus signs. Be creative — and if you create a new obstacle, post a screenshot in the forum. I'd love to see it!

bcb-plus-obstacles

For more on SpriteKit, check out the 2D Apple Games by Tutorials book or some of our other Apple Game Frameworks tutorials. We hope you enjoyed this tutorial!

Brian Broom

Contributors

Brian Broom

Author

Kyle Gorlick

Tech Editor

Chris Belanger

Editor

Morten Faarkrog

Final Pass Editor

Tammy Coron

Team Lead

Over 300 content creators. Join our team.