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
Leave a Reply Cancel reply
Recent Posts
- Getting started with the TLFTextField class in ActionScript 3.0 and Flash CS5
- Adding tick marks to a Slider control in Flash using ActionScript 3.0
- Creating a vertical Slider control in Flash using ActionScript 3.0
- Creating a custom context menu item in Flash using ActionScript 3.0
- Rotating a Sprite object around its x-axis in Flash using ActionScript 3.0 and Flash Player 10
- Detecting when the user changes the color in a ColorPicker control in Flash using ActionScript 3.0
- Getting the currently selected color as a hexadecimal value on a ColorPicker control in Flash using ActionScript 3.0
- Toggling the text field on the ColorPicker control in Flash using ActionScript 3.0
- Creating a vertical Slider control in Flash using ActionScript 3.0
- Setting the number of columns on a ColorPicker control in Flash using ActionScript 3.0
Categories
- Bitmap (1)
- Components (72)
- Button (19)
- CheckBox (2)
- ColorPicker (6)
- ComboBox (1)
- DataGrid (8)
- FLVPlayback (7)
- Label (9)
- ProgressBar (2)
- Slider (3)
- TextArea (1)
- TextInput (7)
- UILoader (7)
- ContextMenu (1)
- Embed (4)
- ExternalInterface (2)
- Flex (7)
- Font (2)
- General (5)
- Graphics (2)
- JSFL (14)
- Loader (3)
- LoadVars (3)
- Microphone (1)
- migration (12)
- MovieClip (1)
- MovieClipLoader (1)
- Sound (2)
- TextField (1)
- TLFTextField (1)
- TransitionManager (1)
- Tween (1)
- Uncategorized (1)
- URLLoader (4)
- URLVariables (1)
- Video (1)
- XML (2)
Advertising
" RT @OReillyMedia: #Ebook Deal/Day: Learning JavaScript Design Patterns -
$11.99
(Save 50%)
http://t.co/ilcmSDv6 " — pdehaan


When I try this code, I get a compile-time error for line 24:
var btn:Button = event.currentTarget as Button;The error is:
I am cs4 windows. What am I doing wrong?
Jimbo,
Sorry, it was a typo in my code. I corrected the entry above, but the revised could is:
Line 2 should have said
evt.targetinstead ofevent.target.Thanks,
Peter
I believe there is also a ‘mouseEnabled’ property you can use to enable and disable buttons.
Yes you can in CS4.. say we have button btnPlay then to disable it…
btnPlay.mouseEnabled = false;
and to enable it..
btnPlay.mouseEnabled = true;