使代码重复性降低

时间:2015-01-29 22:15:15

标签: javascript simplify

我在一个关于角色移动的游戏中写了一部分代码,这段代码非常重复(4个方向几乎都是一样的)。

我无法看到如何让它变得更简单,因为在所有'if'条件下进行了测试,因为在另一种情况下我会创建一个带有正/负变量的系统,我可以将它增加到该位置变化。

感谢您的帮助!

for (var i = 0; i < boxArray.length; i++) {
    var box = boxArray[i];

    if (controller.left && currentLevel[this.id - this.moveHorizontal] != 1) {
        // Enregistrement de la position dans le tableau d'undo/redo
        this.index++;
        var array = [this.x, this.y, this.id];
        this.undoArray.push(array);
        // Si box à côté et pas de collision possible 
        if (box.id == this.id - this.moveHorizontal && currentLevel[this.id - this.moveHorizontal * 2] != 1 && currentLevel[this.id - this.moveHorizontal * 2] != 2) {
            // Décalage de la position du player 
            this.x -= this.boxWidth;
            this.id -= this.moveHorizontal;
            currentLevel[this.id] = 8;
            currentLevel[this.id + this.moveHorizontal] = 0;
            // Décalage de la position de la box
            box.x -= this.boxWidth; 
            box.id -= this.moveHorizontal;
            currentLevel[this.id - this.moveHorizontal] = 2;
            controller.left = false;
        }
        // Sinon si aucun objet à côté  
        else if (currentLevel[this.id - this.moveHorizontal] == 0 || currentLevel[this.id - this.moveHorizontal] == 3) {
            // Décalage de la position du player
            this.x -= this.boxWidth;
            this.id -= this.moveHorizontal;
            currentLevel[this.id] = 8;
            // Décalage de la position du sol
            currentLevel[this.id + this.moveHorizontal] = 0;
            controller.left = false;
        }
    }
    else if (controller.right && currentLevel[this.id + this.moveHorizontal] != 1) {
        this.index++;
        var array = [this.x, this.y, this.id];
        this.undoArray.push(array);
        if (box.id == this.id + this.moveHorizontal && currentLevel[this.id + this.moveHorizontal * 2] != 1 && currentLevel[this.id + this.moveHorizontal * 2] != 2) {
            this.x += this.boxWidth;
            this.id += this.moveHorizontal;
            currentLevel[this.id] = 8;
            currentLevel[this.id - this.moveHorizontal] = 0;
            box.x += this.boxWidth; 
            box.id += this.moveHorizontal;
            currentLevel[this.id + this.moveHorizontal] = 2;
            controller.right = false;
        }
        else if (currentLevel[this.id + this.moveHorizontal] == 0 || currentLevel[this.id + this.moveHorizontal] == 3) {
            this.x += this.boxWidth;
            this.id += this.moveHorizontal;
            currentLevel[this.id] = 8;
            currentLevel[this.id - this.moveHorizontal] = 0;
            controller.right = false;
        }
    }

2 个答案:

答案 0 :(得分:0)

沿着这些方向的东西

for (var i = 0; i < boxArray.length; i++) {
    var box = boxArray[i];

    var direction = {
        'left': -1,
        'right': 1
    }; 

    for (var d in direction) {
        var mh = this.moveHorizontal * direction[d];

        if (controller[d] && currentLevel[this.id + mh])] {
            this.index++;
            var array = [this.x, this.y, this.id];
            this.undoArray.push(array);

            if (box.id == this.id + mh && currentLevel[this.id + mh * 2] != 1 && currentLevel[this.id + mh * 2] != 2) {
                this.x += this.boxWidth * direction[d];
                this.id += mh; 
                currentLevel[this.id] = 8;
                currentLevel[this.id + this.moveHorizontal] = 0;
                // Décalage de la position de la box
                box.x += this.boxWidth * direction[d]; 
                box.id += mh; 
                currentLevel[mh] = 2;
                controller[d] = false;
            } else if (currentLevel[this.id + mh] == 0 || currentLevel[this.id + mh] == 3) {
                // Décalage de la position du player
                this.x += this.boxWidth * direction[d];
                this.id += mh; 
                currentLevel[this.id] = 8;
                // Décalage de la position du sol
                currentLevel[this.id + (mh * -1)] = 0;
                controller[d] = false;
            }
        }
    }
} 

答案 1 :(得分:0)

我认为你绝对可以简化这段代码。 例如,创建一个对象:

var moves = {
    moveLeft: function() { ... },
    moveRight: function() { ... }
};

然后你可以这样说

moves[side](...);

其中side是表示moveLeft或moveRight的字符串。

除此之外,您可以对重复的功能进行分组,例如左侧和右侧重复的功能:

function initMovement() {
   var array = [this.x, this.y, this.id];
   this.index++;
   this.undoArray.push(array);
}