Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

63
Vistas
setInterval inside for loops

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;
                }
            }
        }
    }

7 months ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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
}
7 months ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos