Reputation: 318
I am developing a natural disaster sequance for my game and using the Math.random to simulate variants of an outbreak. So the problem is at the very bottom of my code theres an if statement that should detect if the number hits 0 but it dosent work for some reason. I tried using parseInt() but it still dident work. I'm probaly doing something obveous wrong. Thank you for reading.
var start = document.getElementById("startbtn");
var waterOut = document.getElementById("waterOut");
start.addEventListener("click", disaster1);
function disaster1() {
//fire
var fireDisaster = Math.floor((Math.random() * 100) + 1);
var water = 1000;
setInterval(function() {
waterOut.innerHTML = "Water: " + water;
}, 1);
var waterbtn = document.getElementById("putFireOutbtn");
var fireCount = document.getElementById("fireCount");
var fireCountNumber = fireCount;
fireCount.innerHTML = "Fire: " + fireDisaster;
alert("Theres a wild fire! Use your water to put it out!");
if (fireDisaster >= 1) {
waterbtn.style.width = "100px";
waterbtn.style.height = "100px";
waterbtn.style.left = "50%";
waterbtn.addEventListener("click", putOutFire);
function putOutFire() {
if (water >= 1) {
water -= 1;
fireDisaster -= 1;
console.log(fireDisaster);
}
fireCount.innerHTML = "Fire: " + fireDisaster;
}
}
if (fireDisaster == 0) {
waterbtn.style.width = "0px";
waterbtn.style.height = "0px";
waterbtn.style.left = "1500px";
alert("You put out the fire!");
fireCount.innerHTML = "";
}
}
<p id="waterOut"></p>
<p id="fireCount"></p>
<button id="startbtn">start</button>
<button id="putFireOutbtn">put out the fire!</button>
Upvotes: 0
Views: 87
Reputation: 5290
Right now you're checking fireDisaster
only once when the disaster hits. You want to check it continuously as you put water on the fire, so add that code to the putOutFire
function:
function putOutFire() {
if (water >= 1) {
water -= 1;
fireDisaster -= 1;
console.log(fireDisaster);
}
fireCount.innerHTML = "Fire: " + fireDisaster;
if (fireDisaster == 0) {
waterbtn.style.width = "0px";
waterbtn.style.height = "0px";
waterbtn.style.left = "1500px";
alert("You put out the fire!");
fireCount.innerHTML = "";
}
}
Upvotes: 1