Touchinput: Move player while button is pressed

Hi everybody,

i have two buttons for multitouch input, one of them to run and the otherone to jump.
I want run while the button “run” is pressed and jump once if Button “jump” is pressed. Second works fine, but at the moment i have to click multiple times on the run-button to move.

Here my code:

create() {
    ...
    btn_run = this.add.image(50, 450, "button-right");
    btn_run.on("pointerdown", this.run);
    ...
}

run(){
	player.setVelocityX(200);
	scoreText.x = this.cameras.main.scrollX + 20;
	energyText.x = this.cameras.main.scrollX + 20;
	this.cameras.main.followOffset.x = -300;
}

The same run-function is called if Right Cursor is pressed and in this case it works well.

Would this help?

Sadly not, i have already three pointers and it also works if i touch both buttons at the same time.
My question is, how to recognize if the button is pressed and not just clicked once.

Every time i click the button my player moves a few pixels to right and then stops until i click the button again. But i want to move while its pressed…

Hi,
It seems to be an algorythm problem (but i can be wrong, i don’t use events on controls):
Each time you click on jump, run isn’t called anymore
Add console.log(‘RUN’) at beginning of run function, and console.log(‘JUMP’) at beginning of jump function, and see what happens.

Hmm i don’t think so, because it even doesn’t work if i just want to run. I added console.log(“RUN”) at the beginning and as expected if i touch the button the console Shows RUN one time for every touch and the funtion “run” is executed just once. May be the solution a while-loop? Is there an option to say: while(button_right.isPressed){ this.run();} or something like that?

Perhaps something like that, using two events and the update function:

create() {
  this.isRunning = false;
  btn_run.on("pointerdown", () => {
      this.isRunning = true;
  };
  btn_run.on("pointerup", () => {
     this.isRunning = false;
  };
}

update() {
  if(this.isRunning) {
    player.setVelocityX(200);
	scoreText.x = this.cameras.main.scrollX + 20;
	energyText.x = this.cameras.main.scrollX + 20;
	this.cameras.main.followOffset.x = -300;
  }
}
1 Like