【发布时间】: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