Learn iOS SpriteKit with Examples, Code Snippets and Video Tutorials

How to Make a Periodic Timer in Sprite Kit and Swift

How to Make a Periodic Timer in Sprite Kit and Swift

Below is all the Sprite Kit code you need to start and restart a timer periodically. If you were to build this in Xcode, the Output Window would show you the total seconds the timer has been running, every second.

Notice the forDuration parameter in the wait SKAction is set to 1 second. This could be any number you want obviously. If you wanted to do this roughly at the frame rate of the movie, set it to 1/60 .

Also keep in mind, that inside the run block of code, any variables or functions declared in the class will need self. in front of them. Which is why we write self.totalSeconds

Check out our premium Swift and Sprite Kit Tutorial content!

Visit CartoonSmart.com