JackBid

Get Mouse Position?

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!

Share this post


Link to post
Share on other sites
sprite.x = game.input.x;sprite.y = game.input.y;Or if you want the mouse specifically:sprite.x = game.input.mousePointer.x;sprite.y = game.input.mousePointer.y;

Thanks, this is what I was looking for!

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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

Share this post


Link to post
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;}

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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,

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.