The following example creates a listener for the click event on a Button instance called myButton.
Full code after the jump.
You can detect when a Button instance is pressed by listening for its click event (using the flash.events.MouseEvent.CLICK constant) or its change event (using the flash.events.Event.CHANGE constant) to be dispatched. Using ActionScript 3.0, you listen for events using the addEventListener() method. For more information on handling events in ActionScript 3.0, see the “Event handling” Quick Start article.
Note: ActionScript 2.0 provided three event handling models. ActionScript 3.0 simplifies the process by using a single event handling model. Also, ActionScript 2.0 didn’t use constants for event names but ActionScript 3.0 does.
// ActionScript 3.0 // Requires a Button component instance on the Stage with an instance name of "myButton". myButton.addEventListener(MouseEvent.CLICK, clickHandler); function clickHandler(evt:MouseEvent):void { trace("clicked"); }
The following example uses an event listener to detect when the button’s click event has been dispatched, and updates the label accordingly:
// 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 myButton:Button = new Button(); myButton.label = "Click me"; myButton.move(10, 10); myButton.addEventListener(MouseEvent.CLICK, clickHandler); addChild(myButton); /* Handler function for the Button component instance. This function updates the button's label property when the button is clicked. */ function clickHandler(evt:MouseEvent):void { myButton.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.
One Response to Detecting when a button is pressed 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


I would like to make a gif in the head image of a site play each time
the play buton of a you tube or vimio video is played in the page… how can I detect the click of the play button and cause it to play the gif?