无法在 ViewController 和 SKScene 之间设置委托
Can't set delegate between ViewController and SKScene
我想在我的 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")
}
}
您没有在 MainVC 中将 gameViewControllerDelegate 委托设置为 self 那么怎么可能调用该 SKscene 的委托。所以在 MainVC
中设置为 self
我想在我的 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")
}
}
您没有在 MainVC 中将 gameViewControllerDelegate 委托设置为 self 那么怎么可能调用该 SKscene 的委托。所以在 MainVC
中设置为 self