Jump to content

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

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

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

×
×
  • Create New...