简体   繁体   English

我正在用 javascript 制作一个简单的点击游戏,但其中一个功能似乎不起作用。 有人可以帮我解决吗?

[英]Im making a simple clicker game in javascript but one of the functions doesn't seem to be working. Can someone help me fix it?

I have this problem with a clicker game I have.我有一个点击游戏有这个问题。 So basically what I'm trying to make the game do is once the player has gotten 200 points, the player has the ability to unlock or unveil the new clickers.所以基本上我想让游戏做的是,一旦玩家获得 200 分,玩家就可以解锁或揭开新的点击器。 But for some reason even if you have 0 points you can unlock the new clickers.但出于某种原因,即使您有 0 分,您也可以解锁新的答题器。 Can someone help me with this?有人可以帮我弄这个吗? Thanks谢谢

Javascript Clicker code: Javascript Clicker 代码:

let totalclicks = 0;
let superclicker1 = document.getElementById("superclicker1")
let pointsEl = document.getElementById("points-el")

function clicker1() {
    totalclicks = totalclicks + 5;
    pointsEl.textContent = "Total Points: " + totalclicks
}

Javascript unlock new clicker code Javascript解锁新的点击器代码

function rebirth() {
    if (totalclicks >= 200) {

      } else if (superclicker1.style.display === "none") {
        superclicker1.style.display = "block";
      } else if (superclicker1.style.display = "none") {
  
    } else {

      alert("Need 200 points to win.")  
    }
}

Html for the clicker点击器的 Html

<p id="points-el">Total Points: </p>
<button onclick="clicker1()">Five Point Per Click </button>

Html for the function that unlocks the next clickers:用于解锁下一个点击器的函数的 Html:

<button id="superclicker1" onclick="superclicker10()">Rebirth </button>

This line is where the problem is这条线是问题所在

} else if (superclicker1.style.display = "none") {

Single = are used for assignment, not conditional checks. Single =用于赋值,而不是条件检查。 You probably want either == or === for strict type checking.您可能需要=====进行严格的类型检查。

Since you're using assignment = then that else if statement will always resolve to true .由于您使用的是 assignment =那么else if语句将始终解析为true So your logic looks like this:所以你的逻辑是这样的:

if (false) {

} else if (false) {

} else if (true) {
    // this block will always be triggered
} else {

}

Perhaps you were looking for something like:也许您正在寻找类似的东西:

if (totalclicks >= 200) {
  if (superclicker1.style.display === "none") {
    superclicker1.style.display = "block";
  } else {
    superclicker1.style.display = "none";
  }
} else {
  alert("Need 200 points to win.")  
}
  1. I suggest using more representative names for your element variables.我建议为您的元素变量使用更具代表性的名称。 It makes your code more readable.它使您的代码更具可读性。

  2. I think you want to call rebirth from the rebirth button.我想你想从重生按钮调用rebirth

  3. Your if statement should be nested.您的if语句应该嵌套。 "If the total is equal or greater than 200 then toggle the style based on nested condition, otherwise alert the message. “如果总数等于或大于 200,则根据嵌套条件切换样式,否则警告消息。

 let totalclicks = 0; const pointsEl = document.getElementById('points-el'); const rebirthEl = document.getElementById('rebirth'); function addFive() { totalclicks = totalclicks + 5; pointsEl.textContent = "Total Points: " + totalclicks; } function superclicker() { // Missing code } function rebirth() { if (totalclicks >= 200) { if (rebirthEl.style.display === 'none') { rebirthEl.style.display = 'block'; } else { rebirthEl.style.display = 'none'; } } else { console.log("Need 200 points to win.") } }
 <p id="points-el">Total Points: </p> <button onclick="addFive()">Five Point Per Click</button> <button id="rebirth" onclick="rebirth()">Rebirth</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我似乎无法弄清楚如何修复JavaScript中的两个棉绒。 有人可以帮我理解吗? - I can't seem to figure out how to fix two lints in my javascript. Can someone help me understand? 反应 onChange 不起作用。 有人能帮我吗? - React onChange is not working. Can someone help me? 我的切换导航栏不起作用。 有人能帮我吗? - My toggle navbar is not working. Can someone help me? 我正在制作一个非常简单的点击器游戏,我想在他们购买可购买的(按钮)后将其禁用。 (一段时间。)请帮忙。 我可能不是新人 - Im making a very simple clicker game and I want to after they buy a buyable (button) it disables. (for some time.) Please help. Im maybe not new 我的javascript函数似乎无法正常工作。 我不知道为什么 - My javascript function doesn't seem to be working. I can't figure out why 有人可以帮我解决这个问题吗? - Can someone help me fix this? JavaScript .active 似乎对我不起作用 - JavaScript .active doesn't seem to be working for me 有人可以帮我解决我的 javascript 中的 animation 问题吗? - Can someone help me,and fix the animation problem in my javascript? 第一次在无法正常运作的Clicker游戏中使用JavaScript - First time using javascript for a clicker game that doesn't work 有人可以帮助我使用 javascript 的一种分页效果吗? - Can someone help me with one of the pagination effect using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM