Moving between GameScene and ViewController Swift

大憨熊 提交于 2019-12-08 05:39:18

问题


I want to move from my GameScene to ViewController, when I touch an Image and back to GameScene when I touch an UIButton. It worked from ViewController to GameScene, because it is UIButton.

I have done it like this:

@IBAction func playbut(sender: UIButton) {
    var scene = GameScene(size: CGSize())
    let skView = self.view as! SKView!
    skView.ignoresSiblingOrder = true
    scene.scaleMode = .ResizeFill
    scene.size = skView.bounds.size
    skView.presentScene(scene)
}

But I don't know what code to write to go back to ViewController, when I touch that image, which is on GameScene.

What should I write to GameScene?

Thanks


回答1:


I don't think this is what you're expected to do... You should be using an SKScene - instead of a UIViewController - for your navigation/menu.

But, one post I found did explain this (albeit it's in Obj-C):

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone"     bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"PurchasedVC"];
//Call on the RootViewController to present the New View Controller
[self.view.window.rootViewController presentViewController:vc animated:YES completion:nil];

The Swift version should look something like this (I'm no expert, but the syntax isn't too bad)...

var mainStoryboard = UIStoryboard(name: "Main_iPhone", bundle: nil)
var vc = mainStoryboard.instantiateViewControllerWithIdentifier("PurchasedVC") as! UIViewController
self.view.window?.rootViewController?.presentViewController(vc, animated: true, completion: nil)

In terms of other answers:

  • This is probably the best post I've seen on this topic: Presenting UIViewController from SKScene
  • Additionally, I found this one on transitioning between scenes: SpriteKit how to create and transition to different scenes (Swift language)

I hope that helps. I had the same question, and to be honest, it looks like I was just doing it wrong the whole time!



来源:https://stackoverflow.com/questions/29757044/moving-between-gamescene-and-viewcontroller-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!