Swift 2 SkScene正在改变

时间:2016-01-17 22:32:25

标签: ios swift skscene

我是编码(swift)和制作游戏的新手。我试图让我的游戏切换到我创建的SKScenes,出于某种原因,当我尝试切换场景时,我只是得到一个灰色屏幕,我不明白为什么。 (应该注意的是,我试图转换的场景已经从前一个主场景改变了。下面是我用来改变场景的方法。(我不想在SKScene s之间进行任何删除)

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

            }

        }
    }
}

这是我要改为的场景:(名称为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)
    }
}

}

1 个答案:

答案 0 :(得分:0)

要转换到新场景,您应该执行以下操作:

if let winGameScene = WinGameScene(fileNamed: "WinGameScene") {

            self.view?.presentScene(winGameScene)
}else{
     print("error")
}

重要的是,在尝试执行此操作之前,必须创建名为WinGameScene.sks的资源文件。你可以通过转到:File-&gt; New-&gt; File ...-&gt; Resource-&gt; SpriteKit Scene来实现。