Ios Swift SpriteKit委托模式
我最近一直在努力学习如何使用代理,实际上我似乎大部分都理解它。然而,我有一个问题 我有1个GameViewController、1个StartScene和1个MenuSceneIos Swift SpriteKit委托模式,ios,swift,delegates,Ios,Swift,Delegates,我最近一直在努力学习如何使用代理,实际上我似乎大部分都理解它。然而,我有一个问题 我有1个GameViewController、1个StartScene和1个MenuScene protocol MenuSceneDelegate { func gameOver() func showGameCenter() } 在我的GameViewController中,我有两个方法希望从MenuScene调用。到目前为止,我一直在使用NSNotification,它工作得很好,但我正在尝试使用委托
protocol MenuSceneDelegate {
func gameOver()
func showGameCenter()
}
在我的GameViewController中,我有两个方法希望从MenuScene调用。到目前为止,我一直在使用NSNotification,它工作得很好,但我正在尝试使用委托
所以我在MenuScene建立了一个协议
protocol MenuSceneDelegate {
func gameOver()
func showGameCenter()
}
我不是这样引用它的
var menuSceneDelegate: MenuSceneDelegate?
在GameViewController中,我在顶部添加了MenuSceneDelegate。到目前为止,一切都很标准。然而,我的主要问题是,当我在GameViewController中这样设置代理时
let skView = view as! SKView!
var scene = StartScene(size: skView.bounds.size)
skView.ignoresSiblingOrder = true
scene.scaleMode = .ResizeFill
skView.presentScene(scene)
scene.menuSceneDelegate = self //Sets delegate to StartScene not MenuScene
它只适用于StartScene。如何将GameViewController委托从StartScene设置为MenuScene。如果我先介绍MenuScene,一切都很好。但是,我首先介绍StartScene,因此我试图理解如何将委托设置为MenuScene
我在下面尝试了类似的方法,但它不起作用,而且似乎不对
var menuScene = MenuScene(size: skView.bounds.size)
menuScene.menuSceneDelegate = self
我将感谢任何帮助或支持。非常感谢基本上,解决方案是在我从开始场景切换到游戏场景时设置代理,而不是在开始时设置代理。这是什么意思?这是一个很好的问题lol。这是一个很老的答案。我以后会更新的,很抱歉打扰你。我已经在代理/协议和继承阶梯上来回爬了几天。每次上2步,后退3步。呵呵。一旦你掌握了窍门,情况还不错。我将在以后更新关于继承/子类化的内容,查看这个答案。我不确定这是否有用。我去年问过这个问题