var enemies = [
{
"name" : "Green Slime",
"health" : 30,
"attacks" : [
{
"name" : "Bounce",
"damage" : 10,
"selfDamage" : 0
},
{
"name" : "Self Destruct",
"damage" : 30,
"selfDamage" : 30
}
]
},
{
"name" : "Blue Slime",
"health" : 60,
"attacks" : [
{
"name" : "Bounce",
"damage" : 20,
"selfDamage" : 0
},
{
"name" : "Shine",
"damage" : 30,
"selfDamage" : 0
},
{
"name" : "Self Destruct",
"damage" : 60,
"selfDamage" : 60
}
]
}
];
var enemyText = " Has decided to battle you!";
function $(input, type) {
if (type == "id") {
return document.getElementById(input);
}
else {
return document.getElementsByClassName(input);
}
}
function Fight() {
var a = Math.floor(Math.random() * enemies.length);
var enemy = enemies[parseInt(a)]
var enemyName = enemies[parseInt(a)].name;
$("FightText", "id").innerHTML = "A " + enemyName + enemyText;
while (enemy.health > 0) {
var b = Math.floor(Math.random() * enemy.attacks.length)
var attack = enemy.attacks[b]
$("FightText", "id").innerHTML += "<br>" + enemyName + " Used " + attack.name + "! <i>Dealt " + attack.damage + " damage</i>";
enemy.health -= attack.selfDamage;
}
}
因此,当我重复运行Fight
函数时,它有时不会显示敌人使用的攻击。
如同,它可能只显示A Blue Slime Has decided to battle you!
而不是实际说出它使用的攻击
有人知道如何解决这个问题吗?
请注意我倾向于向其他人提出明显的问题,所以,是的。这个可能很明显。
答案 0 :(得分:3)
您正在更新敌人模板,而不是新实例。
最终,他们都失去了健康,一旦发生这种情况,你就永远不会进入while (enemy.health > 0) {
。
更重要的是:在进一步编码之前,首先应该学习调试javascript代码。可以在任何浏览器和大多数开发环境中进行调试。例如:Debugging in Chrome