Сообщение от yaparoff
|
К нему надо подобраться по другому. Как?
|
Например, так
Animal.prototype.moveAnimal = function(i) {
var stepTop, stepLeft, unitTop, unitLeft, noFree, unitPos = {};
// for (var i = 0; i < animals.length; i++) {
noFree = true;
do {
stepTop = Math.round(Math.random() * 2 - 1) * 20;
stepLeft = Math.round(Math.random() * 2 - 1) * 20;
unitTop = +animals[i].top.replace(/\D/g,"") + stepTop;
unitLeft = +animals[i].left.replace(/\D/g,"") + stepLeft;
if (unitTop < 0 || unitLeft < 0 || unitTop > 380 || unitLeft > 380) {
noFree = true;
} else {
unitTop = unitTop + 'px';
unitLeft = unitLeft + 'px';
noFree = false;
for(var j = 0; j< units.length; j++) {
if(units[j].top == unitTop && units[j].left == unitLeft) {
noFree = true;
}
}
}
} while (noFree);
units[i].top = unitTop;
units[i].left = unitLeft;
animals[i].top = unitTop;
animals[i].left = unitLeft;
var elem = document.querySelector('#a' + i);
elem.style.top = unitTop;
elem.style.left = unitLeft;
// }
}
setInterval(function() {
newAnimals.forEach(function(animal, index) {
console.log(animal);
animal.moveAnimal(index);
});
}, 1000);