I am making a simple game where I need to generate an object 1: its position x and y needs to be in certain range 2: it must not overlap with some existing objects. I use the Math.random()
and the do while
loop for that, but the game intermittently crashes.
I cannot see any flaw in the logic, but why it crashes?
let posX, posY;
let invalid = false;
do {
posX = getRandomInt(0,100);
posY = getRandomInt(0,100);
if (posX > 20 &&
posX < 40 &&
posY > 20 &&
posY < 40
)
invalid = true;
}
while (invalid)
alert("not crash!")
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
Aucun commentaire:
Enregistrer un commentaire