Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Andengine: destinguish between OnAreaTouched and OnSceneTouched

Hi Everyone,

in the game I develop with AndEngine, there are a lot of sprites running around. Now every of this sprites has a TouchArea registered to the scene, because I display some informations about the sprite when it is touched. The Scene itself has a OnSceneTouchListener that I use for moving the camera around and to zoom.

My problem is, that every time the user moves the camera (by touching the display somewhere and moving his finger around) the OnAreaTouched() method of any sprite, that is accidentally under the finger, gets called, when the movement is finished (finger gets lifted). I already limited the triggering events to event.getAction()==UP (before it was a real mess of called touchAreas) but this is not enough. If the user is zooming or moving the camera, the sprites touchAreas should not be activated.

Is there any way I can distinguish between an OnAreaTouched-event and an OnSceneTouched-event? Which one is called first and can I suppress the other?

This is my OnSceneTouched() method (simplified):

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, actually this is not very interesting - but as you can see I always return true to signal that the touch event was handled. Still the OnAreaTouched() gets called

This is a typical OnAreaTouched() Method of a sprite:

public boolean onAreaTouched(final TouchEvent touchEvent, float touchAreaLocalX, float touchAreaLocalY) {
    if (touchEvent.getAction() == TouchEvent.ACTION_UP) {
          // DISPLAY INFORMATION ABOUT THE SPRITE
          return true;
    }
    return false;
}

You see, there is nothing special to it. So I hope someone can help me out here to find a solution how to suppress the OnAreaTouch-event when the OnSceneTouch-event should be used. Maybe I can somehow catch the event.getAction()==UP in the OnSceneTouched()-Method??

I hope I could explain the problem good enough for you to understand (sorry, it's not that easy for me : ). Any help is much appreciated, and thank you for you time!

regards
Christoph

edit:

After experimenting with MahdeTo's suggestion to tag the event somehow I found out the following:

  • the TouchEvent that triggers the OnSceneTouchEvent() Method is not the same as the one triggering the OnAreaTouched() Method.
  • OnAreaTouched() gets called 20 ms later than OnSceneTouchEvent()
  • the event calling OnAreaTouched() starts actually when the user puts his finger down on the display (than he moves it around and the OnSceneTouchEvent() gets called multiple times), when he then lifts his finger the first event stops and gets handled. (I tried it out by measuring the time)

So I came up with the solution to measure how long a touch event lasted. If the event is longer than 200 ms, I guess the user wanted not to simply click but move or zoom (because these actions usually take longer). So now the OnAreaTouched() method gets only called when someone really meant to click and not accidentally swiped over the area.

but it's still not a good solution and I would really appreciate if anyone knows more about controlling such events.

thank you

like image 847
GameDroids Avatar asked Jun 21 '12 12:06

GameDroids


1 Answers

I have recently written a fairly complicated application using touch controls - pressing buttons on the screen, pinch zooming, rotating and moving objects with two fingers and more. What I ended up doing was iteratively improving the application and adding rules controlling how to respond to different combinations of touch events. Whenever something was wrong, I recorded the sequence of TouchEvents and added rules so that the misbehaving action was handled.

You can also create boolean switches that prevent the onAreaTouched to execute, just add a condition that, for example if you want to touch an object, checks whether doNotTouchObject is true or false.

Another thing I found useful were touch mode switchers. I wrote several methods that completely change the behavior when user touches the screen. I believe you can switch them on the go, so it should be possible to call scene.setOnAreaTouchListener(touchListener) while you are touching the screen.

Since un/registering touch areas is blazing fast, simply unregistering certain touch areas while you perform some action is a possibility. After the action is completed, reregister them again.

I used a combination of all of the above and it works rather well, but it is essential to keep the code clean, otherwise debugging or implementing new features will be a pain.

like image 170
JohnEye Avatar answered Oct 22 '22 07:10

JohnEye