我对这种令人难以置信的语言有点陌生,我正在尝试创建一个函数,让我可以慢慢揭示战斗的进展情况。我可以提前编写我的函数,然后在 setTimeout 中声明该函数而无需重写它,因为这似乎永远不会起作用。这是我制作的不太好的代码:
var health=100;
var ehealth=100;
var atk;
var eatk;
function attack(x){
x=Math.floor(Math.random()*11);
atk=x;
ehealth=ehealth-atk
document.write('Enemy Health:' + ' ' + ehealth + ' ')
}
function eattack(x){
x=Math.floor(Math.random()*11);
eatk=x;
health=health-eatk
document.write('Health:' + ' ' + health )
}
function dead(){
if(health<=0){
document.write('You Lose');
}else{
if(ehealth<=0){
document.write('You Win');
}
}
}
function battle(){
document.write('Enemy Health:' + ' ' + ehealth + ' Health: ' + health + '<br/>\n')
while(health>=0&&ehealth>=0){
setTimeout(attack(0),400)
setTimeout(eattack(0),400)
document.write("<br/>\n");
dead();
}
}
帮助!