我正在使用教程为学校制作一款小型游戏,我的变量没有被识别,我无法弄清它是什么。在getEnemy.innerHTML中找不到任何变量。玩家可以工作,但敌人不会。非常感谢,对我轻松一点!
setPreFight: function() {
let getHeader = document.querySelector(".header");
let getActions = document.querySelector(".actions");
let getArena = document.querySelector(".arena");
getHeader.innerHTML = '<p>Task: Find an enemy!</p>';
getActions.innerHTML= '<a href= "#" class="btn-prefight" onclick="GameManager.setFight()">Search for Enemy! </a>';
getArena.style.visibility = "visible";
},
setFight: function() {
let getHeader = document.querySelector(".header");
let getActions = document.querySelector(".actions");
let getEnemy = document.querySelector(".enemy");
//create enemy
let enemy00 = new Enemy("Goblin, 100, 0, 50, 100, 100");
let enemy01 = new Enemy("Troll, 200, 0, 150, 80, 150");
let chooseRandomEnemy = Math.floor(Math.random() * Math.floor(2));
switch (chooseRandomEnemy) {
case 0:
enemy = enemy00;
break;
case 1:
enemy = enemy01;
break;
}
getHeader.innerHTML = '<p>Task: Choose your move </p>';
getActions.innerHTML = '<a href= "#" class="btn-prefight" onclick="PlayerMoves.calcAttack()">Attack! </a>';
getEnemy.innerHTML = '<img src="img/avatar-enemies/ ' + enemy.enemyType + '.png" alt="' + enemy.enemyType + '"class="img-avatar"><div><h3>' + enemy.enemyType + '</h3> <p class=="health-enemy">Health: ' + enemy.health + '</p><p>Mana ' + enemy.mana + '</p><p>Strength: ' + enemy.strength + '</p> <p>Agility: ' + enemy.agility + '</p><p> Speed: ' + enemy.speed + '</p></div>';
}
}
我希望有人能帮助我找到为什么显示不确定的原因。我认为它就在我眼前,但根本找不到!
答案 0 :(得分:1)
尝试初始化敌人变量,如下所示:
//create enemy
let enemy;
let enemy00 = new Enemy("Goblin, 100, 0, 50, 100, 100");
let enemy01 = new Enemy("Troll, 200, 0, 150, 80, 150");