在我的GameScene中,当我使用'touchesBegan()更新变量时,它工作正常,但是当我将'pressBegan()'从GameViewController传递给GameScene时,代码会兴奋,但更改会被忽略。
GameViewController:
class GameViewController: UIViewController {
...
override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
let scene = GameScene(fileNamed: "GameScene")
scene?.pressesBegan(presses, withEvent: event)
}
GameScene:
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”。
答案 0 :(得分:0)
每次你打电话给pressBegan,你都要创建一个新的Gamescene,你需要做
override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
if let skView = self.view as? SKView
{
skView.scene?.pressesBegan(presses, withEvent: event)
}
}