I'm tyring to put setInterval inside for loops but it's not working. monArray and towers are arrays of objects. Detection() is collision detection function. I want to set the setInterval time as one of the towers values. What can I do?
function Attack()
{
for(t = 0; t < towers.length; t++)
{
for(m = 0; m < monArray.length; m++)
{
if(Detection(towers[t], monArray[m]))
{
console.log("attack")
monArray[m].hp -= towers[t].atk //I want to setInterval this part
if(monArray[m].hp <= 0)
{
clearInterval(int)
}
break;
}
}
}
}
here is my attempt, this should work
let detect = (t, m) => { return Detection(towers[t], monArray[m]);}
let attack = (t, m) => { monArray[m].hp -= towers[t].atk; };
let islessthanorzero = (m) => { return monArray[m].hp <= 0; };
let callAttack = null;
function Attack() {
callAttack = setInterval(()=>{
attackLoop:
for(let t = 0; t < towers.length; t++){
for(let m = 0; m < monArray.length; m++) {
if(detect(t, m)){
attack(t, m);
if(islessthanorzero(m)){
if(callAttack != null) clearInterval(callAttack);
break attackLoop;
}
}
}
}
}, 1000); // called every 1000 milliseconds, 1000ms = 1 second
}