JackBid Posted October 12, 2013 Share Posted October 12, 2013 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 More sharing options...
rich Posted October 12, 2013 Share Posted October 12, 2013 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; scofield, Dimple, WombatTurkey and 3 others 5 1 Link to comment Share on other sites More sharing options...
JackBid Posted October 12, 2013 Author Share Posted October 12, 2013 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! Link to comment Share on other sites More sharing options...
charlie_says Posted March 3, 2014 Share Posted March 3, 2014 Is it possible to get the local mouse position? (I'm trying to get the mouse position within a clicked group). Link to comment Share on other sites More sharing options...
rich Posted March 3, 2014 Share Posted March 3, 2014 Local to what? Link to comment Share on other sites More sharing options...
Heppell08 Posted March 3, 2014 Share Posted March 3, 2014 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 More sharing options...
charlie_says Posted March 3, 2014 Share Posted March 3, 2014 @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); Thenlevel_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 More sharing options...
rich Posted March 3, 2014 Share Posted March 3, 2014 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 More sharing options...
charlie_says Posted March 3, 2014 Share Posted March 3, 2014 both pointer.x and .y are returning the same values as .worldX and .worldY. Link to comment Share on other sites More sharing options...
owenconti Posted March 3, 2014 Share Posted March 3, 2014 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 More sharing options...
rich Posted March 3, 2014 Share Posted March 3, 2014 Yes absolutely. If you've got a scrolling game world they will only differ once the camera has moved. Link to comment Share on other sites More sharing options...
charlie_says Posted March 3, 2014 Share Posted March 3, 2014 Ah, that would explain it (it's not a scrolling game as such, but the holders move)... I'll leave the calculated version in place. Link to comment Share on other sites More sharing options...
charlieRobinson Posted September 5, 2017 Share Posted September 5, 2017 Hello. What is the solution if your game is scrolling and the mouse coordinates and angle is skewed because the camera is moving? Thank you! Link to comment Share on other sites More sharing options...
samme Posted September 5, 2017 Share Posted September 5, 2017 Link to comment Share on other sites More sharing options...
charlieRobinson Posted September 6, 2017 Share Posted September 6, 2017 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 More sharing options...
samme Posted September 6, 2017 Share Posted September 6, 2017 game.input.activePointer.worldX; game.input.activePointer.worldY; dream_castle 1 Link to comment Share on other sites More sharing options...
Recommended Posts