【问题标题】:Can't set delegate between ViewController and SKScene无法在 ViewController 和 SKScene 之间设置委托
【发布时间】:2019-01-22 19:18:43
【问题描述】:

我想在我的 GameViewController 和 SKScene 文件之间设置委托,但遇到错误。我的目标是从 SKScene 文件中调用方法。

错误是:“GameScene”类型的值没有成员“gameViewControllerDelegate”

我做错了什么?

提前致谢

MainSC.swift

import UIKit
import SpriteKit
import Firebase
import StoreKit
import GoogleMobileAds

class MainVC: UIViewController, GameViewControllerDelegate {


    override func viewDidLoad() {
        super.viewDidLoad()

     if let view = self.view as! SKView? {

            if let scene = SKScene(fileNamed: "GameScene") {
                let gameScene = scene as! GameScene

                //THIS GUY THROWING ERROR
                 gameScene.gameViewControllerDelegate = self 

                gameScene.scaleMode = .aspectFill
                gameScene.size.width = 414
                gameScene.size.height = 736
                view.presentScene(gameScene)

            }

            view.ignoresSiblingOrder = true

            view.showsFPS = true
            view.showsNodeCount = true
            view.showsPhysics = true

        }
    }
func callMethod(inputProperty:String) {
        print("inputProperty is: ",inputProperty)
    }
}

MainSC.swift

import UIKit
import SpriteKit
import GameplayKit
import Firebase
import GoogleMobileAds

protocol GameViewControllerDelegate : class {
    func callMethod(inputProperty:String)
}

class MainSC: SKScene {

    weak var gameViewControllerDelegate : GameViewControllerDelegate?

override func didMove(to view: SKView) {

       ...

       gameViewControllerDelegate?.callMethod(inputProperty: "call game view controller method")

    }

}

【问题讨论】:

  • 更新:我改变了部分。
  • if let scene = SKScene(fileNamed: "MainSC") { let gameScene = scene as! MainSC gameScene.gameViewControllerDelegate = self

标签: delegates viewcontroller skscene


【解决方案1】:

您没有在 MainVC 中将 gameViewControllerDelegate 委托设置为 self ,所以如何调用该 SKscene 的委托。所以在MainVC中设置为self

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多