# setPollAlways not working

**URL:** <https://phaser.discourse.group/t/setpollalways-not-working/5841>\
**Category:** Phaser 3\
**Created:** [April 9, 2020, 11:41am UTC](https://phaser.discourse.group/t/setpollalways-not-working/5841 "2020-04-09T11:41:38Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![ogamrima](https://avatars.discourse-cdn.com/v4/letter/o/e68b1a/32.png) [@ogamrima](https://phaser.discourse.group/u/ogamrima)\
**Post date:** [April 9, 2020, 11:41am UTC](https://phaser.discourse.group/t/setpollalways-not-working/5841/1 "2020-04-09T11:41:38Z")

</div>

Hello, I am trying to make a topdown shooter game, but my problem is that I can’t make the player rotate correctly.  
The main camera is following the player, but whenever the camera moves, this.input.mousePointer.worldX and worldY don’t update if I don’t move the pointer.  
So I searched for this problem and I saw that this.input.setPollAlways was used for this, but it doesn’t seem to work either. I would prefer a solution without locking the pointer and reticles.

Here’s the code:  
class GamePlay extends Phaser.Scene {

```
  constructor() {

    super({

      key: "GamePlay",

    });

    this.speed = 400;

    this.w = 3200;

    this.h = 2400;

  }

  preload() {}

  create() {

    this.background = this.add.image(this.w / 2, this.h / 2, "background");

    this.player = this.physics.add.sprite(this.w / 2, this.h / 2, "player");

    this.player.setScale(0.25).setCollideWorldBounds();

    this.physics.world.setBounds(0, 0, this.w, this.h);

    this.cameras.main.zoom = 0.5;

    this.cameras.main.startFollow(this.player, true);

    this.cameras.main.setBounds(0, 0, this.w, this.h);

    this.input.setPollAlways();

    this.txt = this.add.text(

      this.cameras.main.scrollX,

      this.cameras.main.scrollY,

      "h ", {

        font: "25px Arial",

        fill: "yellow",

      }

    );

    this.moveKeys = this.input.keyboard.addKeys({

      up: Phaser.Input.Keyboard.KeyCodes.W,

      down: Phaser.Input.Keyboard.KeyCodes.S,

      left: Phaser.Input.Keyboard.KeyCodes.A,

      right: Phaser.Input.Keyboard.KeyCodes.D,

    });

    this.input.keyboard.on("keydown_W", (e) => {

      this.player.setVelocityY(-this.speed);

    });

    this.input.keyboard.on("keydown_S", (e) => {

      this.player.setVelocityY(this.speed);

    });

    this.input.keyboard.on("keydown_A", (e) => {

      this.player.setVelocityX(-this.speed);

    });

    this.input.keyboard.on("keydown_D", (e) => {

      this.player.setVelocityX(this.speed);

    });

    this.input.keyboard.on("keyup_W", (e) => {

      if (this.moveKeys["down"].isUp) {

        this.player.setVelocityY(0);

      }

    });

    this.input.keyboard.on("keyup_S", (e) => {

      if (this.moveKeys["up"].isUp) {

        this.player.setVelocityY(0);

      }

    });

    this.input.keyboard.on("keyup_A", (e) => {

      if (this.moveKeys["right"].isUp) {

        this.player.setVelocityX(0);

      }

    });

    this.input.keyboard.on("keyup_D", (e) => {

      if (this.moveKeys["left"].isUp) {

        this.player.setVelocityX(0);

      }

    });

  }

  update(time, delta) {

    this.player.rotation = Phaser.Math.Angle.Between(

      this.player.x,

      this.player.y,

      this.input.mousePointer.worldX, this.input.mousePointer.worldY

    );

    this.txt.text = this.input.mousePointer.worldX + ", " + this.input.mousePointer.worldY;

    this.txt.setPosition(this.cameras.main.scrollX, this.cameras.main.scrollY);

  }

}
```

---

<div class="post-metadata">

**Author:** ![UltimateProGrammer](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ultimateprogrammer/32/3069_2.png) [@UltimateProGrammer](https://phaser.discourse.group/u/UltimateProGrammer)\
**Post date:** [April 9, 2020, 12:24pm UTC](https://phaser.discourse.group/t/setpollalways-not-working/5841/2 "2020-04-09T12:24:58Z")

</div>

What are you trying to do? If you are trying to get a reticle, there is an example for that but here is how I would do it using event listeners:

```javascript
this.input.on('pointermove', function(pointer) {
  reticle.x = pointer.x + this.cameras.main.scrollX;
  reticle.y = pointer.y + this.cameras.main.scrollY;
});

```

---

<div class="post-metadata">

**Author:** ![ogamrima](https://avatars.discourse-cdn.com/v4/letter/o/e68b1a/32.png) [@ogamrima](https://phaser.discourse.group/u/ogamrima)\
**Post date:** [April 9, 2020, 12:27pm UTC](https://phaser.discourse.group/t/setpollalways-not-working/5841/3 "2020-04-09T12:27:18Z")

</div>

Thats the thing, I don’t want reticles

---

<div class="post-metadata">

**Author:** ![UltimateProGrammer](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ultimateprogrammer/32/3069_2.png) [@UltimateProGrammer](https://phaser.discourse.group/u/UltimateProGrammer)\
**Post date:** [April 9, 2020, 12:28pm UTC](https://phaser.discourse.group/t/setpollalways-not-working/5841/4 "2020-04-09T12:28:55Z")

</div>

Oh sorry. Do you just want to get the position of the mouse?

somewhere in global scope:

```javascript
var mouseX = 0;
var mouseY = 0;

```

In create():

```javascript
this.input.on('pointermove', function(pointer) {
  mouseX = pointer.x + this.cameras.main.scrollX;
  mouseY = pointer.y + this.cameras.main.scrollY;
});

```

---

<div class="post-metadata">

**Author:** ![UltimateProGrammer](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ultimateprogrammer/32/3069_2.png) [@UltimateProGrammer](https://phaser.discourse.group/u/UltimateProGrammer)\
**Post date:** [April 9, 2020, 12:37pm UTC](https://phaser.discourse.group/t/setpollalways-not-working/5841/5 "2020-04-09T12:37:29Z")

</div>

If you wanted a more “right” way to do this then you can use vectors but they are a little difficult to wrap your head around if you’re a beginner.  
somewhere in global scope:

```javascript
vec2 mouse = (0,0)

```

In create():

```javascript
this.input.on('pointermove', function(pointer) {
  mouse.x = pointer.x + this.cameras.main.scrollX;
  mouse.y = pointer.y + this.cameras.main.scrollY;
});

```

And then you can just refer to it using .x and .y.

---

<div class="post-metadata">

**Author:** ![ogamrima](https://avatars.discourse-cdn.com/v4/letter/o/e68b1a/32.png) [@ogamrima](https://phaser.discourse.group/u/ogamrima)\
**Post date:** [April 9, 2020, 12:38pm UTC](https://phaser.discourse.group/t/setpollalways-not-working/5841/6 "2020-04-09T12:38:04Z")

</div>

Unfortunately, it has the same result. It works okay when you constantly move the mouse, but when you don’t and just move the camera, the x, y of the pointer don’t refresh.

---

<div class="post-metadata">

**Author:** ![UltimateProGrammer](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ultimateprogrammer/32/3069_2.png) [@UltimateProGrammer](https://phaser.discourse.group/u/UltimateProGrammer)\
**Post date:** [April 9, 2020, 12:38pm UTC](https://phaser.discourse.group/t/setpollalways-not-working/5841/7 "2020-04-09T12:38:22Z")

</div>

Ohhhhh. Yeah that totally makes sense. Sorry I misunderstood a little.

Put

```javascript
mouse.x = pointer.x + this.cameras.main.scrollX;
mouse.y = pointer.y + this.cameras.main.scrollY;

```

somewhere inside your update() function, and get rid of the mouse move listener. That’s my bad.

This answer assumes your using the vector approach, here is it without that:

```javascript
mouseX = pointer.x + this.cameras.main.scrollX;
mouseY = pointer.y + this.cameras.main.scrollY;

```

---

<div class="post-metadata">

**Author:** ![ogamrima](https://avatars.discourse-cdn.com/v4/letter/o/e68b1a/32.png) [@ogamrima](https://phaser.discourse.group/u/ogamrima)\
**Post date:** [April 9, 2020, 12:41pm UTC](https://phaser.discourse.group/t/setpollalways-not-working/5841/8 "2020-04-09T12:41:39Z")

</div>

Where do i get the pointer.x and pointer.y from then?

---

<div class="post-metadata">

**Author:** ![UltimateProGrammer](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ultimateprogrammer/32/3069_2.png) [@UltimateProGrammer](https://phaser.discourse.group/u/UltimateProGrammer)\
**Post date:** [April 9, 2020, 12:43pm UTC](https://phaser.discourse.group/t/setpollalways-not-working/5841/9 "2020-04-09T12:43:50Z")

</div>

You should be access the variable(s) from anywhere if you declared it globally.  
You can declare it globally by putting it in a script tag in your html. e.g.

```javascript
// index.html
<script>var mouseX; varMouseY;</script>

```

---

<div class="post-metadata">

**Author:** ![ogamrima](https://avatars.discourse-cdn.com/v4/letter/o/e68b1a/32.png) [@ogamrima](https://phaser.discourse.group/u/ogamrima)\
**Post date:** [April 9, 2020, 12:46pm UTC](https://phaser.discourse.group/t/setpollalways-not-working/5841/10 "2020-04-09T12:46:38Z")

</div>

yes that’s the mouseX, mouseY but what about the pointer.x in this "mouseX = **pointer.x** + this.cameras.main.scrollX

---

<div class="post-metadata">

**Author:** ![UltimateProGrammer](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ultimateprogrammer/32/3069_2.png) [@UltimateProGrammer](https://phaser.discourse.group/u/UltimateProGrammer)\
**Post date:** [April 9, 2020, 12:49pm UTC](https://phaser.discourse.group/t/setpollalways-not-working/5841/11 "2020-04-09T12:49:31Z")

</div>

Of course. Give me one second I have to find it in the docs

---

<div class="post-metadata">

**Author:** ![UltimateProGrammer](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ultimateprogrammer/32/3069_2.png) [@UltimateProGrammer](https://phaser.discourse.group/u/UltimateProGrammer)\
**Post date:** [April 9, 2020, 12:50pm UTC](https://phaser.discourse.group/t/setpollalways-not-working/5841/12 "2020-04-09T12:50:55Z")

</div>

declare `var pointer` in global scope and then in create() use this:

```javascript
pointer = this.input.activePointer;

```

---

<div class="post-metadata">

**Author:** ![ogamrima](https://avatars.discourse-cdn.com/v4/letter/o/e68b1a/32.png) [@ogamrima](https://phaser.discourse.group/u/ogamrima)\
**Post date:** [April 9, 2020, 12:52pm UTC](https://phaser.discourse.group/t/setpollalways-not-working/5841/13 "2020-04-09T12:52:23Z")

</div>

was just about to say that I got it… thank you, it works

in update()

```
  this.mouseX = this.input.mousePointer.x + this.cameras.main.scrollX;
  this.mouseY = this.input.mousePointer.y + this.cameras.main.scrollY;
  this.player.rotation = Phaser.Math.Angle.Between(
    this.player.x,
    this.player.y,
    this.mouseX, this.mouseY
  );
```

---

<div class="post-metadata">

**Author:** ![UltimateProGrammer](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ultimateprogrammer/32/3069_2.png) [@UltimateProGrammer](https://phaser.discourse.group/u/UltimateProGrammer)\
**Post date:** [April 9, 2020, 12:53pm UTC](https://phaser.discourse.group/t/setpollalways-not-working/5841/14 "2020-04-09T12:53:29Z")

</div>

Make sure to mark whatever helped you get it as solution.

---

<div class="post-metadata">

**Author:** ![ogamrima](https://avatars.discourse-cdn.com/v4/letter/o/e68b1a/32.png) [@ogamrima](https://phaser.discourse.group/u/ogamrima)\
**Post date:** [April 10, 2020, 1:10pm UTC](https://phaser.discourse.group/t/setpollalways-not-working/5841/15 "2020-04-10T13:10:28Z")

</div>

See [Problem with player rotation by mouse](https://phaser.discourse.group/t/problem-with-player-rotation-by-mouse/5867/2) for correct solution.
