Jake
Jake

Reputation: 11

Swift "Start Game Menu"

I'm still very new, however I'm almost done with a game, the problem is i don't have a scene that allows the user to hit "Tap to play" so when the app loads up, the user is just thrown into into the game with no warning. I know i need to create the scene under

         override func didMoveToView(view: SKView) {
        /* Setup your scene here */

but i can't seem to find on how to do this! I've looked everywhere, but can't find anything to fix this, all help is appreciated, I hope this question is clear, if not, my apologies!

Upvotes: 1

Views: 1792

Answers (1)

ronatory
ronatory

Reputation: 7324

A simple startScene with transition to a gameScene for your game could be something like this:

In your StartScene class create a constant for a SKLabelNode

// you can use another font for the label if you want...
let tapStartLabel = SKLabelNode(fontNamed: "STHeitiTC-Medium")

then in didMoveToView :

override func didMoveToView(view: SKView) {
    // set the background
    backgroundColor = SKColor.whiteColor()

    // set size, color, position and text of the tapStartLabel
    tapStartLabel.fontSize = 16
    tapStartLabel.fontColor = SKColor.blackColor()
    tapStartLabel.horizontalAlignmentMode = .Center
    tapStartLabel.verticalAlignmentMode = .Center
    tapStartLabel.position = CGPoint(
        x: size.width / 2,
        y: size.height / 2
    )
    tapStartLabel.text = "Tap to start the game"

    // add the label to the scene   
    addChild(tapStartLabel)
}

then in touchesBegan to go from startScene to your current gameScene:

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

    let gameScene = GameScene(size: size)
    gameScene.scaleMode = scaleMode

    // use a transition to the gameScene
    let reveal = SKTransition.doorsOpenVerticalWithDuration(1)

    // transition from current scene to the new scene
    view!.presentScene(gameScene, transition: reveal)
}

to make the StartScene your first scene with the Tap to play label add this code in the viewDidLoad() method of your GameViewController:

override func viewDidLoad() {
    super.viewDidLoad()

    let scene = StartScene(size: view.bounds.size)

    // Configure the view.
    let skView = self.view as! SKView
    skView.showsFPS = true
    skView.showsNodeCount = true

    /* Sprite Kit applies additional optimizations to improve rendering performance */
    skView.ignoresSiblingOrder = true

    /* Set the scale mode to scale to fit the window */
    scene.scaleMode = .ResizeFill

    skView.presentScene(scene)

}

It should look like that: enter image description here

Upvotes: 3

Related Questions