其他变量无缘无故变化 - javascript

时间:2015-05-07 17:48:44

标签: javascript arrays loops for-loop

我有一个函数,我遍历2个数组并做一些逻辑。我的问题是当我更改数组中的一个对象属性时,它们也会更改我的基础对象变量和整个两个数组中的所有其他对象。即使我只想编辑特定的对象属性。我不想粘贴整个代码,所以我会告诉你我的重要功能,但也留下了我整个代码的链接。

var updateMobs = function() {

    for (var b = 0; b < mobsBlue.length; b++) {
        BM = mobsBlue[b];
        BM.x = BM.x - BM.object.speed;

        doCollision(BM, redBase, BM);
        doCollision(BM, debugPlayer, BM);

        if (BM.x < 0){
            mobsBlue.splice(b, 1);
        };

        BM.Draw(ctx, false, true, "blue")


    };
    for (var r = 0; r < mobsRed.length; r++) {
        RM = mobsRed[r];
        RM.x = RM.x + RM.object.speed;

        doCollision(RM, blueBase, RM);
        doCollision(RM, debugPlayer, RM);

        if (RM.x > ctx.canvas.width){
            mobsRed.splice(r, 1);
        };

        RM.Draw(ctx, false, true, "red")    

        for (var br = 0; br < mobsBlue.length; br++) {
            BM = mobsBlue[br];

            if (doCollision(RM, BM, collisionNull) == true) { // ATTACKING
                BM.x = BM.x + BM.object.speed;
                RM.x = RM.x - RM.object.speed;

                if (BM.object.attackTime == BM.object.attackSpeed || RM.object.attackTime == RM.object.attackSpeed) {
                    if (BM.object.armourType == 'light') {
                        BM.object.health = BM.object.health - RM.object.lightDamage;
                    };
                    if (BM.object.armourType == 'heavy') {
                        BM.object.health = BM.object.health - RM.object.heavyDamage;
                    };
                    if (RM.object.armourType == 'light') {
                        RM.object.health = RM.object.health - BM.object.lightDamage;
                    };
                    if (RM.object.armourType == 'heavy') {
                        RM.object.health = RM.object.health - BM.object.heavyDamage;

                    };
                    alert("BLUE" + BM.object.health)
                    alert("RED" + RM.object.health)
                    if (BM.object.health <= 0) {
                        mobsBlue.splice(br, 1);
                    };
                    if (RM.object.health <= 0) {
                        mobsRed.splice(r, 1);
                    };
                    BM.object.attackTime = 0;
                    RM.object.attackTime = 0;

                };
                BM.object.attackTime = BM.object.attackTime + 1;
                RM.object.attackTime = RM.object.attackTime + 1;

            };                          
            BM.Draw(ctx, false, true, "blue");
            RM.Draw(ctx, false, true, "red");
        };
    };
};

http://pastebin.com/TPZE3Uqu

2 个答案:

答案 0 :(得分:0)

您的问题在于JavaScript的原型继承。

好好阅读https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain

您应该使用&#34; var&#34;来隔离您的范围。如:

var BM = mobsBlue[br];

答案 1 :(得分:0)

这样做:

function clone(obj) {
    if (null == obj || "object" != typeof obj) return obj;
    var copy = obj.constructor();
    for (var attr in obj) {
        if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
    }
    return copy;
}

OR 在ECMAScript 6中有Object.assign方法,它将所有可枚举的自有属性的值从一个对象复制到另一个对象。

例如:

var x = {myProp: "value"};
var y = Object.assign({}, x); //shallow copy

或者在jQuery中

var newObject = jQuery.extend({}, oldObject); // Shallow copy

var newObject = jQuery.extend(true, {}, oldObject); //深层复制