Question

i am making a game using andengine in android, in this game various sprites(eggs) are moving here and there on screen.i want to get touch event of each sprite independently, sometimes when one sprite is over another then onAreaTouched() give touch event of both sprites,in case when i return here false or super.onAreaTouched(),if i return here true then a sprite moving over the area which is registered for another sprite could not give the touch event

So what is appropriate way to get touch event of only upper shown sprite.

Was it helpful?

Solution

The other approach you could use is an IOnSceneTouchListener, with the onSceneTouchEvent method. You can get the X & Y from the pTouchEvent parameter, and check whether any sprite contains it.

Now, in case you didn't know, each entity has a list of children (And your scene is an entity). When an entity draws itself, it also calls the draw methods of the children. The childrens' draw methods are called from the first child method to the last (As their order in the list); Therefore, the first child on the list is drawn first. The last one is drawn last. So the last one will always override any other entities it is drawn over.

So, iterate over the scene children list from the last children to the first. If any sprite contains the point of touch, let it handle the event and return true to say that you consumed the event.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top