我很好奇是否有人能够找到一种方法来保持场景的活跃时间。当我需要触发某些事件时,这很重要,例如说“游戏开始 5 分钟后生成飞船”。
更新周期的问题是,当前耗时不仅仅是事件游戏时间,因此如果您接到电话或切换应用程序,返回时您会及时得到 X 分钟的跳跃时间。
目前,我使用 SKAction.customAction
来跟踪我的时间,它可以工作,但我不能保证 Action 触发的顺序,所以我的事件最终可能会延迟 1 帧.
这是我现在正在做的事情,请告诉我您为保持时间一致做了什么。
//注意:NodeComponent是一个协议(protocol)扩展,用于快速访问GKSKNodeComponent的节点
import GameplayKit
class ElapsedTimeComponent:GKComponent,NodeComponent
{
var elapsedTime : TimeInterval = 0.0
override func didAddToEntity() {
node.scene?.addComponentToComponentSystem(self)
self.node.run(SKAction.customAction(withDuration:330000000000000000000000000000000000000){node,seconds in
elapsedTime = seconds
(node as! SKLabelNode).text = "\(seconds)"
})
}
override func update(deltaTime seconds: TimeInterval) {
}
}
最佳答案
事实证明,SKView 有一个可以附加到任何地方的委托(delegate)协议(protocol)。我已将我的最新测试设置为如下所示:
public class GameScene: SKScene,SKViewDelegate {
var previousTime : TimeInterval = 0
var gameTime : TimeInterval = 0
public func view(_ view: SKView, shouldRenderAtTime time: TimeInterval) -> Bool
{
if !self.isPaused{
gameTime += time - previousTime
}
print("GameTime: \(gameTime)")
componentSystems.forEach({$0.update(deltaTime: gameTime)})
self.previousTime = time
return !self.isPaused
}
public override func didMove(to view: SKView) {
view.delegate = self
}
}
https://developer.apple.com/documentation/spritekit/skviewdelegate
如果我理解正确,那么除此之外我唯一需要注意的是确保从游戏返回时,view.isPaused 不会在同一时间取消暂停我的场景(呃为什么你对我这样做 Apple) 以允许发生 1 个渲染循环。
当然,这种方法的缺点是我创建的任何暂停屏幕都必须在这个场景之外,因为渲染被禁用。
关于ios - 使用更新当前时间来跟踪游戏时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57574341/