I am making a little game similar to tetris. I need the blocks to fall faster as the players score goes up I currently have this code:
setInterval(function(){
if(gameOn == 1){
gravity();
}
},1000);
This calls my gravity function and which makes the blocks drop down and does all the collision detection etc.
var time = 1000 - score;
setInterval(function(){
if(gameOn == 1){
hideStuff('hover');
gravity();
time = 1000 - score;
}
},time);
I thought something like this might work, but it doesn't seem to. Any ideas? Thanks
You could do a setTimeout loop. That way, every call to setTimeout will reflect any changes done to the value of time
var time = 1000;
(function foo(){
if(gameOn == 1){
hideStuff('hover');
gravity();
time = 1000 - score;
}
setTimeout(foo,time);
}());
You'll need to clear the interval and re-start it whenever you want to change the delay:
window.icount = setInterval(function(){
if(gameOn == 1){
gravity();
}
},1000);
var time = 1000 - score;
clearInterval(window.icount);
window.icount = setInterval(function(){
if(gameOn == 1){
hideStuff('hover');
gravity();
time = 1000 - score;
}
},time);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With