Many years ago there was a game on Teletext called Bamboozle! In the game, players had to correctly answer between 12 and 30 questions, in order correctly. Each question had 4 possible answers (selected using the Red, Green, Blue and Yellow buttons on the remote control) If a player got one question wrong they had to restart the entire game. For those of you who remember Teletext and how slow it was loading a page, this could be a frustrating process
This tutorial will help you build a game to replicate the functionality of the Bamboozle game
if let scene
so it reads as show below (note the word 'if' is removed, along with the opening brace):
let scene = GameScene(size: view.bounds.size)
}
at the end of the methodimport UIKit
, change it to read import SpriteKit
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override init(size: CGSize) {
super.init(size: size)
}
override func didMove(to view: SKView) {
}
didMoveToView
method add the code below to create a label for the question:
//set background color
backgroundColor = SKColor.black
//create a label node for the question
let question = SKLabelNode(text: "What is 4 + 2?")
question.fontName = "Menlo"
question.position = CGPoint(x:frame.midX, y:frame.maxY-50)
//add the question to the scene
addChild(question)
let answer1 = SKLabelNode(fontNamed: "Menlo")
answer1.text = "2"
answer1.position = CGPoint(x:70, y:200)
answer1.fontColor = SKColor.red
addChild(answer1)
class
, and outside of any other function, add the following code:
//implicitly unwrapped optional (denoted by the ! after SKLabelNode)
private var rightAnswer : SKLabelNode!
The !
after SKLabelNode
denotes that this variable is optional, but it will be treated by default as if it has a value. (The alternative for an optional variable is to use a question mark ?
). If we were to omit the !
or ?
, then we would have to provide a default value for the right answer.
rightAnswer = answer3;
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
let touch :UITouch = touches.first!
let touchLocation = touch.location(in: self)
if rightAnswer.contains(touchLocation){
let q2Scene = Question2(size:size)
let fade = SKTransition.fade(withDuration: 0.5)
view?.presentScene(q2Scene, transition: fade)
}
else
{
//on the first screen we just stay where we are (until the player gets the right answer)
//for later scenes we will add code here to go right back to the first scene
}
}
You should now be able to run the project. Nothing will happen if the incorrect button is pressed, but when the right answer is selected the scene will change.
You can download a completed version of the game from GitHub
It should be possible to simply reuse one scene, loading the question and answer combination from a file. Carry out further research to identify how this might be done and attempt to implement it.