I'm trying to make a game where I push a button and the number of times that the button is pressed is displayed

this is written in Javascript btw. I am just beginning to write code. everything I have learned is from khanacademy.org and this code is written in their code so it may be different IDK though :/
I am still on Intro To JS: Drawing and Animation.

QUESTION: why does it keep saying NaN for not a number when I run it? How do I fix it?

here is the code:

var buttonPressedNum = 0;
var buttonSize = 150;

var draw = function() {

background (50, 150, 250);

fill(250, 150, 50);
ellipse(200, 250, buttonSize, buttonSize);
fill(0, 0, 0);
text('PUSH ME', 130, 255);

if(mouseX > 125, mouseX < 275, mouseY > 175, mouseY < 325) {
    if(mouseIsPressed) {
        var buttonPressedNum = buttonPressedNum + 1;

var buttonPressedNum = buttonPressedNum + 0;

fill(255, 0, 0);
text ('Clicks: ' + buttonPressedNum, 91, 100);


sorry if formatting is bad this is my first post

1 answer

  • answered 2020-07-29 17:22 Wesley Weaver

    As mentioned in the comment, what's the question? Are you getting compile errors? Is it just not working?

    From looking at this it's just going to count up the button press count regardless of the IF statement and then end.

    you'd need the count and the display to only run inside the if statement where it checks the location and which mouse button

    you also probably only want this entire script to run on button press otherwise it's going to constantly update with the press count every cycle and probably just crash