The first part--using Flash CS4 to create an AIR submenu for the local menu--the second part of the Essential Guide to Flash CS4 Air development Highlight.
Part II-Create an AIR local menu using Flash CS4-the first part of the Essential Guide to Flash CS4 Air development Highlight.
Next, you will apply the menuitemselected () method, which will start each time the user selects an item on the context menu. When a selection event occurs, the event listener method receives an instance of the Flash.events.Event class as a parameter. The selection event is passed to the root element through the selected element on the menu, through the hierarchy of the menu.
The object received as a parameter has the following two properties: Target Currenttarget
If the event has been registered directly on the element of the menu (for example in this example), then the two properties will have the same value. In this example, two properties refer to instances of the selected Nativemenuitem class. However, if the event is registered on a root menu or on a root of a submenu, the target property of the received event object will always refer to the selected element. The Currenttarget property will always reference the object registered by the event.
When an item is selected, the menuitemselected () method writes the label of the selected element to Textarea,textarea as a text output console. Here is the code:
Called on click on menu items
Private Function menuitemselected (evt:event): void
{
Access Nativemenuitem Instance selected
var item:nativemenuitem = evt.target as Nativemenuitem;
Write in the textarea selected item ' s label
Output.appendtext ("CLICKED on:" + Item.label + file.lineending);
}
Displaying the code for the native submenus
The the complete Ch06p01.as class, handles with the Menuitemselected () method:
Package Com.comtaste.foed.essentialair.chapter6
{
Import Fl.controls.Button;
Import Fl.controls.TextArea;
Import Flash.display.MovieClip;
Import Flash.display.NativeMenu;
Import Flash.display.NativeMenuItem;
Import flash.events.Event;
Import Flash.filesystem.File;
public class Ch06p01 extends MovieClip
{
Onstage components
public Var Button:button;
public Var Output:textarea;
Class properties
private Var Menuroot:nativemenu;
Public Function Ch06p01 ()
{
Super ();
Generate native menu to use
Createnativemenu ();
Assign menu to right-click on button
Button.contextmenu = Menuroot;
}
Create a complete native menu
Private Function Createnativemenu (): void
{
Instantiate main Menu Object
Menuroot = new Nativemenu ();
Append submenus to Menu root
Menuroot.additem (Createfirstsubmenu ());
Menuroot.additem (Createsecondsubmenu ());
}
Private Function Createfirstsubmenu (): Nativemenuitem
{
Create First submenu
var Submenu:nativemenuitem =
New Nativemenuitem ("My First Custom submenu");
Initialize child container
Submenu.submenu = new Nativemenu ();
Create First submenu child
var Submenuitem1:nativemenuitem =
New Nativemenuitem ("Menu 1 Item 1");
Register event Listener for menu item
Submenuitem1.addeventlistener (Event.select,
menuitemselected);
Add Item to submenu
SubMenu.submenu.addItem (SUBMENUITEM1);
Create a second child, register event listener for
Selection event and assign to submenu
var Submenuitem2:nativemenuitem =
New Nativemenuitem ("Menu 1 Item 2");
Submenuitem2.addeventlistener (Event.select,
menuitemselected);
SubMenu.submenu.addItem (SUBMENUITEM2);
Return submenu;
}
Private Function Createsecondsubmenu (): Nativemenuitem
{
Create First submenu
var Submenu:nativemenuitem =
New Nativemenuitem ("Second submenu");
Initialize child container
Submenu.submenu = new Nativemenu ();
Create First submenu child
var Submenuitem1:nativemenuitem =
New Nativemenuitem ("Menu 2 Item 1");
Register event Listener for menu item
Submenuitem1.addeventlistener (Event.select,
menuitemselected);
Add Item to submenu
SubMenu.submenu.addItem (SUBMENUITEM1);
Add a separator item
Label would be ignored for separator items
var Submenuseparator:nativemenuitem =
New Nativemenuitem ("", true);
Add Separator to Menu
SubMenu.submenu.addItem (Submenuseparator);
Create a second child, register event listener for
Selection event and assign to submenu
var Submenuitem2:nativemenuitem =
New Nativemenuitem ("Menu 2 Item 2");
Submenuitem2.addeventlistener (Event.select,
menuitemselected);
SubMenu.submenu.addItem (SUBMENUITEM2);
Create a new item as an internal submenu
Using Addsubmenu command
var Childsubmenu:nativemenuitem =
SubMenu.submenu.addSubmenu (New Nativemenu (),
"Nested menu");
Initialize child container
Childsubmenu.submenu = new Nativemenu ();
Create a child, register event listener for
Selection event and assign to internal submenu
var Submenuitem3:nativemenuitem =
New Nativemenuitem ("Menu 2 nested Item 1");
Submenuitem3.addeventlistener (Event.select, menuitemselected);
ChildSubMenu.submenu.addItem (SUBMENUITEM3);
Return submenu;
}
Called on Click
Private Function menuitemselected (evt:event): void
{
var item:nativemenuitem = evt.target as Nativemenuitem;
Output.appendtext ("CLICKED on:"
+ Item.label + file.lineending);
}
}//Close class
}//Close Package
Test local Menu
Now it's time to test the application. To test the program, return to Flash Ch06p01.fla. Project Then, from the Flash CS4 Controls menu, select the Test Movie command to run the application. After compiling and executing, right-click the button in the Center of the window (using Ctrl-click on MAC OS X system). The local menu you prepared will appear. You can view the application in Figure 6-4.
The CH06P01.FLA project in Flash
Each time you select an item from the context menu, the label for the selected element is displayed in TextArea. In the next section, you'll learn how to use the window-level and application-level menus in a real-world application.
Original: http://flex.sys-con.com/node/850137