This tutorial will take you through the process of creating a simple Magic 8 ball style game
import UIKit
, change it to import SpriteKit
import SpriteKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let scene = BallScene(size: view.bounds.size)
// Configure the view.
let skView = self.view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .aspectFill
skView.presentScene(scene)
}
}
class
, add the code shown in the snippet below:
override init(size: CGSize) {
super.init(size: size)
backgroundColor = SKColor.black
let backgroundImage = SKSpriteNode(imageNamed: "ball.png")
backgroundImage.position = CGPoint(x: frame.midX, y: frame.midY)
backgroundImage.name = "ball"
addChild(backgroundImage)
}
This code adds the ball to the centre of the screen. Note that by default, the anchor point of sprites is their centre, so the centre of the ball image is positioned to the mid x and y positions of the at the centre position of the scene.
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//create a message node
let message = SKLabelNode(fontNamed: "Chalkduster")
//set the text and size
message.text = "Maybe"
message.fontSize = 20
//position it in the center of its parent (the ball)
message.position = CGPoint(x: 0, y: 0)
//add the message to the ball
backgroundImage.addChild(message)
It would be useful to change the message, so the magic 8 ball works more like a real one. To do this we need to be able to access the message label, and change its message at random. (We also need to be able to find the ball, as the message label is a 'child' of the ball)
message.name = "message"
{
, start typing the word 'touchesBegan'. Xcode will suggest a method to enter, and pressing the tab key once the relevant suggestion is visible will input it. It should appear as follows:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {}
This method is called every time the user touches the screen in some way, the next step is to add code inside this method to locate the label and change its message based on a random number.
//find the label node
let ball = childNode(withName: "ball")
let message = ball?.childNode(withName: "message") as! SKLabelNode
//generate a random number between 0 and 1
let randomNumber = Int.random(in: 0...1) //for versions of Swift <4.2 use: let randomNumber = Int(arc4random_uniform(2))
//if the number is a 1, set the message to Yes, if not set it to No
if (randomNumber == 1) {
message.text = "Yes"
}else{
message.text = "No"
}
Instead of using if
and else
to make decisions, we can choose a particular option based on a particular situation. In programming this is known as switch case
.
Below is an example of how we can change the text of our label based on the random number
switch randomNumber {
case 0:
message.text = "Sure Thing"
break
case 1:
message.text = "Probably Not"
break
default:
break
}
In this example we are making a decision based on the randomNumber, saying that in the case it is a 0, set the label to read 'Sure Thing', and then 'break' which means to stop running this bit of code. However, in the case that the randomNumber is a 1, it will set the text to read 'Probably not'. we also have an option to set if none of the cases are met - which in this case does nothing.
At present our randomNumber is only 0 or 1, because we are getting the remainder when we divide a number by 2, modify the code so that we can have more options (I would suggest 5 or 6) and instead of using if and else to make decisions, use the switch case example, but extend it to provide more answers. If you get stuck ask your tutor for help.
Animating the message is fairly simple, we make it invisible by setting it's alpha value to 0:
message.alpha=0
then once we've set the new message we create and apply a fade in animation as shown below:
let fadeIn = SKAction.fadeIn(withDuration: 1.0)
message.run(fadeIn)