How can I repeat an action every 30 seconds in SpriteKit?

72 Views Asked by At

In my SpriteKit project I would like to toggle a variable for a second and then untoggle it, every 30 seconds:

This is how I handle the timer:

override func didMove(to view: SKView) {
     DispatchQueue.main.asyncAfter(deadline: .now(), execute: {
            self.myTimer()
     })
 }

func myTimer() {
        
        run(.wait(forDuration: 1), completion: {
            self.timer += 1
            
            //This is how I use them after 30 seconds
            self.timer > 29 ? self.goldBottle = true : nil
            self.timer > 30 ? self.goldBottle = false : nil

            self.myTimer()
        })
    }

What can I do? Any suggestion is appreciated

0

There are 0 best solutions below