简体   繁体   English

无法使用Xcode 8和Swift 3以编程方式在SpriteKit中触发segue

[英]Can't programmatically trigger segue in SpriteKit, using Xcode 8 and Swift 3

I'm making a game in Xcode 8 using SpriteKit and have been having a very difficult time programmatically triggering a segue. 我正在使用SpriteKit在Xcode 8中制作一个游戏,并且在编程上触发segue时非常困难。 I have gone into "Main.storyboard" and made a segue between my "GameViewController" and "MenuViewController" given it the identifier "toMenu". 我已经进入“Main.storyboard”并在我的“GameViewController”和“MenuViewController”之间制作了一个segue,因为它标识符为“toMenu”。

Failed Attempt #1 尝试失败#1

Within my GameScene.swift file I have added the following code to the update function. 在我的GameScene.swift文件中,我将以下代码添加到更新功能中。

override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered

    if gameIsRunning == false{
        score = 0
        performSegue(withIdentifier: "toMenu", sender: self)
    }
    }

My goal is for the segue to trigger whenever gameIsRunning is false. 我的目标是每当gameIsRunning为false时触发segue。 However, I got the following error: 但是,我收到以下错误:

Use of unresolved identifier 'performSegue'

This is odd because performSegue is a declaration according to the API Reference . 这很奇怪,因为performSegue是根据API Reference的声明。

Failed Attempt #2 尝试失败#2

I tried this solution by putting the following code into "GameScene.swift" 我通过将以下代码放入“GameScene.swift”来尝试此解决方案

override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered

    if gameIsRunning == false{
        score = 0
        GameScene(MenuViewController, animated: true, completion: nil)
    }

and I got this error: 我收到了这个错误:

Type of expression is ambiguous without more context

Failed Attempt #3 尝试失败#3

I found this solution very confusing but I following along anyways. 我发现这个解决方案非常令人困惑,但无论如何我都在追随。 I had already done step 1, so as stated in step 2, I put the following code at the bottom of "GameViewController.swift" 我已经完成了第1步,所以如第2步所述,我将以下代码放在“GameViewController.swift”的底部

func goToDifferentView() {

self.performSegue(withIdentifier: "toMenu", sender: self)

}

and didn't get an error! 并没有得到错误! Then I added this code to "GameViewController.swift" as instructed in step 3 of the solution 然后我按照解决方案的第3步中的说明将此代码添加到“GameViewController.swift”

    override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(self, selector: #selector(goToDifferentView), name: NSNotification.Name(rawValue: "toMenu"), object: nil)

Then I did step 4 of the solution so I put the following code into "MenuViewController.swift" 然后我做了解决方案的第4步,所以我将以下代码放入“MenuViewController.swift”

    override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.post(NSNotification(name: NSNotification.Name(rawValue: "toMenu"), object: nil) as Notification)

I then put the following code into my "GameScene.swift" file to actually call the function. 然后我将以下代码放入我的“GameScene.swift”文件中以实际调用该函数。

    override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered

    if gameIsRunning == false{
        score = 0
        GameViewController().goToDifferentView()
    }

I then ran my game on my iPhone and everything went well until I get to the part of the game where gameIsRunning turns to false. 然后我在我的iPhone上运行我的游戏,一切顺利,直到我进入游戏的部分,其中gameIsRunning变为假。 Instead of the segue doing its thing, I got the following error: 而不是segue做它的事情,我得到以下错误:

Thread 1: Signal SIGABRT

I've gotten this error several times and it's always been that I have something hooked up incorrectly in "Main.storyboard". 我已经多次得到这个错误,而且我总是在“Main.storyboard”中错误地连接了一些东西。 However, this an incredibly simple game and everything looks normal. 然而,这是一个非常简单的游戏,一切看起来很正常。 I also didn't get this error until adding the code that I just showed you. 在添加我刚刚向您展示的代码之前,我也没有收到此错误。 I have confirmed several times that the segue has the identifier "toMenu". 我已多次确认segue具有标识符“toMenu”。 I have absolutely no clue why the segue won't work and why my Attemp #3 is causing a SIGABRT error. 我完全不知道为什么segue不起作用以及为什么我的Attemp#3导致SIGABRT错误。

Since you are trying to trigger this segue in the update() function, it's being called many times per second. 因为你试图在update()函数中触发这个segue,所以它每秒被调用很多次。 Try the following code and it should work. 尝试以下代码,它应该工作。

override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered

if gameIsRunning == false{
    score = 0
    gameIsRunning = true
   NotificationCenter.default.post(NSNotification(name: NSNotification.Name(rawValue: "toMenu"), object: nil) as Notification)
}

If you were to place print("update") in your if statement you would see what I mean. 如果您在if语句中放置print("update") ,您会看到我的意思。 Setting the gameIsRunning bool to true inside your if will insure it only gets called once. 在你的if中将gameIsRunning bool设置为true将确保它只被调用一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM