Search code examples
actionscript-3buttonmobileair

ActionScript / AIR - One Button Limit (Exclusive Touch) For Mobile Devices?


Two years ago, when I was developing an application for the iPhone, I used the following built-in system method on all of my buttons:

[button setExclusiveTouch:YES];

Essentially, if you had many buttons on screen, this method insured that the application wouldn't be permitted do crazy things when several button events firing at the same time as any new button press would cancel all others.

problematic: ButtonA and ButtonB are available. Each button has a mouse up event which fire a specific animated (tweened) reorganization/layout of the UI. If both button's events are fired at the same time, their events will likely conflict, causing a strange new layout, perhaps a runtime error.

solution: Application buttons cancel any current pending mouse up events when said button enters mouse down.

private function mouseDownEventHandler(evt:MouseEvent):void
{
    //if other buttons are currently in a mouse down state ready to fire
    //a mouse up event, cancel them all here.
}

It's simple to manually handle this if there are only a few buttons on stage, but managing buttons becomes more and more complicated / bug-prone if there are several / many buttons available.

Is there a convenience method available in AIR specifically for this functionality?


Solution

  • I'm not aware of such thing.

    I guess your best bet would be creating your own Button class where you handle mouse down, set a static flag and prevent reaction if that flag has been already set up by other instance of the same class.

    In pseudo-code:

    class MyButton
    {
       private static var pressed : Boolean = false;
    
       function mouseDown(evt : MouseEvent)
       {
          if(!pressed)
          {
             pressed = true;
             // Do your thing
          }
       }
    }
    

    Just remember to set pressed to false on mouse up and you should be good to go.

    HTH,

    J