Graphics Reference
In-Depth Information
_menuItem.addChild(_menuItemLabel);
addChild(_menuItem);
}
s see those methods that are called at the end of
the createMenuItem method. The first two we
So now let
'
ll look at together are
drawItemBackground and addItemListeners. drawItemBackground uses
the graphics property of the menu item passed in (via the item
parameter) to draw a rectangle with the color, alpha, and size that are
also passed in through the method
'
s parameters. The first thing it
does is clear the graphics in case there was anything drawn in the
menu item prior to this call. Once things are cleared out, the begin
Fill method is called. This is where the background color and alpha
values come in. With the fill color and alpha set, all that
'
s left to do in
this method is to call the graphics.drawRect method. The next
method, addItemListeners , simply adds MouseEvent listeners to the
menu item, so its look can be changed with the alterState method
(coming up after we look at the placeItem method).
'
private function
drawItemBackground(item:Sprite,color:uint,alpha:Number,
w:Number,h:Number):void {
item.graphics.clear();
item.graphics.beginFill(color, alpha);
item.graphics.drawRect(0, 0, w, h);
}
private function addItemListeners(item:Sprite):void {
item.addEventListener(MouseEvent.MOUSE_OVER,
alterState, false, 0, true);
item.addEventListener(MouseEvent.MOUSE_DOWN,
alterState, false, 0, true);
item.addEventListener(MouseEvent.MOUSE_UP, alterState,
false, 0, true);
item.addEventListener(MouseEvent.MOUSE_OUT, alterState,
false, 0, true);
}
The last method that was called from within createMenuItem was
placeItem , and it
re going to look at. The two
parameters this method is expectingare,ofcourse,theitemthat
needs to be placed (itemToPlace) and the index of that item. The
very first thing we do here is check to see if index is equal to 0. If
itis,wecanjumprightbackoutofthismethodbecausethere
'
s the next method we
'
'
s
nothing we need to do since the very first item in a SimpleMenu
gets placed at the x,y position of (0,0) within the SimpleMenu itself.
Once index is anything other than 0, the code can continue on to
thenextlinewhereweneedtoget a reference to the previously
created menu item (lastItem) . This is the second time the index
parameter comes in handy because we can check one position
Search WWH ::




Custom Search