I am working on a fantasy game in AndEngine. I have created tiles dynamically (matrix format) and I placed a sprite on one of the tiles. I used onAreatouchlistener for identifying the sprites, I give a zvalue 0 for tiles and for sprite 1, but onAreaTouchListener does not identify the sprite, it only identifies the tiles. I want to know both values, when user touch on particular location. Please suggest a solution.
Here’s some sample code in use:
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final ITouchArea pTouchArea,
final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
if (pSceneTouchEvent.isActionDown()) {
final Sprite face = (Sprite) pTouchArea;
String mrc = (String) face.getUserData();
if (mrc.equals("48")) {
selectedtlie = true;
inittileLevel(level1);
} else
Toast.makeText(this, "" + mrc, Toast.LENGTH_SHORT).show();
i++;
if (i == 2)
return true;
else
return false;
}
if (pSceneTouchEvent.isActionUp()) {
i = 0;
return true;
}
return true;
}
There are a couple different things you might be asking. Your question is a little difficult to interpret, but I’ll try to go possible interpretation by possible interpretation.
Checking if Sprites are at Same Location
That’s your subject line. I think you’re asking something a little different, but if that’s all you’re asking, you can just compare the X’s and Y’s and ignore the Z’s, like this:
Checking if Sprites are Overlapping
Of course, you might mean something different by “same location” than just whether the top left corner is placed at the same point. For example, you might want to know if the two sprites are overlapping at all, regardless of whether they are precisely placed at the same XY. In that case you want to get into collision detection and there is more than one way to handle collisions (e.g., do you want pixel perfect collision testing?). In any event, here’s a nice example of detecting pixel-perfect collisions. If you want to do something simpler you could just get the bounding box Rect for each sprite and use the intersect() method in android.graphics.Rect.
Handling Touch Events
The body of your question asks the following, where you seem to be getting at something different:
Now I’m confused, because onAreaTouchListener does not “identify sprites,” it listens for touchevents. My guess is that what you’re asking for is a way to use touchEvents to both a sprite and another sprite underneath it.
Register TouchAreas
First: Make sure you’re registering the handlers for both sprites. I.e., something like:
Use the Boolean Returned by OnTouch()
If the touchevent isn’t being propagated through, another thing to look at is the boolean returned by onTouch(). That boolean is used to indicate whether the handler has consumed the event or if it should be passed through.
Check Touch Traversal Order
You might also play around with scene.setOnAreaTouchTraversalFrontToBack() and scene.setOnAreaTouchTraversalBackToFront() to see if they’re both receiving touch events and the issue is propagation through or if the issue is you haven’t registered the handler on one of the sprites.
Check AreaBinding
Finally, take a look at scene.setTouchAreaBindingEnabled(true). You mentioned in the comments you were having problem with the “TouchUp” event in particular. This might be the result of having touchareabinding set to false. Essentially what toucharea binding does is it allows the touch area to look at and receive touch events outside its toucharea. So if a finger goes down inside the area and goes up outside (and binding is off) it will not receive the touch up event. If binding is on, it will still receive the touch event.