In my tank game (not unlike awesome tanks for reference) I have created a timer for my bullet but when run it has a runtime error saying that AItimer is not defined. Im confused since i have another timer which in the same program but does not have this error.
function aiStartTimer() { if(shoot == 0) { //creates aitimer variable AItimer = setInterval("aiFireBullet()",100); shoot = 1*1; } else if(shoot == 1) { clearInterval(AItimer); shoot = 0 * 1; } } function StartTimer() { if(onOff == 0) { //creates timer variable timer = setInterval("FireBullet()",100); onOff = 1*1; } else if(onOff == 1) { clearInterval(timer); onOff = 0 * 1; }
}
here is a fiddile https://jsfiddle.net/tm9oL74r/
You have to pass the function definition in argument of setTimeout, not a string. Replace AItimer = setInterval("aiFireBullet()", 100); by AItimer = setInterval(aiFireBullet, 100); and it should work.
setTimeout
AItimer = setInterval("aiFireBullet()", 100);
AItimer = setInterval(aiFireBullet, 100);
2.1m questions
2.1m answers
60 comments
57.0k users