如何检测哪个场景? (SWIFT / spritekit / xcode的)

时间:2014-11-27 00:48:48

标签: xcode swift background scene

我在gameviewcontroller中使用此代码作为我的背景:

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

但我有一个开始(菜单)场景,我不想要这个背景,但仍然出现。

如何检测我是哪个场景(Gamescene / Menuscene等)?

还是有另一种方式吗?

2 个答案:

答案 0 :(得分:1)

SKSceneSKNode的子类。您可以使用name属性执行此操作。只需将GamesceneMenuscene的名称设置为'game''menu',如下所示:

scene.name = "game"

并检查财产:

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

答案 1 :(得分:0)

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("")
        }
    }