简体   繁体   中英

Present GameScene from UIViewController

I want to present a GameScene with GameViewController from my ViewController . I tried:

// let GameControllerA2 = GameViewController()

            // let transition = CATransition()
            // transition.duration = 0.5
            // transition.type = kCATransitionPush
            // transition.subtype = kCATransitionFromRight
            // view.window!.layer.add(transition, forKey: kCATransition)

            // self.present(GameControllerA2, animated: true, completion: nil)

            if let vc3 = self.storyboard?.instantiateViewController(withIdentifier: "A2ID") as? GameViewControllerA2 {
                let appDelegate = UIApplication.shared.delegate as! AppDelegate
                appDelegate.window?.rootViewController!.present(vc3, animated: true, completion: nil)

Code Photo

But this all doesn't work. Just if I simply drag [Button] from the Storyboard to the the GameViewController it doesn't crash, but I need it programmatically. Any questions about my process? Ask, I'll reply.

First you need to assign a storyboard identifier to the view controller you want to present on top of your current controller. You can do that in the storyboard in the identity inspector section

Let that identifier be "StoryboardIdentifierForNewClass" and the class name be NewClassViewController. Then,

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("StoryboardIdentifierForNewClass") as! NewClassViewController
self.presentViewController(vc, animated: true, completion: nil)

The idea is pretty simple.

First of all you need to add an SKView to your view controller.

Next you create a GameScene object and you add it to the SKView

view.presentScene(scene)

That's it. Now you have your GameScene inside your ViewController.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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