daemiean Posted June 6, 2014 Share Posted June 6, 2014 Hi, Is there any way to obtain the point inside the sprite where you are clicking? What I want is to apply force in the opposite direction to the center of the sprite, so I would need to know where I clicked, sorry if is a stupid question but I'm new with phaser. Thanks! Link to comment Share on other sites More sharing options...
lewster32 Posted June 7, 2014 Share Posted June 7, 2014 When the pointer is over the sprite, you can check sprite.input.pointerX() and sprite.input.pointerY() to give you where the sprite was clicked. These values are zeroed on the top left of the sprite so some calculation may be needed to get it relative to the centre of the sprite (not sure if these values take into account anchor, for instance). So something like this:function update() { if (sprite.input.justPressed(game.input.activePointer)) { var clickPosition = new Phaser.Point(sprite.input.pointerX(), sprite.input.pointerY()); // ... do something here with clickPosition to determine desired velocity... }} Link to comment Share on other sites More sharing options...
daemiean Posted June 7, 2014 Author Share Posted June 7, 2014 Wow, now fails and cuts off the rendering of the game with this log in console: TypeError: this._pointerData[a] is undefined... any idea why? Link to comment Share on other sites More sharing options...
lewster32 Posted June 7, 2014 Share Posted June 7, 2014 Weird, that's the private var it consults to get the position. What version are you using? The docs have this as being relevant to 2.0.5. Link to comment Share on other sites More sharing options...
lewster32 Posted June 7, 2014 Share Posted June 7, 2014 If for some reason this is broken, the next best alternative I can suggest is to calculate where the pointer is via game.input.activePointer.worldX and .worldY and then comparing it to the sprite's position. If you take the difference of the pointer's world position from the sprite position you end up with the same values as above. Link to comment Share on other sites More sharing options...
Recommended Posts