Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

126
Vistas
How to correctly move objects at different speeds in the direction of the `Y` axis

Tried to make a script like this:

let intervals = [],
isClick = [],
isGameOver = false,
countElement = 3,
count = 0,
gameOver = function () {
  if (isGameOver) {
     return;
  }
  isGameOver = true;
  if (countElement <= count) {
     for (var i = 0; i < intervals.legth; ++i) {
        clearInterval(intervals[i]);
     }
     intervals = [];
     countElement = 0;
  }
},
elm = function (index) {
   return function () {
     if (isGameOver || isClick[index]) {
         return null;
     }
     isClick[index] = true;
     clearInterval(intervals[index]);
     intervals[index] = null;
     if (!intervals.filter(a => a).length) {
        count = countElement;
        gameOver();
        return;
     }
   };
};
for (let i = 0; i < 17; ++i) {
    setTimeout(() => {
       element.on('pointerup', elm(i));
       intervals[i] = setInterval(() => {
          if (countElement <= count) {
              clearInterval(intervals[i]);
              gameOver();
              return;
          }
          if (-64 > element.position.y) {
              clearInterval(intervals[i]);
              intervals[i] = null;
              ++count;
          } else {
              element.position.y -= 30;
          }
      }, pos.speed);
    }, pos.startTime * i);
}

It actually works, but for some reason it doesn't always work as it should.

Perhaps I'll tell you right away what is required .. It is necessary to generate the required number of elements and move along the axis Y.

They must have different speeds.
I tried to solve it like this:

let rnd = function (min, max) {
    return Math.floor(Math.random() * (max - min) + min);
}, pos = {
    speed: Math.floor(rnd(100, rnd(370, 470))),
    startTime: Math.floor(rnd(rnd(370, 470), rnd(700, 1000)))
}

In general, I would like to see the elements start flying with different departure and flight speeds, there were attempts that can be missed if the element flew away, when you click on the element, it stops.

Well, in fact, if all the elements flew out - it doesn't matter if they flew away or stopped, the main thing is that all intervals should stop and there would be a kind of exit ...

Connoisseurs help out how this can be done without third-party libraries?

How to do it correctly, please show.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Problem solved... There was no need to zero out countElement and intervals in gameOver. Because of this, subsequent intervals were looped, as the indices were violated.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda