UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Project 17 Challenge 2 How to reduce the timer by intervals of 0.1 seconds?

Forums > 100 Days of Swift

@AE542  

Hey guys I've been working through the SpriteKit project for this project but the second challenge is quite tough. I understand that you would have to call gameTimer.invalidate() to prevent multiple timers from running, but I guess its really hard to tell if the timer is working or not as the game is so fast. I'm also a little confused about where to exactly write the timer...long story short, what would be the best way for the timer to reduce for every 20 enemies?

newTimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
for enemies in self.possibleEnemies {
self.runTimer -= 0.1
print(self.newTimer as Any)
if enemies.count == 20 {

self.newTimer?.invalidate()

}
} //didn't seem to do anything.

Apologies if my code is messy. I tried to put this in createEnemy function as I thought it would run from there but, where would be the correct place to put the code? Would I need to create a new func and call it from inside the createEnemy func or somewhere else?

Thanks for your time guys.

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.