重置游戏时不会提示用户



我在app.js的开头添加了一个函数,其中我添加了一种函数promptUser,我希望每次当健康条小于零并且游戏重置时都调用它。

但在重置时,我没有得到提示-有什么想法或帮助吗?

let chosenMaxLife; let currentMonsterHealth;
let currentPlayerHealth;
let hasBonusLife = true;
function promptUser() {
const enteredValue = prompt('Maximum life of you and Monster is', '100');
chosenMaxLife = parseInt(enteredValue);
if (isNaN(enteredValue)) {
chosenMaxLife = 100;
}
currentMonsterHealth = chosenMaxLife;
currentPlayerHealth = chosenMaxLife;
adjustHealthBars(chosenMaxLife);
resetGameAgain();
}
attackBtn.addEventListener('click', attackHandler);

您需要在全局范围内声明变量。您在函数内部声明了它们,因此在函数外部无法访问它们。

const Attack_Value = 10;
const monster_attack_value = 12;
const strong_attack_value = 28;
const player_healValue = 20;
let currentMonsterHealth;
let currentPlayerHealth;
function promptUser() {
const enteredValue = prompt('Maximum life of you and Monster is', '100');
let chosenMaxLife = parseInt(enteredValue);

if (isNaN(enteredValue)) {
chosenMaxLife = 100;
}
currentMonsterHealth = chosenMaxLife;
currentPlayerHealth = chosenMaxLife;
let hasBonusLife = true;    //You need to declare it in global scope as well, depending on usage. 
}

if (currentMonsterHealth <= 0 || currentPlayerHealth <= 0) {
reset();
promptUser();
}
}

确实解决了这个问题。基本上,在我学习的时候,我有一个函数,但我从来没有在任何地方叫过它。

最新更新