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.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
How To Make A Game Like Color Switch with SpriteKit and Swift
30 mins
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.
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.
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.
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!
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!