Reputation: 21
I am trying to speed up an object in JS game x2 every 10 points. In the game at the same time I use
let game = setInterval(draw,100);
Which technique should I try? I have been searching for a really long time yet trying to do it with changing the setInterval didn't work as it should (it accelerated over and over).
Would be really grateful for any advice (not looking for a ready code, just saying!).
Upvotes: 0
Views: 249
Reputation: 92440
You just need to hang onto the game
value returned from setInterval
. You can use that to stop the current interval timer with clearInterval()
and then start a new one with your new rate. Since you didn't post much code here's a contrived example that counts to 100 speeding up every 10 numbers:
let i = 0
let speed = 1
let basespeed = 1000
function play() {
console.log("play", i++)
if (i % 10 == 0) {
speed *= 2
clearInterval(int)
if (i >= 100) return
int = setInterval(play,basespeed/speed)
}
}
let int = setInterval(play, basespeed)
Upvotes: 1