不会减去JavaScript对象属性

时间:2017-12-08 20:47:40

标签: javascript

我一直在尝试创建JavaScript对象,其中包含游戏中角色的不同属性,这些属性受到游戏当前回合中发生的事件的影响。当我从一个字符数组中依次访问当前字符并根据事件的效果将更改应用到他的属性时,我无法获取要减去的属性。即使属性是负数,它们也会不断增加,而不是减少角色的属性。如果有人能帮助我理解为什么会这样,我会非常感激!

这是角色对象的样子:

let russian = {
    happiness: 7,
    vitality: 7,
    satiation: 7,
    log: [],
    portrait: 'russian'
};

这是事件对象:

let playerInTurn = currentPlayer;
let event = {
    updateHappiness: -Math.abs(1),
    updateVitality: Math.abs(2),
    updateSatiation: -Math.abs(1),
    name: 'Cardio marathon',
    description: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
    updateAttributes: function() {
        if (this.updateHappiness > 0) {
            charactersArray[currentPlayer].happiness += this.updateHappiness;
        } else {
            charactersArray[currentPlayer].happiness -= this.updateHappiness;
        }
        if (this.updateVitality > 0) {
            charactersArray[currentPlayer].vitality += this.updateVitality;
        } else {
            charactersArray[currentPlayer].vitality -= this.updateVitality;
        }
        if (this.updateSatiation > 0) {
            charactersArray[currentPlayer].satiation += this.updateSatiation;
        } else {
            charactersArray[currentPlayer].satiation -= this.updateSatiation;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我没有注意到我正在减去负数,所以我被指示摆脱检查,只是将事件中的属性添加到角色,现在它按预期工作。谢谢Nina Scholz,Mark_M和Slai