Mouse position is incorect on pointerdown

Hi, im trying to make to add object on click, but i have a problem when game is scaled, the position is incorrect.

Im using this for a game scale

mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH

and this to get the position of the mouse

this.scene.input.on(‘pointerdown’,function(pointer){

        var pointX = pointer.x;

        var pointY = pointer.y;
         
        App.graphics.strokeRectShape(new Phaser.Geom.Rectangle(pointX, pointY, 32, 32));

    });

But the position is wrong, everytime when you change browser size and game scale is changed mouse position is wrong and rect is drawn on wrong position.

Also when you move camera, position is also wrong.

It there any trick to fix this? Thanks.

Seems to work fine for me (3.54.0).
When using camera you probably need to use pointer.worldX.

1 Like

Thank you, i did like this and its working

var pointX = this.scene.input.mousePointer.x + this.scene.cameras.main._scrollX;
var pointY = this.scene.input.mousePointer.y + this.scene.cameras.main._scrollY;

but i also tried your solution and its working also!!!

var pointX = this.scene.input.mousePointer.worldX;
var pointY = this.scene.input.mousePointer.worldY;

Thank you, i will use worldX/Y its less code. Im using phaser (v3.53.1)

Hm I updated now to phaser (v3.54.0) and pointerdown doesnt work at all.
Im using DOM for game ui and phaser place div above canvas and that is blocking mouse click.
I tried setTopOnly but its not working. When i move canvas above div, everything is working fine.

However phaser (v3.53.1) didnt have this problem.

Yeah, known bug. Fixed in 55.
For now you could do:

1 Like

Nice, now its working, its a good that there is the solution. Thanks