setPollAlways not working

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);

  }

}

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:

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

Thats the thing, I don’t want reticles

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

somewhere in global scope:

var mouseX = 0;
var mouseY = 0;

In create():

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

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:

vec2 mouse = (0,0)

In create():

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.

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.

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

Put

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:

mouseX = pointer.x + this.cameras.main.scrollX;
mouseY = pointer.y + this.cameras.main.scrollY;
1 Like

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

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.

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

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

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

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

pointer = this.input.activePointer;

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
  );
1 Like

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

1 Like

See Problem with player rotation by mouse for correct solution.