Graphics Reference
In-Depth Information
And now, without further ado, the much talked about
create
MenuItem
method! This little chunk of code is responsible for creating
each menu item and adding it to the stage. If you remember in the
class constructor, we called this method with each item in the
menuArray
. The three parameters in this method are the parameters
that get passed when you use the
forEach
method from the Array
class. They are the value of the item, the index of that item in its
containing Array, and the containing Array object itself. Inside
createMenuItem
, the first thing that happens is the instantiation and
namingofanewSprite,whichwillbethenewmenuitem.Inorder
to access the menu item later (e.g., from the
assignAction
method),
the menu item is named by appending its index in the array to the
string
“
menuItem
,
”
so we end up with
menuItem0
,
menuItem1
,andso
on. Next, we
ll set the
mouseChildren
property to false, so no
children within this menu item will be able to take over the focus of
the mouse. After that comes setting the
buttonMode
to true, so the
menu item will show the hand cursor and will accept a click event if
the space bar or Enter key are pressed while it has focus.
After creating the new menu item, I move on to create its label
as a new TextField. I won
'
t go through each line of this process in
great detail, but you can see that the properties being set are
name
,
autoSize
(set to
“
left
”
, which essentially means the text is left
aligned),
antiAliasType, selectable,
and
text
.Also,weruna
setTextFormat
call to set the initial formatting of the label. From
here, we call a couple of other methods for the look, interactivity,
and location of the menu item being created (those are up next).
Finally, the label is added to the menu item, and the menu item is
added to the SimpleMenu.
'
private function createMenuItem(itemLabel:String,
index:int, array:Array):void {
_menuItem = new Sprite();
_menuItem.name =
"
menuItem
"
+ index;
_menuItem.mouseChildren = false;
_menuItem.buttonMode = true;
_menuItemLabel = new TextField();
_menuItemLabel.name =
"
itemText
"
;
_menuItemLabel.autoSize =
"
left
"
;
_menuItemLabel.antiAliasType =
"
advanced
"
;
_menuItemLabel.selectable = false;
_menuItemLabel.text = itemLabel;
_menuItemLabel.setTextFormat(_labelFormatUp);
drawItemBackground(_menuItem, _upBackground,
_backgroundUpAlpha, _menuItemLabel.width,
_menuItemLabel.height);
addItemListeners(_menuItem);
placeItem(_menuItem, index);