I want to increment a counter variable roughly every 2 seconds in SKScene update (the game loop) I could use a timer variable and increment it using delta time.
self.incTimer = self.incTimer + self.deltaTime
if self.incTimer >= 2.0 {
self.counter++
self.incTimer = 0.0
}
I was curious if there was any other way, I looked at running multiple SKActions with waitForDuration but (as I already knew) they all run concurrently. Is there a better/another way I might be missing?
You can use the repeatActionForever with sequence actions to make a repeating timer that runs a block every 2 seconds:
SKAction.repeatActionForever(
SKAction.sequence([SKAction.waitForDuration(2),
SKAction.runBlock({self.counter++})])
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With