我正在制作游戏,我需要扣除适量的积分。我的这个陈述不起作用(playerPower)部分。
function checkCol(cell, row){
var color = map[row][cell];
console.log(color);
if (color == "g") {
alert("Green");
playerPower -= onGrass - passengerBayPower
} else if (color == "b") {
alert("Brown");
playerPower -= onRock - passengerBayPower
} else if (color == "w") {
alert("White");
playerPower -= onIce - passengerBayPower
}
- =然后 - ?
是否有问题答案 0 :(得分:1)
您可以通过以下方式简化:
var color = map[row][cell];
var diff = 0;
console.log(color);
if (color == "g") {
alert("Green");
diff = onGrass - passengerBayPower
} else if (color == "b") {
alert("Brown");
diff = onRock - passengerBayPower
} else if (color == "w") {
alert("White");
diff = onIce - passengerBayPower
}
playerPower -= diff;
答案 1 :(得分:0)
让我们给你的变量赋一些值。假设playerPower
为10,onGrass
为2,passengerBayPower
为4.您可能希望答案为10 - 2 - 4 = 4
,对吧?错...
playerPower -= onGrass - passengerBayPower
对应于......
playerPower = playerPower - (onGrass - passengerBayPower)
= playerPower - onGrass + passengerBayPower
// ^^^ using basic mathematics
这意味着您获得10 - 2
+
4 = 12
要修复,请使用onGrass + passengerBayPower
,或完全删除passengerBayPower
并将其扔到最后:
playerPower -= passengerBayPower;
因为这对所有情况都很常见。