Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incrementing counter in update loop?

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?

like image 228
fuzzygoat Avatar asked Aug 16 '26 12:08

fuzzygoat


1 Answers

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++})])
                            )

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!