User Guide

Creating an application with the Menu component (Flash Professional only) 893
4. In the Actions panel, on the first frame, enter the following code to add an event listener
to listen for
click events on the button. The code also listens for a change event on the
menu and displays the name of the selected menu item in the Output panel:
/**
Requires:
- Menu component in library
- Button component in library
*/
import mx.controls.Button;
import mx.controls.Menu;
this.createClassObject(Button, "menu_button", 10, {label:"Launch
Menu"});
// Create a menu.
var my_menu:Menu = Menu.createMenu();
// Add some menu items.
my_menu.addMenuItem("Open");
my_menu.addMenuItem("Close");
my_menu.addMenuItem("Save");
my_menu.addMenuItem("Revert");
// Add a change-listener to Menu to detect which menu item is selected.
var menuListener:Object = new Object();
menuListener.change = function(evt_obj:Object) {
var item_obj:Object = evt_obj.menuItem;
trace("Item selected: "+item_obj.attributes.label);
};
my_menu.addEventListener("change", menuListener);
// Add a button listener that displays the menu when the button is
clicked.
var buttonListener:Object = new Object();
buttonListener.click = function(evt_obj:Object) {
var my_button:Button = evt_obj.target;
// Display the menu at the bottom of the button.
my_menu.show(my_button.x, my_button.y + my_button.height);
};
menu_button.addEventListener("click", buttonListener);
5.
Select Control > Test Movie.
Click the Launch Menu button to display the menu. When you select a menu item, a
trace() statement reports the selection in the Output panel.