Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!