I have a small game that when all hearts are lost, a reset button should appear. I CANNOT for the life of me figure out why it’s not showing up. I’ve reworded it 3 times wondering if it was how I was assigning it’s value but none of them worked. The button appears as soon as I remove the statements for hiding it, but there should be validation statements working in place to make it APPEAR again.
const resetButton = document.getElementById(‘reset’);
function reset() {
location.reload();
}
resetButton.addEventListener('click', reset);
// Hide reset button until no hearts left
function hideReset() {
if (foodHeart1.src.match("images/emptyheart.png" && playHeart1.src.match("images/emptyheart.png"))) {
resetButton.style.display = "visible";
} else {
resetButton.style.display = "none";
}
}
hideReset();
Please help me
I have tried moving it around, placing it in new areas, but haven’t had much luck. I’m not sure how else I can write this.
2
Answers
What calls the function hideReset()? if it isn’t called in the primary update loop, or in the hit collision, nothing will get it there. Using the images to query whether or not the player is dead is an interesting idea, but it would be more performant to just use a variable.
I feel like I might need to add some more code to answer completely… I hope this is helpful!
Here is some code that appears to get your required functionality:
I don’t believe there is a style.display = "visible", but I hope someone will correct me if that is wrong. (From a quick look at MDN. I have set it to inline here but you can use any appropriate value to display the button.
I also used the length operator on the match function as it returns an array (for your conditional statemnt).
As James mentioned in the comment the hideReset() function is likely going to be called where you update your heart values. In this case I have put it in the function that changes the images.
There will be many ways to improve this but I hope it gets you over the initial bump you’re facing