afowler2k Posted January 31, 2016 Share Posted January 31, 2016 I am building a small GUI using Phaser and need to be able to add a sprite arbitrarily to the world. I would like the added sprite to have the same parent as the sprite the pointer is over. What I am struggling with is how to find the sprite that is visible directly under the pointer. I have tried iterating through all the children on the world looking for everything who's bounds contain that point, but then I don't know how to figure out which of those is actually the top most visible on the screen. Is there an easier way to get the sprite under the pointer? Link to comment Share on other sites More sharing options...
rcoaxil Posted January 31, 2016 Share Posted January 31, 2016 The top one is obviously the one with smallest Z coordinate. Exactly what are you trying to accomplish, though? Just duplicate a sprite with a small offset by clicking on it? What you're doing may not be the best approach altogether. afowler2k 1 Link to comment Share on other sites More sharing options...
afowler2k Posted February 1, 2016 Author Share Posted February 1, 2016 Hi, Thanks for the response. So yes, basically duplicate the sprite under the pointer with an offset would be a good start. What would be the best way to do this? Link to comment Share on other sites More sharing options...
rcoaxil Posted February 1, 2016 Share Posted February 1, 2016 You see, I'm asking because rather than looking for help accomplishing specific goal, you're looking for help accomplishing one very specific step on your way to the goal. But it might so happen that the whole path you chose is sub-optimal and thus the whole topic is moot. If you describe the goal you want to achieve, people might suggest better ways of getting there. I would keep an array of sprites that can be duplicated, sorted by depth in descending order, so I could iterate over them all and the first one to pass hit test is the one I'm looking for. Of course that would be redundant if you can do that with any sprite from a specific group. If there's really many of them, I'd also used spatial partitioning of some sort because too long lists create too big processing burden. Also keep in mind that sprite hit area is rectangle. So you may need to individually assign proper hit testing polygons to them. Link to comment Share on other sites More sharing options...
Recommended Posts