swift - SpriteKit View 不会更新以响应计时器事件

标签 swift events swift3 timer sprite-kit

在我们的程序中,有一个基于计时器的变量,每秒一次。我们的 View 更新函数会在计时器计时时运行,或者在用户按下某个键以在应用程序中引起 Action 时运行。通过 print 语句我们可以看到该变量实际上每秒更改一次,但更新只会在用户按下某个键时传播到屏幕上可见的 SKLabelNode。在没有物理事件的情况下,是否需要调用一些特殊的“刷新”函数来将更新推送到屏幕?

func startGame() {
        ...
        let timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timerUpdate), userInfo: nil, repeats: true)
}

func timerUpdate(){
        print("Before:\(mouseLife)")
        mouseLife -= 1
        displayNumbers2()
        print(mouseLife)
}

func displayNumbers2() {
        ...
        mouseLifeLabel.text = String(mouseLife)
        ...
}

最佳答案

我现在还没有编译器,也许有些功能有点不同......

var mouseLifeLabel: Int = 5 {
    didSet {
        mouseLifeLabel.text = String(mouseLife)
        if mouseLifeLabel <= 0 {
              //Gameover func
        }
    }
}

func startGame() {
        ...
        let action = SKAction.block({[unowned self] in
             self.mouseLife -= 1
        })
        let wait = SKAction.wait(1)
        let seq = SKAction.sequance([action, wait])
        let forever = SKAction.repeatForever(seq )
        run(forever)
}

关于swift - SpriteKit View 不会更新以响应计时器事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44771519/

相关文章:

swift - 创建一个带有子模块的 Swift 框架

ios - 事件指示器未出现

objective-c - 带 iOS 的 Linkedin SDK - 身份验证失败

ios - 输入时在 Swift 中的文本字段中格式化货币

excel - 单击单元格时的操作

javascript - “this”在其他事件中无法正常工作。我不知道为什么

qt - 如何在Qt中发出一定间隔的信号?

ios - 如何从 NSDictionary 转换为 Dictionary ? swift 3

ios - Swift 以编程方式在 UITableViewCell 中添加 UIViewController 或 UIView

ios - Swift 3 - 如何验证对象的类类型