首页 > 解决方案 > 如何暂停某个代码并运行另一个代码直到需要

问题描述

我正在尝试从头开始制作一个口袋妖怪游戏,直到每次单击我的攻击选项时,它都会运行第一个 if 语句,然后运行下一个 if 语句替换第一张图片,然后让我有机会运行下次他们是我可以运行第一个 if 语句而不与第二个 if 语句冲突的方式吗?我发布了我的代码的链接

if (humanchoice === "effective") {
  let BOT = document.createElement("img")
  BOT.src = 'https://lh3.googleusercontent.com/Jys5bTaWg_ZRBr9GwE1CJ-4XzNWOsgp64aX0g1yXJ5UaMBE0vuuLO-OcIDDN7LNfKbH2ovFhwkyb2cKuiyhbKYUtuRDy-OOxNWVz6pEMIgRMim-LF0HHg-gMj1x-n8fJMAqaf8Y_=w246-h41-p-k'
  BOT.className = 'overlappedEnemyHealth'
  document.body.appendChild(BOT)
  console.log(BOT)
  if (BOT.src === 'https://lh3.googleusercontent.com/Jys5bTaWg_ZRBr9GwE1CJ-4XzNWOsgp64aX0g1yXJ5UaMBE0vuuLO-OcIDDN7LNfKbH2ovFhwkyb2cKuiyhbKYUtuRDy-OOxNWVz6pEMIgRMim-LF0HHg-gMj1x-n8fJMAqaf8Y_=w246-h41-p-k' && humanchoice === "effective") {
    BOT.src = "https://lh3.googleusercontent.com/Yy8PPjNdJqTl8cmGTD81PzPBt3arn4358tGjNOR4iEkpe9s6c5beV7UBN-QPeSNypgMt2-nJKGwn0T46g4HkJtwx9e4lCk_fV8LPNq0OR0k7CmE5Wg_6eq0-I56M4zAjZp1-Vk86=w246-h41-p-k"
    console.log(BOT)
  }
} else if (botchoice === "super effective") {} 
  else if (humanchoice === "not effective") {}

console.log(humanchoice)
console.log(botchoice)
}


function fightch() {
}

function fightchance() {
  return Math.floor(Math.random() * 3)
}

function fightchoiceYou(choice) {
  return ["effective", "super effective", "not effective"][choice]
}

function fightchanceBot() {
  return Math.floor(Math.random() * 3)
}

function fightchoiceBot(choice) {
  return ["effective", "super effective", "not effective"][choice]
}

https://codepen.io/jsmirga22/pen/ZEBEbNY

标签: javascripthtml

解决方案


推荐阅读