Flash CS3 Documentation |
|||
| Using ActionScript 3.0 Components > Using the UI Components > Using the Button > Creating an application with the Button | |||
The following procedure explains how to add a Button component to an application while authoring. In this example, the Button changes the state of a ColorPicker component when you click it.
aCp.visible = false;
aButton.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(event:MouseEvent):void {
switch(event.currentTarget.label) {
case "Show":
aCp.visible = true;
aButton.label = "Disable";
break;
case "Disable":
aCp.enabled = false;
aButton.label = "Enable";
break;
case "Enable":
aCp.enabled = true;
aButton.label = "Hide";
break;
case "Hide":
aCp.visible = false;
aButton.label = "Show";
break;
}
}
The second line of code registers the function clickHandler() as the event handler function for the MouseEvent.CLICK event. The event occurs when a user clicks the Button, causing the clickHandler() function to take one of the following actions depending on the Button's value:
The following procedure creates a toggle Button using ActionScript and displays the event type in the Output panel when you click the Button. The example creates the Button instance by invoking the class's constructor and it adds it to the Stage by calling the addChild() method.
This adds the component to the library, but doesn't make it visible in the application.
import fl.controls.Button; var aButton:Button = new Button(); addChild(aButton); aButton.label = "Click me"; aButton.toggle = true; aButton.move(50, 50);
The move() method positions the button at location 50 (x coordinate), 50 (y coordinate) on the Stage.
aButton.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(event:MouseEvent):void {
trace("Event type: " + event.type);
}
When you click the button, Flash displays the message, "Event type: click" in the Output panel.
Flash CS3
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flash/9.0/main/00000423.html