Raggy
Raggy

Reputation: 47

Swift 3 SpriteKit main menu

I am using SpriteKit and trying to make a simple game with a main menu. I have already made the game but I am having trouble creating the main menu.

Below is my code for the main menu and I want it to change to the gameScene and start my game.

import SpriteKit

class MenuScene: SKScene {

    var aButton = SKShapeNode(circleOfRadius: 50)

    override func didMove(to view: SKView) {
        aButton.fillColor = SKColor.red
        aButton.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
        self.addChild(aButton)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let scene = GameScene(fileNamed: "aButton")
        scene?.scaleMode = .aspectFill
        view!.presentScene(scene!, transition: SKTransition.doorsOpenVertical(withDuration: 1))
    }
}

Upvotes: 2

Views: 657

Answers (2)

SumNeuron
SumNeuron

Reputation: 5198

I have posted a fairly thorough - yet basic - example for making a main menu is Swift3 for SKSpriteKit over here. In relation to your question, which attempts to do so programmatically rather than via the storyboard, I am also just copy-pasting the relevant part below:

Custom 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.

enter image description here

GameViewController

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)

}

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)

        }

    }


}

DifficultyScene

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)
        }

    }


}

GameScene

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")
}

Upvotes: 0

Nik
Nik

Reputation: 1664

Try changing this line:

let scene = GameScene(fileNamed: "aButton")

To this:

let scene = GameScene(size: self.scene.size)

The first line transitions to a .SKS file named "aButton". I assume you're trying to transition once the button is touched.

To do this, first give the button a name:

aButton.name = "button"

Then transition if it's touched. Your entire touchesBegan method should look something like this:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    let location = touches.first?.locationInNode(self)
    let touchedNode = self.nodeAtPoint(location)

    if touchedNode.name == "button" {
        let newScene = GameScene(size: self.scene.size)
        newScene.scaleMode = .aspectFill
        view!.presentScene(newScene, transition: SKTransition.doorsOpenVertical(withDuration: 1))
    }
}

Upvotes: 1

Related Questions