I would like to create a main menu for my game in swift.
I am using the following code:
import SpriteKit
class menuScene: SKScene {
SKViews
Let's say, like this M.W.E., you want a menu, a difficulty, and a game scene.
Then you can make a series of custom SKViews
to transition between.
This code loads the menuScene:
override func viewDidLoad() {
super.viewDidLoad()
let menuScene = MenuScene(size: view.bounds.size)
let skView = view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
skView.ignoresSiblingOrder = true
menuScene.scaleMode = .resizeFill
skView.presentScene(menuScene)
}
class MenuScene: SKScene {
let playButton = SKLabelNode()
override init(size: CGSize) {
super.init(size: size)
backgroundColor = SKColor.white
playButton.fontColor = SKColor.black
playButton.text = "play"
playButton.position = CGPoint(x: size.width / 2, y: size.height / 2)
addChild(playButton)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let touchLocation = touch!.location(in: self)
if playButton.contains(touchLocation) {
let reveal = SKTransition.doorsOpenVertical(withDuration: 0.5)
let difficultyScene = DifficultyScene(size: self.size)
self.view?.presentScene(difficultyScene, transition: reveal)
}
}
}
class DifficultyScene: SKScene {
let easyButton = SKLabelNode()
let hardButton = SKLabelNode()
let menuButton = SKLabelNode()
override init(size: CGSize) {
super.init(size: size)
backgroundColor = SKColor.white
easyButton.fontColor = SKColor.black
easyButton.text = "easy"
hardButton.fontColor = SKColor.black
hardButton.text = "hard"
menuButton.fontColor = SKColor.black
menuButton.text = "menu"
easyButton.position = CGPoint(x: size.width / 2, y: size.height / 2)
hardButton.position = CGPoint(x: size.width / 2, y: size.height / 2 - easyButton.fontSize * 2)
menuButton.position = CGPoint(x: size.width / 4 * 3, y: size.height / 4)
addChild(easyButton)
addChild(hardButton)
addChild(menuButton)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let touchLocation = touch!.location(in: self)
if easyButton.contains(touchLocation) {
let reveal = SKTransition.doorsOpenVertical(withDuration: 0.5)
let gameScene = GameScene(size: self.size, difficulty: easyButton.text!)
self.view?.presentScene(gameScene, transition: reveal)
}
if hardButton.contains(touchLocation) {
let reveal = SKTransition.doorsOpenVertical(withDuration: 0.5)
let gameScene = GameScene(size: self.size, difficulty: hardButton.text!)
self.view?.presentScene(gameScene, transition: reveal)
}
if menuButton.contains(touchLocation){
let reveal = SKTransition.doorsOpenVertical(withDuration: 0.5)
let menuScene = MenuScene(size: self.size)
self.view?.presentScene(menuScene, transition: reveal)
}
}
}
add this to your GameScene
:
init(size: CGSize, difficulty: String) {
super.init(size: size)
gameDifficulty = difficulty
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
Alternatively, you can use Storyboards. In the M.W.E. for another S.O. question they have a basic "menu" set up.
In your case, what you would do is:
Images