Estoy tratando de poner setInterval dentro de bucles pero no funciona. monArray y towers son matrices de objetos. Detection() es una función de detección de colisiones. Quiero establecer el tiempo setInterval como uno de los valores de las torres. ¿Que puedo hacer?
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; } } } }
aquí está mi intento, esto debería funcionar
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 }