Segue in Skscene to Uiviewcontroller

Segue in SKScene to UIViewController

You are calling the segue on the root viewController. I think that is the problem. You need to call the segue on the scene's viewController instead (where I am assuming you have created the segue, hence it is not being found on the root viewController).

Now the problem is that an SKScene does not have direct access to it's viewController, but just the view in which it is contained. You need to create a pointer to it manually. This can be done by creating a property for the SKScene:

class GameScene: SKScene {
weak var viewController: UIViewController?
...
}

Then, in the viewController class, just before skView.presentScene(scene)

scene.viewController = self

Now, you can access the viewController directly. Simply call the segue on this viewController:

func returnToMainMenu(){
viewController?.performSegueWithIdentifier("menu", sender: vc)
}

Unable to segue from Spritekit SKScene to another UIViewController, XCODE8/Swift

Just for anyone who might come across this, I solved it by using notification center. Ie added a notification observer on the parent view controller of the game scenes (which calls a function that performs the segue), and at the end point in the game where I wanted to do the segue, I posted to that notification, and it works great.

adding observer in ViewDidLoad of UIViewController:

    override func viewDidLoad() {
super.viewDidLoad()

NotificationCenter.default.addObserver(self, selector: #selector(GameViewController.doaSegue), name: NSNotification.Name(rawValue: "doaSegue"), object: nil)
}

func doaSegue(){
performSegue(withIdentifier: "toNext", sender: self)
self.view.removeFromSuperview()
self.view = nil
}

And then calling it from within the game SKScene:

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "doaSegue"), object: nil)

How to perform segue from within a SKScene?

Segues belong to view controllers, not SpriteKit scenes. If you want to perform a segue from within a SpriteKit node's event handling code, you'll need to look up the view controller that owns the view presenting the scene. For example, in a SKScene subclass' touchesBegan:withEvent: method:

UIViewController *vc = self.view.window.rootViewController;
[vc performSegueWithIdentifier:@"id" sender:nil];

Segue from GameScene to a viewController Swift 3

The problem is that the viewController property is declared in GameScene, but when the scene is created it is of type SKScene. The simple way to fix it would be to initialise the scene as a GameScene then you will have access to the members declared in GameScene.

if let scene = GameScene(fileNamed: "GameScene") {
// Other code
scene.viewController = self
}

Also, you should try an exit seque when returning to main menu, but that's another topic.

How to get back from SKScene to UIViewController (MenuVC)?

try:

self.view.window!.rootViewController?.dismiss(animated: false, completion: nil)

This should dismiss all views above the rootViewController

edit:

This may work for you as we define the ViewController, make sure in your Storyboard your MenuVC has an Identifier:

let storyboard = UIStoryboard(name: "Main", bundle: nil)

let vc = storyboard.instantiateViewController(withIdentifier: "MenuVC") // Make sure is the same identifier as in the storyboard.

vc.view.frame = (self.view?.frame)!

vc.view.layoutIfNeeded()


UIView.transition(with: self.view!, duration: 0.3, options: .transitionFlipFromRight, animations:

{

self.view?.window?.rootViewController = vc

}, completion: { completed in

})

}

Edit:

It seems the best way to do this, is to host your SKScene class in another view controller. What I suggest is to create another ViewController where your SKScene will be, for example SKViewController. Embed your MenuVC inside a navigationController clicking on it in storyBoard, clicking Editor (from the menu at the top) > Embed in > Navigation Controller. Edit your MenuVC so that instead of opening your Scene, you just segue to the SKViewController, and open the scene there. To do this just create a segue from MenuVC in storyboard, with an identifier "MenuToSKScene" for example, and when you want to open the scene, from your MenuVC just do:

  self.performsegue(with identifier: "MenuToSKScene", sender: self)

Then do whatever you do currently in MenuVC in SKViewController instead to open the scene.

This will send you to the SKViewController, which has your SKScene class. Then when you want to go back to the menu, all you have to do is:

 self.dismiss(animated: true, completion: nil)

or create a segue from SKViewController to MenuVC with identifier "SKSceneToMenu" and call:

self.performsegue(with identifier: "SKSceneToMenu", sender: self)

So essentially, don't open the SKScene from MenuVC, but have a separate viewController SKViewController that you open when you want to open your scene, so that you can jump back to the menu easily.

How To Present a SKScene from a UIViewController

I think I might have found a solution-I didn't present the GameScene directly from the MenuViewController, but instead, when the playButton is pressed I transitioned to the GameViewController and then presented the GameScene from there.

I don't know why it doesn't work in the MenuViewController, but this method works.



Related Topics



Leave a reply



Submit