Search code examples
iosswiftskscene

Swift 2 SkScene Changing


I am new to both coding (swift) and making games. I am trying to get my game to switch SKScenes which I have created, for some reason when I try and switch scenes I just get a grey screen and I have no idea why. (It should be noted that the scene I am trying to transition from was already changed from the previous main scene. Below is the method I am using to change scenes. (I do not want any tranistion between the SKScenes)

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

    for touch in touches {
        let location = touch.locationInNode(self)

        if(self.nodeAtPoint(location).name != nil && self.nodeAtPoint(location).name != "enter") {
            userSequence.append(Int(self.nodeAtPoint(location).name!)!)
            print("userSequence:\(userSequence)")
        }
        if(self.nodeAtPoint(location).name == "enter"){
            //compareSequences()
            if PreviewNumbersGameScene.sharedInstance.TempGeneratedSequence == userSequence.reverse(){
                print(PreviewNumbersGameScene.sharedInstance.TempGeneratedSequence)
                print("good")
                let Winscene = WinGameScene(fileNamed: "WinGameScene")

                self.scene!.view?.presentScene(Winscene)
            }
            else{
                print("Temp:\(PreviewNumbersGameScene.sharedInstance.TempGeneratedSequence)")
                loose()
                print("bad")

            }

        }
    }
}

And this is the scene I want to change to: (Name is WinGameScene)

import SpriteKit

class WinGameScene: SKScene{


//MARK: - Variables




//MARK: - View
override func didMoveToView(view: SKView) {

    ///* Setup your scene here */

    loadView()
}


func loadView(){

    backgroundColor = SKColor.greenColor()
    print("winSceneLoaded")
}



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

    for touch in touches {

        let scene = PreviewNumbersGameScene(fileNamed: "PreviewNumbersGameScene")

        self.view?.presentScene(scene)
    }
}

}


Solution

  • In order to transition to a new scene, you should do something like this:

    if let winGameScene = WinGameScene(fileNamed: "WinGameScene") {
    
                self.view?.presentScene(winGameScene)
    }else{
         print("error")
    }
    

    The important thing here is that before you try to do this, you have to create a resource file called WinGameScene.sks. You can do that by going to: File->New->File...->Resource->SpriteKit Scene.