DNC
DNC

Reputation: 443

how to detect which scene? (swift/spritekit/xcode)

I am using this code for my background in the gameviewcontroller:

    let yourImage = UIImage(named: welkeLevel)
    let imageview = UIImageView(image: yourImage)
    self.view?.addSubview(imageview)

But I have a begin (menu) scene where I do not want this background and is still showing up.

How do I detect which scene I am (Gamescene / Menuscene etc.)?

Or is there another way?

Upvotes: 1

Views: 1610

Answers (2)

Alvin George
Alvin George

Reputation: 14294

I have worked out the same issue with a replay button. I have to identify which scene the user is in and reload the same when user clicks replay button.

1) Add a extension function  to detect current game level

extension SKScene {

    static func sceneWithClassNamed(className: String, fileNamed fileName: String) -> SKScene? {
        if let SceneClass = NSClassFromString("JumperCrab.\(className)") as? SKScene.Type,
            let scene = SceneClass(fileNamed: fileName) {
                return scene
        }
        return nil
    }

    func replayCurrentScene(currentLevelName: String,currentGameSceneIndex:Int)
    {
        GameViewController().getReplayCurrentSceneDetailsToVC(currentLevelName, currentGameSceneIndex: currentGameSceneIndex)
    }
}

2) Call the above extension method in every game scene.

    override func didMoveToView(view: SKView) {
        self.replayCurrentScene( "Round 1", currentGameSceneIndex: 0)
}

3) Set up a similar method at the View Controller.

    func getReplayCurrentSceneDetailsToVC(currentLevelName: String,currentGameSceneIndex:Int)
    {
        NSUserDefaults.standardUserDefaults().setInteger(currentGameSceneIndex, forKey: "LevelInt")
        NSUserDefaults.standardUserDefaults().synchronize()

        NSUserDefaults.standardUserDefaults().setObject(currentLevelName, forKey: "LevelName")
        NSUserDefaults.standardUserDefaults().synchronize()

    }

4)  Create variables 

    //GAME LEVEL
    var gameLevelSceneNameArray = NSArray()
    var currentGameSceneAtVC:String?
    var currentGameSceneIndexAtVC:Int?

5) Make my level list array

  override func viewDidLoad() {
        super.viewDidLoad()

        //Game Scenes
        gameLevelSceneNameArray = ["GameScene","SecondRound","ThirdRound", "FourthRound","FifthRound","SixthRound","SeventhRound", "EighthRound","NinethRound"]
}

6) Apply on the Replay Game button .

    @IBAction func replayButtonClicked(sender: AnyObject) {

    currentGameSceneIndexAtVC =   NSUserDefaults.standardUserDefaults().integerForKey("LevelInt")
    currentGameSceneAtVC = NSUserDefaults.standardUserDefaults().objectForKey("LevelName") as? String

 self.moveToSelectedGameLevel(currentGameSceneAtVC!, gameSceneName: self.gameLevelSceneNameArray[currentGameSceneIndexAtVC!] as! String)

    }

7) Set up a game scene navigation too (using switch case )


func moveToSelectedGameLevel(levelNameString:String, gameSceneName:String)
    {
        let skView = self.view as! SKView
        switch(levelNameString)
        {
        case "Round 1":
            if let gameScene = SKScene.sceneWithClassNamed(gameSceneName, fileNamed: gameSceneName) {
                skView.presentScene(gameScene)
            }
        case "Round 2" :
            if let gameScene = SKScene.sceneWithClassNamed(gameSceneName, fileNamed: gameSceneName) {
                skView.presentScene(gameScene)
            }
        case "Round 3":
            if let gameScene = SKScene.sceneWithClassNamed(gameSceneName, fileNamed: gameSceneName) {
                skView.presentScene(gameScene)
        default:
            print("")
        }
    }

Upvotes: 0

67cherries
67cherries

Reputation: 6951

SKScene is a subclass of SKNode. You can use the name property to do this. Just set the name of Gamescene or Menuscene to 'game' or 'menu' like this:

scene.name = "game"

And check the property like so:

if self.name == "game"{
    //Do something.
    println("game")
} else if self.name == "menu
    //Do something else.
    println("menu")
}

Upvotes: 1

Related Questions