There are many cases where you might want to disable a button in your application. For example, you might want to prevent a user from hitting the Submit button twice, or you might want to disable a Submit button until the user first checks a check box.

The following example creates two Button instances. The first instance is enabled and allows the user to click on the button. The second instance has its enabled property set to false which prevents the user from interacting with the component and dispatching its click event.

Full code after the jump.

// ActionScript 3.0
// Import the required component classes.  
import fl.controls.Button;
 
// Create a new Button component instance and add it to the display list.
var enabledButton:Button = new Button();
enabledButton.label = "enabled = true";
enabledButton.move(10, 10);
enabledButton.addEventListener(MouseEvent.CLICK, clickHandler);
addChild(enabledButton);
 
/* Create a new Button component instance, set the enabled property to false, 
   and add it to the display list. */
var disabledButton:Button = new Button();
disabledButton.enabled = false;
disabledButton.label = "enabled = false";
disabledButton.move(120, 10);
disabledButton.addEventListener(MouseEvent.CLICK, clickHandler);
addChild(disabledButton);
 
/* Handler function for the enabledButton and disabledButton button instances. This
   function updates the target button's label property when the button is clicked. */
function clickHandler(evt:MouseEvent):void {
    var btn:Button = evt.currentTarget as Button;
    btn.label = "Ouch!";
}

For more information on the Flash/ActionScript 3.0 Button component, see the “Using the Button component” Flash Quick Start on Adobe.com.

 

4 Responses to Enabling and disabling Button instances in Flash with ActionScript 3.0

  1. Jimbo says:

    When I try this code, I get a compile-time error for line 24: var btn:Button = event.currentTarget as Button;

    The error is:

    1120:Access of undefined property event.

    I am cs4 windows. What am I doing wrong?

  2. Peter deHaan says:

    Jimbo,

    Sorry, it was a typo in my code. I corrected the entry above, but the revised could is:

    1
    2
    3
    4
    
    function clickHandler(evt:MouseEvent):void {
        var btn:Button = evt.currentTarget as Button;
        btn.label = "Ouch!";
    }

    Line 2 should have said evt.target instead of event.target.

    Thanks,
    Peter

  3. ilike2flash says:

    I believe there is also a ‘mouseEnabled’ property you can use to enable and disable buttons.

  4. flashCS4 says:

    Yes you can in CS4.. say we have button btnPlay then to disable it…
    btnPlay.mouseEnabled = false;

    and to enable it..
    btnPlay.mouseEnabled = true;

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Spam Protection by WP-SpamFree