Reputation: 21
In SpriteKit is it better to use the loop or a timer to create a count down effect? I tried both and it worked but I'd like to know which is good practice?
Upvotes: 0
Views: 156
Reputation: 3995
you want to use an SKAction
:
let delay: TimeInterval = 2
let command: SKAction = .run {
print("timer is up!")
}
let wait: SKAction = .wait(forDuration: delay)
let sequence: SKAction = .sequence([wait, command])
run(sequence)
Using Timer
is not good because it runs outside of the SK loop and can cause crashes... you can use .update()
and make your own timers, but the SKAction is a much easier way of doing it.
You can actually do the above in 1 line:
run(.sequence([.wait(forDuration: 2), .run({print("timer done!")})])
Upvotes: 1