Search code examples
swiftsprite-kitskscene

Switch between Scenes in Sprite-Kit (Swift) using LabelNode


I use SKLabelNodes as Buttons and a if inside a menu helper to detect touches on them. Printing works (so does detection), but how do I switch to my Game Scene properly?

func menuHelper(touches: NSSet) {
        for touch in touches {
            let nodeAtTouch = self.nodeAtPoint(touch.locationInNode(self))
            if nodeAtTouch.name == "title" {
                print("Title pressed")
            }
            else if nodeAtTouch.name == "newGame" {
            let scene = GameScene()
            }
        }

}

Solution

  • You just have to make a new scene (and transition, but that's optional) and present it by using appropriate SKView's method:

    else if nodeAtTouch.name == "newGame" {
    
       let transition = SKTransition.fadeWithDuration(1.0)
    
       let nextScene = GameScene(size: scene!.size)
       nextScene.scaleMode = .AspectFill //set the scale mode like you did in your view controller
    
       scene?.view?.presentScene(nextScene, transition: transition)
    }
    

    Without transition , you would just use presentScene method instead of presentScene:transition