Answer the question
In order to leave comments, you need to log in
How to track if else condition asynchronously in JS?
There is a condition that checks the coordinates of the hero with the coordinates of other objects (collision conditions, etc.)
When the hero moves, his coordinates change, I check changes through setInterval which runs this check every 50ms, but it has its own disadvantages such as CPU load (with a heavy load of functions) so and their jambs in working out the conditions.
if( heroObj.position.left+heroObj.position.right>=rockObjs.position.left && //Слево
heroObj.position.top+heroObj.position.bottom<=200 &&
heroObj.position.top+heroObj.position.bottom<=280 /* &&
heroObj.position.left<=rockObjs.position.right*/){
heroObj.position.top=120; console.log("moree") ;
}else{heroObj.position.top=250; console.log("down");};
Answer the question
In order to leave comments, you need to log in
Instead of setInterval, try using requestAnimationFrame() - the callback will be called frequently, but in it, check the time from the previous "working" operation so that more than 50ms have elapsed.
The condition checks you provided shouldn't be any noticeable to the CPU, unless you're running them for a million pairs of objects each time.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question