onAreaTouched - press and held down not working

  ... discussions about development with the GLES2 branch of AndEngine.

onAreaTouched - press and held down not working

Postby unexpected62 » Mon Mar 05, 2012 10:23 am

I searched ..... and from the lack of results I'm assuming that I'm either not understanding something or I have a bug.

I created a sprite and overrode onAreaTouched(). I want to make it so that when it's pressed, it does its action, but also, if they continue to hold down the button, the action continues. Here's my code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Sprite downArrow = new Sprite(upDownArrowsX, downArrowY, this.mArrowTextureRegion, getVertexBufferObjectManager()){
  2.                         @Override
  3.                         public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
  4.                                 mobile.setAngle(mobile.getAngle() - 1);
  5.                                 return true;
  6.                         }
  7.                 };
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Trouble is, this only works if it is repeatedly tapped... well sort of. If I tap and hold in the same spot on the screen, it won't repeat. But if I "rub" the button for lack of a better word and give it more input points still within the touch area, the event will continue without me lifting my finger.

I checked the "ZoomExample" from the AndEngine examples... the one where if you touch and hold the screen zooms. The implementation is identical to mine, and shown below.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. scene.setOnSceneTouchListener(new IOnSceneTouchListener() {
  2.                         @Override
  3.                         public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
  4.                                 switch(pSceneTouchEvent.getAction()) {
  5.                                         case TouchEvent.ACTION_DOWN:
  6.                                                 ZoomExample.this.mSmoothCamera.setZoomFactor(5.0f);
  7.                                                 break;
  8.                                         case TouchEvent.ACTION_UP:
  9.                                                 ZoomExample.this.mSmoothCamera.setZoomFactor(1.0f);
  10.                                                 break;
  11.                                 }
  12.                                 return true;
  13.                         }
  14.                 });
  15.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


I tried using this switch format and I didn't get any different results. In fact, doing that actually made it so that you couldn't even "rub it" at all. Had to actually lift the finger to get the action to occur again. My only thought is that this is the "onSceneTocuhEvent()" being overrode instead of the onAreaTouched(). Maybe something is different in the backend of the source code??

I'm pretty stumped on this one. Any help is appreciated.
unexpected62
 
Posts: 56
Joined: Sun Feb 26, 2012 10:29 pm

Re: onAreaTouched - press and held down not working

Postby Harti79 » Mon Mar 05, 2012 10:42 am

I've solved this by settin in the onTouch Area method a variable to the actual last action to do and update my player in an updatehandler with this action stored. So when in method onTouchArea the Action is UP i set to 0 and when DOWN to 1:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public boolean onAreaTouched(TouchEvent pSceneTouchEvent,float pTouchAreaLocalX, float pTouchAreaLocalY)
  2.                                 {
  3.                                         if(mGameRunning){
  4.                                                 if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP){
  5.                                                         mPlayer.setChangeX(0);
  6.                                                 }else{
  7.                                                         mPlayer.setChangeX(-1);
  8.                                                 }
  9.                                         }
  10.                                         return true;
  11.                                 }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


In Player Sprite in the onManagedUpdate() i do the update of the Player with this variable mPlayer.changeX.
Harti79
 
Posts: 90
Joined: Wed Dec 21, 2011 5:48 pm

Re: onAreaTouched - press and held down not working

Postby unexpected62 » Mon Mar 05, 2012 10:45 am

Wow thanks for the quick reply. Makes sense, I'll give it a shot tomorrow and post back.
unexpected62
 
Posts: 56
Joined: Sun Feb 26, 2012 10:29 pm

Re: onAreaTouched - press and held down not working

Postby unexpected62 » Mon Mar 05, 2012 10:59 am

You know... thinking about this more...... this would work. However, how can you implement a "debouncing" fix then? Because now, it would be very difficult to only have the event take place one time if they really did intend to just tap.

I kind of want this functionality:

Tap: do it once
Tap and Hold: wait and if held long enough, then repeatedly do it until finger lifted.

How can that wait threshold be established here?
unexpected62
 
Posts: 56
Joined: Sun Feb 26, 2012 10:29 pm

Re: onAreaTouched - press and held down not working

Postby Harti79 » Mon Mar 05, 2012 11:42 am

Then call in the onAreaTouched the move method too.
Then it will be done when hold and when clicked i think.
Harti79
 
Posts: 90
Joined: Wed Dec 21, 2011 5:48 pm

Re: onAreaTouched - press and held down not working

Postby unexpected62 » Fri Mar 09, 2012 7:30 am

Ok....... I feel like I must be missing something.

I know the solution posted above will work, but its not really "correct" because it doesn't solve all the issues. For example, right now I want the action to take place if tapped, the action to repeatedly take place if pressed and held, and the action to stop when the finger is lifted.

The action can take place if held using the code above, but if the user presses, moves their finger outside the Sprite, and then releases, the isActionUp() will never be triggered, because the "isActionUp()" occurred outside of the Sprite's area.

And yes, I've setTouchAreaBindingOnActionDownEnabled(true);

Basically, I feel like if someone can answer this question, I will have my answer.

What the heck has to happen for TouchEvent.isActionCanceled() and TouchEvent.isActionOutside() to be triggered? These seem to describe my use case perfectly, but they don't work as I'd expect.
unexpected62
 
Posts: 56
Joined: Sun Feb 26, 2012 10:29 pm

Re: onAreaTouched - press and held down not working

Postby kyv » Fri Mar 16, 2012 4:55 pm

Try something like the HoldDetector class in the latest and greatest AndEngine. You might need to tweak it a bit so it does an action upon the initial touch.

cheers
kyv
 
Posts: 1
Joined: Fri Mar 16, 2012 4:53 pm

Re: onAreaTouched - press and held down not working

Postby unexpected62 » Sat Mar 17, 2012 10:32 pm

kyv wrote:Try something like the HoldDetector class in the latest and greatest AndEngine. You might need to tweak it a bit so it does an action upon the initial touch.

cheers


Thanks for the suggestion..... but I can't find a single example of how this is used.... and I don't think its in the examples.
unexpected62
 
Posts: 56
Joined: Sun Feb 26, 2012 10:29 pm

Re: onAreaTouched - press and held down not working

Postby unexpected62 » Wed Apr 25, 2012 11:42 pm

I know I posted this about two months ago, but I'm finally getting around to working on fixing this "bug" because I have been putting it off......

Exactly as I have described above, there is a missing piece.

...but if the user presses, moves their finger outside the Sprite, and then releases, the isActionUp() will never be triggered, because the "isActionUp()" occurred outside of the Sprite's area.


How can this be remedied? My only thought is to implement some sort of if the sprite.contains(x, y) of the touch event then you know its inside. However, this seems to be ludicrous in my opinion because then I'm reinventing the wheel on top of a touch system that should just work already!
unexpected62
 
Posts: 56
Joined: Sun Feb 26, 2012 10:29 pm

Re: onAreaTouched - press and held down not working

Postby 5Type » Mon May 07, 2012 11:46 pm

Hi,

going to pull this thread up again, because im facing the same issue.

I have my Sprite touched, scaling it by 1.1f if the TouchEvent is ACTION_DOWN and scaling it back to initial size when ACTION_UP is fired. But if press and move outside the Sprite, the ACTION_UP never gets triggered. How can this be solved?

Already searched my night though some threads, but wasnt able to find one containing a solution.


Thanks in advance :)
5Type
 
Posts: 9
Joined: Mon May 07, 2012 8:04 am
Location: Cologne, Germany

Next

Return to GLES2

Who is online

Users browsing this forum: BadChoice2, vocado, xZippo and 37 guests