做游戏,发现按home键,等一下,再回到游戏,方法
-(void)update:(NSTimeInterval)currentTime
{
if (lastUpdateTime) {
dt = currentTime - lastUpdateTime;
} else {
dt = 0;
}
lastUpdateTime = currentTime;
}
即使游戏在后台也能继续运行。这不好,因为我使用这种方法来计算自游戏开始跟踪分数以来已经过去的秒数,如果它在应用程序处于后台时运行,当你回来时,你的分数比你离开时高.我创建节点的所有其他方法都会停止,但这个不会。当应用程序进入后台时,如何让它暂停。
最佳答案
因此您的游戏已暂停.. 但 dt
会查看一帧与另一帧之间的时间差异。所以在我的场景中,我创建了一个名为 catchUp
的 bool 属性。当它设置为 true 时,我会将 dt
设置为 0。
这是一些代码
override func update(currentTime: NSTimeInterval) {
if self.last_update_time == 0.0 || self.catchUp {
self.delta = 0
} else {
self.delta = currentTime - self.last_update_time
}
self.last_update_time = currentTime
if self.catchUp { // now we start getting delta time again
self.catchUp = false
}
当我重新开始游戏时:
func resumeGame(sender: UIButton!){
gameScene.catchUp = true
self.skView.paused = false
希望对您有所帮助:)
关于objective-c - SpriteKit如何在进入后台时完全暂停应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27559784/