Jump to content

Get Mouse Position?


JackBid
 Share

Recommended Posts

Hi,

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.

 

Thanks!

Link to comment
Share on other sites

  • 4 months later...

console.log('X:' + this.input.activePointer.x);

console.log('Y:' + this.input.activePointer.y);

That will log the position in console of the active pointer in the game. Basically the same as what rich said above but this tells you in console the mouse position.

Hope that's what you're looking for :)

Link to comment
Share on other sites

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

Then

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

onInputDown sends 2 parameters to your callback: the sprite and the pointer that clicked it.

 

So:

level_clicked: function (bg, pointer) {  pointer.x;  pointer.y;  // or if you need it in world coordinates:  pointer.worldX;  pointer.worldY;}
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.

 

ie:

 

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. 

 this.game.debug.pointer(this.input.activePointer);

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!

 

EDIT::::
Looks like the solution may be:

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

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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