# Touchinput: Move player while button is pressed

**URL:** <https://phaser.discourse.group/t/touchinput-move-player-while-button-is-pressed/6285>\
**Category:** Phaser 3\
**Created:** [May 11, 2020, 10:29am UTC](https://phaser.discourse.group/t/touchinput-move-player-while-button-is-pressed/6285 "2020-05-11T10:29:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Felicia\_Debye](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/felicia_debye/32/3220_2.png) [@Felicia\_Debye](https://phaser.discourse.group/u/Felicia_Debye)\
**Post date:** [May 11, 2020, 10:29am UTC](https://phaser.discourse.group/t/touchinput-move-player-while-button-is-pressed/6285/1 "2020-05-11T10:29:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ThiagoRodrigues](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/thiagorodrigues/32/3322_2.png) [@ThiagoRodrigues](https://phaser.discourse.group/u/ThiagoRodrigues)\
**Post date:** [May 13, 2020, 7:20am UTC](https://phaser.discourse.group/t/touchinput-move-player-while-button-is-pressed/6285/2 "2020-05-13T07:20:38Z")

</div>

Would this help?

> [@How to enable multitouch ? jsfiddle inside](https://phaser.discourse.group/t/how-to-enable-multitouch-jsfiddle-inside/2422):
>
> Hi, I have two sprites (button\_move and button\_fire) with two touchs events. I want to have multitouch enabled : when i press on the same time on the two buttons, normaly the alpha of the two buttons is 0.5. But it’s not working. I have follow the docs, especially this : [https://rexrainbow.github.io/phaser3-rex-notes/docs/site/index.html/touchevents/](https://rexrainbow.github.io/phaser3-rex-notes/docs/site/index.html/touchevents/)[https://labs.phaser.io/index.html?dir=input/multitouch/&q=](https://labs.phaser.io/index.html?dir=input/multitouch/&q=) And i made a jsfiddle about it : [https://jsfiddle.net/espace3d/vLq08gke/12/](https://jsfiddle.net/espace3d/vLq08gke/12/) the f…

---

<div class="post-metadata">

**Author:** ![Felicia\_Debye](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/felicia_debye/32/3220_2.png) [@Felicia\_Debye](https://phaser.discourse.group/u/Felicia_Debye)\
**Post date:** [May 13, 2020, 9:28am UTC](https://phaser.discourse.group/t/touchinput-move-player-while-button-is-pressed/6285/3 "2020-05-13T09:28:45Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![BlunT76](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/blunt76/32/1454_2.png) [@BlunT76](https://phaser.discourse.group/u/BlunT76)\
**Post date:** [May 13, 2020, 10:02am UTC](https://phaser.discourse.group/t/touchinput-move-player-while-button-is-pressed/6285/4 "2020-05-13T10:02:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Felicia\_Debye](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/felicia_debye/32/3220_2.png) [@Felicia\_Debye](https://phaser.discourse.group/u/Felicia_Debye)\
**Post date:** [May 13, 2020, 12:57pm UTC](https://phaser.discourse.group/t/touchinput-move-player-while-button-is-pressed/6285/5 "2020-05-13T12:57:48Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![BlunT76](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/blunt76/32/1454_2.png) [@BlunT76](https://phaser.discourse.group/u/BlunT76)\
**Post date:** [May 13, 2020, 1:51pm UTC](https://phaser.discourse.group/t/touchinput-move-player-while-button-is-pressed/6285/6 "2020-05-13T13:51:55Z")

</div>

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

```javascript
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;
  }
}

```
