Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

128
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda