本文介绍了从GameScene到viewController Swift 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从GameScene到UIViewController制作一个segue。到目前为止,我的代码如下:

I need to make a segue from a GameScene to a UIViewController. My code so far is the following:

在GameViewController.swift中我添加了:

In the GameViewController.swift I added:

if let view = self.view as! SKView? {
        if let scene = SKScene(fileNamed: "GameScene") {
            scene.scaleMode = .aspectFill

            scene.viewController = self
            view.presentScene(scene)
        }
         ...

在我的GameScene.swift中我添加了

and in my GameScene.swift I added

class GameScene: SKScene, SKPhysicsContactDelegate {
    var viewController: UIViewController?

    ...

以及

func returnToMainMenu(){

    self.viewController.performSegueWithIdentifier("push", sender: viewController)

}

所以我的问题是,当我说 - scene.viewController = self - 我得到一个错误,表示类型'SKScene'的值没有成员'viewController'。我该怎么做才能解决这个问题?

So my problem is that when I state - scene.viewController = self - I get an error that says "value of type 'SKScene' has no member 'viewController'". What can I do to fix this?

推荐答案

问题是 viewController 属性在 GameScene 中声明,但是在创建场景时,它的类型为 SKScene 。修复它的简单方法是将场景初始化为 GameScene 然后您将有权访问在 GameScene

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.

这篇关于从GameScene到viewController Swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 09:12