[英]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.")
}
I suggest using more representative names for your element variables.我建议为您的元素变量使用更具代表性的名称。 It makes your code more readable.它使您的代码更具可读性。
I think you want to call rebirth
from the rebirth button.我想你想从重生按钮调用rebirth
。
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.