Hello everybody,
there are a lot of sprites in the game that I am developing with AndEngine. Now each of these sprites has TouchArea
registered on the scene because I show some information about the sprite when it is touched. The scene itself has an OnSceneTouchListener
, which I use to move the camera and zoom.
My problem is that every time the user moves the camera (by touching the screen somewhere and moving his finger), the OnAreaTouched()
method of any sprite that accidentally falls under the finger is called when the movement is finished (the finger rises). I have already limited trigger events to event.getAction()==UP
(before it became a real mess called touchAreas), but this is not enough. If the user zooms in or moves the camera, do not activate sprite touch screens.
Is there a way to distinguish between OnAreaTouched
-event and OnSceneTouched
-event? Which one is called the first and can I crush the other?
This is my OnSceneTouched()
(simplified) method:
public boolean onSceneTouchEvent(Scene scene, final TouchEvent event) { boolean isZooming = event.getMotionEvent().getPointerCount() >= 2; if (event.getAction() == MotionEvent.ACTION_DOWN) { // REMEMBER FIRST TOUCHPOINT, TO KNOW IN WHICH DIRECTION TO MOVE this.touchPoint = new Point(event.getMotionEvent().getX(), event.getMotionEvent().getY()); } else if (event.getAction() == MotionEvent.ACTION_MOVE) { if (isZooming) { // DO SOME ZOOM STUFF } else { // DO SOME MOVEMENT STUFF } return true; }
OK, this is actually not very interesting, but as you can see, I always return true
to signal that a touch event has been processed. However, OnAreaTouched()
gets a call
This is a typical OnAreaTouched()
sprite method:
public boolean onAreaTouched(final TouchEvent touchEvent, float touchAreaLocalX, float touchAreaLocalY) { if (touchEvent.getAction() == TouchEvent.ACTION_UP) {
You see, there is nothing special. So I hope someone can help me find a solution here on how to suppress OnAreaTouch
-event when using OnSceneTouch
-event. Maybe I can somehow catch event.getAction()==UP
in OnSceneTouched()
-Method ??
Hope I could explain the problem, good enough for you to understand (sorry, this is not so easy for me :). Any help is greatly appreciated and thanks for your time!
respectfully
Christoph
edit:
After experimenting with MahdeTo's suggestion to mark the event in some way, I found out the following:
TouchEvent
, which runs the OnSceneTouchEvent()
method, does not match the one that runs the OnAreaTouched()
method.OnAreaTouched()
receives the call 20 ms later OnSceneTouchEvent()
- the event that
OnAreaTouched()
actually begins when the user places his finger on the display (by which he moves it, and OnSceneTouchEvent()
is called several times), when he then raises his finger, the first event stops and gets processed. (I tried this by measuring time)
So, I came up with a solution to measure how long the touch event lasted. If the event is longer than 200 ms, I think that the user just wanted not to click, but to move or enlarge (since these actions usually take longer). So, now the OnAreaTouched()
method is called only when someone really wanted to click and not accidentally miss the area.
but this is still not a very good solution, and I would really appreciate it if anyone knew about managing such events.
Thank you