Cannot programmatically activate segue in SpriteKit using Xcode 8 and Swift 3

I am making a game in Xcode 8 using SpriteKit, and you have a very difficult time when the program starts a session. I went to "Main.storyboard" and took a break between my "GameViewController" and "MenuViewController", giving it the identifier "toMenu".

Failed to try # 1

In my GameScene.swift file, I added the following update function code.

override func update(_ currentTime: TimeInterval) { // Called before each frame is rendered if gameIsRunning == false{ score = 0 performSegue(withIdentifier: "toMenu", sender: self) } } 

My goal is to have segue run whenever gameIsRunning is false. However, I got the following error:

 Use of unresolved identifier 'performSegue' 

This is odd because performSegue is a declaration according to the API Reference .

Failed to try # 2

I tried this solution by putting the following code in "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 to try # 3

I found this solution very confusing, but I still follow. I already did step 1, as indicated in step 2, I put the following code at the bottom of "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 described in step 3 of the solution

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

Then I made step 4 of the decision, so I put the following code in "MenuViewController.swift"

  override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.post(NSNotification(name: NSNotification.Name(rawValue: "toMenu"), object: nil) as Notification) 

Then I put the following code in my GameScene.swift file to actually call the function.

  override func update(_ currentTime: TimeInterval) { // Called before each frame is rendered if gameIsRunning == false{ score = 0 GameViewController().goToDifferentView() } 

Then I ran my game on my iPhone, and everything went well until I got to the part of the game where gameIsRunning turns to false. Instead of executing segue, I got the following error:

 Thread 1: Signal SIGABRT 

I got this error several times, and it was always that I didn’t connect correctly to "Main.storyboard". However, this is an incredibly simple game, and everything looks fine. I also did not get this error until adding the code that I just showed you. I have confirmed several times that segue has the identifier "toMenu". I totally don’t understand why segue is not working and why my Attemp # 3 is causing a SIGABRT error.

+7
ios xcode swift sprite-kit segue
source share
1 answer

Since you are trying to call this session in the update () function, it is called many times per second. 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 put print("update") in your if , you'll see what I mean. Setting gameIsRunning bool to true if you insured it only once.

+2
source share

All Articles