I am curious if anybody has been able to figure out a way to keep how long a scene has been active for. This is critical for when I need to fire certain events, like say "spawn ship 5 minutes into game play".
The issue with the update cycle, is that the current time being passed is not active game time only, so if you get a call or switch apps, upon return you will get that X minute jump in time.
Currently, I use an SKAction.customAction
to keep track of my time, and it works, but I cannot guarantee the order in which actions fire, so my events could end up being 1 frame off.
Here is what I am doing now, please let me know what you have done to keep time consistent.
//Note: NodeComponent is a protocol extension to quickly access the GKSKNodeComponent's node
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) {
}
}