Updating a variable with this.input.once('pointerdown'...)

Hi there, I’m very new to Phaser so I apologize if this has already been answered (I did spend a long time searching for an answer without any luck).

Basically I am creating a game where when a mouse is clicked, an action happens. Once that action happens, (i.e. when the mouse is clicked) I want to update a variable. I don’t want this action to occur multiple times if the mouse button is held down which is why I have found success with
“with this.input.once(‘pointerdown’…”.

I have been able to get the action working the way that I want, but updating the variable is driving me crazy as it seems to be so simple but isn’t working.

Here is an example:

 this.input.once('pointerdown', function (mouse) {
        variable1 = variable1 +1;
 }, this);

The variable does go up, but it is by weird amounts and seems to depend on the last time you clicked.
Its as if variable1 is being increased by 200 every second or every “game tick”, even in the background. Then only when we click, does it actually update the variable that we can see.

On the flip side, using keyboard commands, it works perfectly normally:

if(Phaser.Input.Keyboard.JustDown(this.cursors.left))  {
            variable1 = variable1 + 1;
        } 

Is there something completely obvious with what I am doing wrong? My best guess is that there is something specific about the ‘pointerdown’ event that I don’t know. Even if I don’t click the mouse at all on game start this variable appears to be changing in the background or something because the first time I click, it could jump to very high numbers.

I appreciate any help in advance!

:wave:

You should be calling once('pointerdown', …) in your scene create function (or a similar function), but not in the scene update function.

Wow that simple! Moving to the Create() function worked like a charm thank you!