objective-c - SpriteKit如何在进入后台时完全暂停应用程序

标签 objective-c sprite-kit

做游戏,发现按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/

相关文章:

sprite-kit - SKShader 创建视差背景

objective-c - UI 测试中不完整的通用字符名称

Swift SKSpriteNode绘制顺序

xcode - 碰撞时将节点粘到另一个节点

ios - 为什么我的 uitableview 单元格行重复字幕

ios - 代码中的 SKTileSet 平铺变体访问

string - SKLabel.text 整数不更新 - swift

ios - 如何更改 UITabBar 选择颜色

objective-c - 数组复制使应用程序崩溃

ios - 在 ScrollView 中显示从相机拍摄的图像