Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Get Mouse Position?


Recommended Posts


I am very new to Phaser, so this is a very basic question. I want objects to follow the mouse around the screen. To do this I was going to get mouse position values and then set the x and y coordinates of a sprite to the mouse position. Unfortunately I could not find an example of this from the files and thought that the forums would be the quickest and easiest place for an answer.



Link to comment
Share on other sites

  • 4 months later...

@Rich: Local to the item that was clicked. 


I attach a listener to the background of my group.

holder = game.add.group();//holder.id = id;holder.bg = holder.create(0, 0, 'bg');holder.bg.inputEnabled = true;holder.bg.events.onInputDown.add(this.level_clicked, holder);		


level_clicked: function(bg){	var holder = bg.group;		console.log(holder.input.x); // fails	console.log(this.input.x);   // fails        console.log(bg.input.x);     // returns undefined	console.log(game.input.x);   // works...},

If I can get the screen position of the click, I can mathematically work out where abouts the click has occurred within the holder, but this defeats the object of having a listener for each holder... <edit> I realised that's not true, if I uncomment my holder.id line, then the calculation becomes very easy - just not as easy as pulling the x/y position straight off the object that was clicked

Link to comment
Share on other sites

both pointer.x and .y are returning the same values as .worldX and .worldY.


I could be wrong, but I believe the world values would only differ from pointer.x/y if the world (area of the game) is greater than the area of the canvas it is being played it.




Game =  800px x 800px, Canvas = 800px x 800px  = World and pointer events will return same coordinates


Game = 2000px x 2000px, Canvas = 800px x 800px = World and pointer events may differ

Link to comment
Share on other sites

  • 3 years later...

Thanks, samme. 


the world X and Y are the coordinates I am looking for. How do I get those coordinates into my function? Can I call activePointer anywhere in my code to get them? Thank you!


Looks like the solution may be:

game.input.activePointer.x + game.camera.x,

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...