Skip to main content
Inspiring
October 11, 2011
Answered

TouchEvent problem, please help

  • October 11, 2011
  • 4 replies
  • 3621 views

Hi. So I have this ship on the stage and 2 buttons. One button is Left, other is Right. When I press and hold left, the ship rotates left, and when I press right it rotates right. If I release any of them, the ship stops rotating.

Now, my problem is, if I hold down left or right, and drag/move my finger off of the button and release my finger outside the button area, it never triggers the TOUCH_END, so the ship keeps rotating til I tap any of the directions.

How do I make it stop?

(using Flash CS5.5)

Here's the code:

var left:Boolean;

var right:Boolean;

Multitouch.inputMode=MultitouchInputMode.TOUCH_POINT;

Left.addEventListener(TouchEvent.TOUCH_BEGIN, leftBegin);

Left.addEventListener(TouchEvent.TOUCH_END, leftEnd);

Right.addEventListener(TouchEvent.TOUCH_BEGIN, rightBegin);

Right.addEventListener(TouchEvent.TOUCH_END, rightEnd);

function leftBegin(te:TouchEvent):void {

    left=true;

}

function leftEnd(te:TouchEvent):void {

    left=false;

}

function rightBegin(te:TouchEvent):void {

    right=true;

}

function rightEnd(te:TouchEvent):void {

    right=false;

}

addEventListener(Event.ENTER_FRAME,whatever);

function whatever(event:Event):void {

    if (left==true) {

        Ship.rotation-=2;

    }

    if (right==true) {

        Ship.rotation+=2;

    }

}

This topic has been closed for replies.
Correct answer

Hi TenchyMyo,

If the problem still is just to stop the rotation when the finger moves off the left / right button then you can listen for the following event :

  • TOUCH_ROLL_OUT: Indicates that a touch point has left an InteractiveObject

I think this should solve the problem you described in the very first post.

Just listen for 2 more event  listeners :

Left.addEventListener(TouchEvent.TOUCH_ROLL_OUT, leftEnd);

Right.addEventListener(TouchEvent.TOUCH_ROLL_OUT, rightEnd);

This events will be triggered as soon as you move your finger off the left/right buttons and eventually turn off the rotation.

Let me know how it goes.

Thanks,

Meet

Message was edited by: meetshah4288

4 replies

Correct answer
October 12, 2011

Hi TenchyMyo,

If the problem still is just to stop the rotation when the finger moves off the left / right button then you can listen for the following event :

  • TOUCH_ROLL_OUT: Indicates that a touch point has left an InteractiveObject

I think this should solve the problem you described in the very first post.

Just listen for 2 more event  listeners :

Left.addEventListener(TouchEvent.TOUCH_ROLL_OUT, leftEnd);

Right.addEventListener(TouchEvent.TOUCH_ROLL_OUT, rightEnd);

This events will be triggered as soon as you move your finger off the left/right buttons and eventually turn off the rotation.

Let me know how it goes.

Thanks,

Meet

Message was edited by: meetshah4288

TenchyMyoAuthor
Inspiring
October 12, 2011

Awesome, yeah, works now the way I want it. No need to use stage, now I can fire and turn at the same time, and release finger outside the buttons and it stops. Thanks.... good thing I don't need to use stage or event.stopImmediatePropagation().

Another question: left and right are next to each other, is it possible somehow to slide over to another and activate it? Like, I hold down left, ship rotates left, I slider my finger to right and it starts rotating right instead. I've tried a couple of things but didn't work... hmm...   too bad TOUCH_BEGIN doesn't trigger if I'm sliding into it from another place, like holding down outside the buttons in nowhere, sliding into left or right so it starts left or right. I tried holding down left and slide over to the right, having TOUCH_BEGIN on right to left=false... didn't work either.... oh well.. might figure something out eventually.... I'm persistent....

Sliding from somewhere else into/onto a button and releasing triggers TOUCH_END though, even though it didn't first start there.

Colin Holgate
Inspiring
October 12, 2011

From the sound of it, you should look at the TOUCH_ROLL_OVER event.

Inspiring
October 11, 2011

OK, maybe I misunderstood...cheers and good luck

Inspiring
October 11, 2011

This should work...

Left.addEventListener(TouchEvent.TOUCH_BEGIN, leftBegin);

Right.addEventListener(TouchEvent.TOUCH_BEGIN, rightBegin);

function leftBegin(te:TouchEvent):void {

    stage.addEventListener(TouchEvent.TOUCH_END, leftRightEnd);

    left=true;

}

function rightBegin(te:TouchEvent):void {

   stage.addEventListener(TouchEvent.TOUCH_END, leftRightEnd);

    right=true;

}

function leftRightEnd(te:TouchEvent):void {

   stage.removeEventListener(TouchEvent.TOUCH_END, leftRightEnd);

    right=false;

    left=false;

}

TenchyMyoAuthor
Inspiring
October 11, 2011

mola2alex wrote:

This should work...

Left.addEventListener(TouchEvent.TOUCH_BEGIN, leftBegin);

Right.addEventListener(TouchEvent.TOUCH_BEGIN, rightBegin);

function leftBegin(te:TouchEvent):void {

    stage.addEventListener(TouchEvent.TOUCH_END, leftRightEnd);

    left=true;

}

function rightBegin(te:TouchEvent):void {

   stage.addEventListener(TouchEvent.TOUCH_END, leftRightEnd);

    right=true;

}

function leftRightEnd(te:TouchEvent):void {

   stage.removeEventListener(TouchEvent.TOUCH_END, leftRightEnd);

    right=false;

    left=false;

}

Thanks, I prefer my code over yours, mine being:

Left.addEventListener(TouchEvent.TOUCH_BEGIN, leftBegin);

Right.addEventListener(TouchEvent.TOUCH_BEGIN, rightBegin);

stage.addEventListener(TouchEvent.TOUCH_END, leftRightEnd);

function leftBegin(te:TouchEvent):void {

    left=true;

}

function rightBegin(te:TouchEvent):void {

    right=true;

}

function leftRightEnd(te:TouchEvent):void {

    right=false;

    left=false;

}

As mine is shorter, easier to read and I prefer to have the EventListeners together. Yours have removing the listeners though, which I don't need, as I only add them once and never need to remove them again. Unless you can convince me somehow (as I'm still pretty new to AS3 and stuff, only done stuff in AS2 before and didn't use listeners then)

Inspiring
October 11, 2011

Your code preference is your decision.  The only issue with your code in my opinion is that your stage event always fires whenever the screen is touched even when you don't press the left or right button.  This could have undesirable effects if you add anything to that function 'leftRightEnd' in the future as any TOUCH_END on any part of the stage will cause it to execute.  My code only ads the listener when BEGIN (aka a button is pushed) and then removes itself when it ENDS.  It is all preference but I find the less events being listened to, the easier it is to troubleshoot when the UI gets complex.  Whether it will impact your application at the end of the day will depend.

Inspiring
October 11, 2011

Try this:

Don't add left and right TOUCH_END listeners and try the below.  Once you begin touching, you add an END to the stage so anywhere you release the touch it will fire.  When it fires, it executes the left=false and also cleans up the stage listener.  You would need something similar for right as well. 

function leftBegin(te:TouchEvent):void {

      left=true;

     stage.addEventListener(TouchEvent.TOUCH_END, leftEnd);

}

function leftEnd(te:TouchEvent):void {

        left=false;

        stage.removeEventListener(TouchEvent.TOUCH_END, leftEnd);

}

TenchyMyoAuthor
Inspiring
October 11, 2011

Thanks, but it still behaves the same way, never executing left=false when finger is released outside the button area, cuz it never executes leftEnd. As always, holding and releasing ON the buttons works.

So the problem is still the same, it doesn't execute TOUCH_END if the finger is released outside a button.

Colin Holgate
Inspiring
October 11, 2011

To work in the way you want it to work, you should add the touch end listener to the stage, not to the button.