将SKScene链接到UIViewController

时间:2016-06-22 12:53:30

标签: uiviewcontroller skscene

当你在swift中创建一个新游戏文件时,默认情况下你会在main.storyboard上给你一个GameScene.swift文件,一个GameViewController.swift文件和一个UIViewController。在我的代码中,我创建了另一个SKScene(类似于已经存在的GameScene),我无法将它链接到我在main.storyboard上的另一个UIViewController。我想这样做,因为我需要从我的GameScene到这个新场景,然后到我的主ViewController /初始ViewController

如何正确链接SKScene和ViewController?

下面是EndSceneViewController.swift文件,然后是我要链接到的EndScene.swift文件。

import UIKit
import SpriteKit

class EndSceneVC: UIViewController{


    override func viewDidLoad() {
        super.viewDidLoad()


        if let scene = EndScene(fileNamed:"EndScene") {
            // Configure the view.
            let skView = self.view as! SKView
            skView.showsFPS = true
            skView.showsNodeCount = true

            /* Sprite Kit applies additional optimizations to improve rendering performance */
            skView.ignoresSiblingOrder = true

            /* Set the scale mode to scale to fit the window */
            scene.scaleMode = .AspectFill
            scene.viewController = self
            skView.presentScene(scene)

        }
    }

    override func shouldAutorotate() -> Bool {
        return true
    }

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
            return .AllButUpsideDown
        } else {
            return .All
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Release any cached data, images, etc that aren't in use.
    }

    override func prefersStatusBarHidden() -> Bool {
        return true
    }
}



import SpriteKit

class EndScene : SKScene {
    var viewController : UIViewController?

    override func didMoveToView(view: SKView) {
        print("hello")
    }

}

1 个答案:

答案 0 :(得分:0)

您想通过切换控制器从一个场景移动到另一个场景吗?

Root ViewController可以从当前显示的SKScene进行定位 - 您不必为场景添加其他属性。

class GameScene: SKScene {
    override func didMoveToView(view: SKView) {
        let viewController = self.view?.window?.rootViewController
    }
}