gpt4 book ai didi

swift - tvOS pressesBegan() 未在 GameScene 中更新

转载 作者:行者123 更新时间:2023-11-30 13:42:30 27 4
gpt4 key购买 nike

在我的 GameScene 中,当我使用“touchesBegan()”更新变量时,它工作正常,但是当我将“pressesBegan()”从 GameViewController 传递到 GameScene 时,代码会变得很兴奋,但更改会被忽略。

游戏 View Controller :

class GameViewController: UIViewController {
...
override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
let scene = GameScene(fileNamed: "GameScene")
scene?.pressesBegan(presses, withEvent: event)
}

游戏场景:

class GameScene: SKScene {
var test = true
...
override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
test = false
print(test)
}
override func update(currentTime: CFTimeInterval) {
print(test)
}
}

此代码将在控制台中导致“false”然后“true”。

最佳答案

每次调用 pressesBegan 时,您都在创建一个新的游戏场景,您需要执行以下操作

override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
if let skView = self.view as? SKView
{
skView.scene?.pressesBegan(presses, withEvent: event)
}
}

关于swift - tvOS pressesBegan() 未在 GameScene 中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35393949/

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