Skip to main content
Participant
July 6, 2012
Question

touch events not firing for sprites (on Air for Android)?

  • July 6, 2012
  • 6 replies
  • 2605 views

I tried to run a air app in Galaxy Tab 10.1 and i realise that I can only listen to touch events from stage but not from the movieclips.

i.e. stage.addeventlistener(TouchEvent...) -> will give response but abcSprite.addeventlistener(TouchEvent...) -> will NOT give response

Any one know why?

This topic has been closed for replies.

6 replies

Mark.fromOP
Inspiring
July 12, 2012

Hmm my only guess is that when you have a stage.TouchEvent it now takes over everything its like you assigned a touch event to the parent and all sprites are children of the stage so the stage takes over everything? Take out the stage events. So only have one touch event and assign it to that sprite/movieclip I be it will work.

Known Participant
July 11, 2012

As a general rule, within my code I try to keep from adding events directly to the stage. Although one good use is to add a temporary listener to the stage when a button gets a TOUCH_OUT or TOUCH_ROLL_OUT event that will catch a TOUCH_END that finished elsewhere on the stage. This lets the button properly reset its state.

By default AIR will turn touch events into mouse events and that works fine for simple apps. But if you want to listen for and process the touch events directly, if for instance you app wants to handle multi-touch, then add code like this:

if (Multitouch.supportsTouchEvents) {

     Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;

}

Just be aware that touch events have an id and they start with 0 and go up! So check for id's with 'if (touchId > -1)...'

I created my own ScreenButton class to contain all this functionality. Essentially it starts off with adding all the event listeners, records the touch id on the initial TOUCH_BEGIN event (unless its already tracking an id), then the other handlers for the button only respond to incoming touch events with that same id until the button resets state on a TOUCH_END.

If you find your touches are getting ignored, then its likely some other object is grabbing the event. Most commonly this can happen when a sub-MovieClip or textfield "steals" the click/tap. So if for instance a button or MovieClip needs to get the click then have it set mouseChildren = false so that none of its children can "steal" it.

On occasions I've had objects on a lower layer or lower depth within my own apps click steal - even when they have no event listeners registered! So as a general rule now, if an object is for display purposes only, then I'll set mouseEnabled = false and mouseChildren = false for it.

And when an event handler recognized a given touch id and handles it then you should have the code call preventDefault() on the incoming event. For example:

private function onTouchTap(touchEvent:TouchEvent) : void {

     touchEvent.preventDefault();

     // code here to process the event

     // Note: AIR must internally track Begin/End events to send Tap events, and a Tap event

     //  is only send when both a touch begin and end are received inside the object's area.

     //  Hence this handler doesn't need to check for a touchId - which is good because Tap

     //  events are sent AFTER a TOUCH_END event (where this object has already

     //  cleared its touchId tracking property.)

}

Participating Frequently
July 12, 2012

What a helpful answer!

Would be nice if you can take a look at my question? maybe very easy to you

http://forums.adobe.com/message/4551098

Regards,

Participant
July 9, 2012

help anyone?

Participating Frequently
July 11, 2012

Just curious, would the stage's handler function happen to have something like

stopImmediatePropagation();

somewhere in the code?