按下开始按钮时的重置时间

问题描述 投票:0回答:2

我有一个点击游戏的应用/游戏。您必须点按电池一定次数才能获胜。我想在游戏中添加一个计时器。

var lives = 3;
var score = 0;
onEvent("start_button", "click", function() {
  setScreen("game_screen");
  lives = 3;
  score = 0;
  setText("number_lives", lives);
  setText("total_score", score);
  timedLoop(1000, function() {
    setPosition("half", randomNumber(50,280), randomNumber(50,350));
  });
  timedLoop(3000, function() {
    setScreen("timesUp");
  });
});
onEvent("half", "click", function() {
  setPosition("half", randomNumber(50,280), randomNumber(50, 350));
  if (score == 9) {
    setScreen("win_screen");
  }
  score = score + 1;
  setText("total_score", score);
});
onEvent("background", "click", function() {
  if (lives == 1) {
    setScreen("lose_screen");
  }
  lives = lives - 1;
  setText("number_lives", lives);
});
onEvent("playAgain_button", "click", function() {
  setScreen("welcome_screen");
});
onEvent("tryAgain_button", "click", function() {
  setScreen("welcome_screen");
});
onEvent("userDecisionButton", "click", function() {
  if (getText("userDecision")=="Yes"|| "yes" ) {
    setScreen("game_screen");
    lives = 3;
    score = 0;
    setText("number_lives", lives);
    setText("total_score", score);
    setTimeout(function() {
      setScreen("timesUp");
    }, 3000);
  }
  if (getText("userDecision")=="No" || "no") {
    setScreen("welcome_screen");
    stopTimedLoop();
  }
});

发生的情况是,当您键入是或否(再次播放)时,计时器每3秒循环运行一次。点击链接以查看我的代码正在运行:https://studio.code.org/projects/applab/5SIn_7HAsJUjYU_Z6qtoy2sloUfQS7cQV8oaBl4rQkg任何人都可以帮助甚至尝试吗?请吗?

javascript time reset timed
2个回答
0
投票

[编辑]

您可能打算这样做:

getText("userDecision")=="Yes"|| "yes"

测试“是”或“是”,但是您的语法有缺陷。在逻辑上等效于:

(getText("userDecision")=="Yes") || "yes"

始终是真实的。

您应该改用它:

"yes" === getText("userDecision").toLowerCase()

getText("userDecision")=="No" || "no"您有类似的问题。


0
投票

尝试在按下开始按钮时使计时器开始计时,并在失败时使计时器停止

© www.soinside.com 2019 - 2024. All rights reserved.