I created a really simple game in xcode using spritekit and swift. Now I want to have a main menu that shows up when I first start the app. Then i want to have a button that when tapped on, will start the game. Is there a way to do this? Should i be using story board? thanks so much! :)
3 Answers
By using SpriteKit you can do it this way:
In your GameViewController.swift replace your code with this code:
import UIKit
import SpriteKit
class GameViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        //load your GameScene from viewController
        let scene = GameScene(size: view.bounds.size)
        let skView = view as! SKView
        skView.showsFPS = false
        skView.showsNodeCount = false
        skView.ignoresSiblingOrder = true
        scene.scaleMode = .ResizeFill
        skView.presentScene(scene)
    }
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
}
This code will load GameScene when you start your game.
Add this code in your GameScene.swift class:
import SpriteKit
class GameScene: SKScene {
    //create playbutton instance
    let playButton = SKSpriteNode(imageNamed: "play_unpresed")
    override func didMoveToView(view: SKView) {
        backgroundColor = UIColor.greenColor()
        addPlayButton()  //add playbutton
    }
    func addPlayButton(){
        playButton.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
        playButton.xScale = 0.2
        playButton.yScale = 0.2
        self.addChild(playButton)
    }
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        /* Called when a touch begins */
        for touch in (touches as! Set<UITouch>){
            let location = touch.locationInNode(self)
            //this will detect touch on play button
            if self.nodeAtPoint(location) == self.playButton {
                //it will transits to the next scene
                let reveal = SKTransition.flipHorizontalWithDuration(0.5)
                let letsPlay = playScene(size: self.size)
                self.view?.presentScene(letsPlay, transition: reveal)
            }
        }
    }
    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }
}
This will load a scene with one button now when you press a button it will take you to your playScene And for that you have to create a new file by clicking Command + N then iOS Source -> Cocoa Touch class -> next -> add class name playScene -> Subclass of SKScene -> and create it.
Add import SpriteKit in your playScene.swift
Check THIS sample project for more info.
And HERE is the easy tutorial for spriteKit.
- 71,228
 - 33
 - 160
 - 165
 
- 
                    
 - 
                    
 - 
                    If i replace the code in gameviewcontroller then my game will not work properly? – King_Cui Aug 09 '15 at 07:34
 - 
                    I suggest you to read the tutorial which I suggest you in my answer and it will give you basic knowledge about spriteKit. – Dharmesh Kheni Aug 09 '15 at 07:36
 
You can just drag one viewController to the storyBoard.
set it
Is Initial View Controller, add one button on itcontrol+dragfrom the button to your Game View Controller. chooseshow
I've test it. It can work. When I click the button, It will navigate to the Game view controller
- 1,611
 - 13
 - 20
 
- 
                    hold `control` key, use your mouse left click on the button, don't release, It will show a segue line, darg it to your `sprite view controller` – ronan Aug 08 '15 at 07:16
 
Storyboards
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:
- go to Main.storyboard.
 - on the right-hand tool bar, find view controller
 - drag view-controller into Main.storyboard
 - click on the new view-controller
 - click - on the right-hand tool bar - the identity inspector (looks like a business card)
 - change Class to GameViewController
 - click on view within the hierarchy on the left (under the new view controller)
 - click the identity inspector
 - change class to SKView
 - click on the original view controller
 - click on the identity inspector
 - change class to UIViewController
 - click on the view within the original UIViewController
 - click on identity inspector
 - change class to UIView
 - find button at the bottom of the right-hand side tool bar
 - drag it onto the first view
 - right click drag from the button to the second view
 - on the pop-up menu, under action segue, click show
 - right click drag from the button up, add horizontally center constraints
 - right click drag from the button to the right, add vertically center constraints
 
Images







