gpt4 book ai didi

swift - 如何在 Swift 中使用 SpriteKit 从 GameViewController 中的 GameScene 调用函数?

转载 作者:搜寻专家 更新时间:2023-11-01 05:38:14 26 4
gpt4 key购买 nike

我已经阅读了无数关于该问题的解决方案,但没有一个对我有用。

在 GameScene 中我有一个函数

func resetBalloon(){

balloonNode?.runAction(SKAction.moveTo(balloonLocation!, duration: 0.5))


}

在 GameViewController 中我有代码

class GameViewController: UIViewController {
var gameScene = GameScene()

其次是

func resetID(sender:AnyObject!){ //function is linked to a button
gameScene.resetBalloon()
println("all good") //this line runs when the button is pressed

}

有什么建议吗?提前感谢我能得到的任何帮助。

最佳答案

如果我没记错的话,GameViewController的主要作用只是呈现SKScene,而游戏逻辑应该在SKScene中。当然也有异常(exception),但我不认为 resetBalloon() 是其中之一。

所以你的 GameViewController 可能就是这样的:

class GameViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
let scene = GameScene(size: view.bounds.size)
let skView = view as! SKView
...
skView.presentScene(scene)
}
}

其他一切(甚至切换到另一个场景)都应该在 GameScene 中。

关于swift - 如何在 Swift 中使用 SpriteKit 从 GameViewController 中的 GameScene 调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34352051/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com