Я встретил некоторые проблемы с циклом for в моем коде. Он должен разбирать массив, содержащий 3 объекта, и перемещать их, если условия правильные. Но обнаружен и перемещен только объект , остальные два не обнаружены. Я не понимаю, почему, потому что я сделал другие подобные методы, и у меня не было бы проблем.Для цикла не работает правильно при анализе массива
Это метод, и я знаю, что это трудно понять, но проблема заключается в том, что обнаруженный объект «ящик» всегда является первым из массива и никогда не является вторым или третьим.
Player.prototype.move = function(direction) {
for (var i = 0; i < boxArray.length; i++) {
var box = boxArray[i];
// Enregistrement de la position dans le tableau d'undo/redo
this.index++;
var array = [this.x, this.y, this.id];
this.undoArray.push(array);
// Définition de la direction de déplacement
if (direction == 'RIGHT') {
this.moveCoeff = 1;
this.moveLength = 1;
}
else if (direction == 'LEFT') {
this.moveCoeff = -1;
this.moveLength = 1;
}
else if (direction == 'TOP') {
this.moveCoeff = -1;
this.moveLength = 11;
}
else if (direction == 'BOTTOM') {
this.moveCoeff = 1;
this.moveLength = 11;
}
// Si box à côté et pas de collision possible
if ((controller.left || controller.right || controller.top || controller.bottom) &&
box.id == this.id + (this.moveLength * this.moveCoeff) &&
currentLevel[this.id + (this.moveLength * 2 * this.moveCoeff)] != 1 &&
currentLevel[this.id + (this.moveLength * 2 * this.moveCoeff)] != 2) {
// Décalage de la position du player
if (direction == 'RIGHT' || direction == 'LEFT') this.x += this.boxWidth * this.moveCoeff;
else this.y += this.boxWidth * this.moveCoeff;
this.id += this.moveLength * this.moveCoeff;
currentLevel[this.id] = 8;
currentLevel[this.id - (this.moveLength * this.moveCoeff)] = 0;
// Décalage de la position de la box
if (direction == 'RIGHT' || direction == 'LEFT') box.x += this.boxWidth * this.moveCoeff;
else box.y += this.boxWidth * this.moveCoeff;
box.id += this.moveLength * this.moveCoeff;
currentLevel[this.id + (this.moveLength * this.moveCoeff)] = 2;
}
// Sinon si aucun objet à côté
else if ((controller.left || controller.right || controller.top || controller.bottom) &&
(currentLevel[this.id + (this.moveLength * this.moveCoeff)] == 0 ||
currentLevel[this.id + (this.moveLength * this.moveCoeff)] == 3)) {
// Décalage de la position du player
if (direction == 'RIGHT' || direction == 'LEFT') this.x += this.boxWidth * this.moveCoeff;
else this.y += this.boxWidth * this.moveCoeff;
this.id += this.moveLength * this.moveCoeff;
currentLevel[this.id] = 8;
// Décalage de la position du sol
currentLevel[this.id - (this.moveLength * this.moveCoeff)] = 0;
}
// Fin du déplacement
controller.right = false;
controller.left = false;
controller.top = false;
controller.bottom = false;
}
}
Вот та часть, где я толкаю объекты в массиве (в цикле):
var box = new Box();
box.id = i;
boxArray.push(box);
Думаю, никто не пытался ответить на ваш вопрос, потому что слишком много строк. Постарайтесь сделать это крошечным. Сосредоточьтесь на проблеме. И попытайтесь воспроизвести проблему в [JSFiddle] (http://jsfiddle.net/) –
, как вы заполняете 'boxArray'? можете ли вы включить эту часть? –