3
\$\begingroup\$

In my AndEngine-based game there are a few sprites on the scene. One sprite acts as a surface and the rest of them are placed on top of this surface. I want to take unique actions whenever any of the these sprites is touched (the surface as well as the sprites on top of it). When I attach use registerTouchListerner for the surface sprite the Touch Listener does not work for any other sprite on top of it. Touching the sprites on top triggers the event, but it thinks that the surface sprite has been touched.

When I remove the touch listener from the surface it can successfully responds to touch event for the rest of the sprites on top.

So, does the AndEngine allow to attach OnAreaTouch listener to sprites on the top layer when the bottom layer sprite has already been attached to a touch listener?

(OnSceneTouched event cannot be used, as the 'surface' sprite only occupies bottom half of the screen and not the complete scene background)

\$\endgroup\$
3
  • 1
    \$\begingroup\$ I got the solution. If you attach the surface sprite to the listener BEFORE attaching listener to the top sprites it doesn't work. I changed the code to attach listener to the top sprites FIRST and later to the surface sprite, things started to work fine. \$\endgroup\$
    – Haider
    Aug 21 '13 at 14:06
  • \$\begingroup\$ I am short of points. Can't answer my own question \$\endgroup\$
    – Haider
    Aug 27 '13 at 7:00
  • \$\begingroup\$ My problem is also the same like you..and solution is also like your solution but i cant implement ...beacuse i want attach sprite2 to listener on touching of top sprite1 \$\endgroup\$
    – Passion
    Nov 22 '13 at 6:06
1
\$\begingroup\$

To quote @Haider on his solution in comments:

I got the solution. If you attach the surface sprite to the listener BEFORE attaching listener to the top sprites it doesn't work. I changed the code to attach listener to the top sprites FIRST and later to the surface sprite, things started to work fine.

So this sounds like a order of execution issue. If you're having a similar issue try organising your event listeners in an a different order.

A huge tip for anyone starting with events, especially those related to drawing, get a piece or paper and write out the layers of your scene from the furthest background right up to your foreground effects sprites and put them in order. This should be the order that you draw them in and the opposite order for how you interact with them (mouse click, touch screen etc.)

Good luck!

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .