Component class
package
{
import flash.display.Sprite;
import flash.events.Event;
public class Component extends Sprite implements IMenu
{
protected var numItems:Array;
protected var menuText:String;
protected var myURL:String;
/*
* To store the children of the submenu we use a Vector. We could have used
* an array but a Vector is more precise and compiled faster.
*/
private static var mySubArray:Vector.<Composite> = new Vector.<Composite>();
private static var previousNode:Component;
public function Component (myNum:Array=null, myText:String=null, _url:String=null)
{
numItems = myNum;
menuText = myText;
myURL = _url;
}
/*
* Common functions between Component and Composite
*/
public function getItems ():Array
{
return numItems;
}
public function getMenuText ():String
{
return menuText;
}
public function getMyURL ():String
{
return myURL;
}
/*
* The function when there are no children of the MenuItem.
*/
internal function showItem ():void
{
trace (myURL);
setParent (this);
}
/*
* When the MenuItem has children we add them to an array.
*/
internal function addItems (mySub:Composite):void
{
mySubArray.push (mySub);
setParent (this);
}
/*
* Function to remove all children.
*/
internal function deleteItems ():void
{
/*
* "previousNode" refers to the object pressed prior to the current object.
*/
if (previousNode != null)
{
if (mySubArray.length > 0)
{
for (var j:int = 0; j < mySubArray.length; j++)
{
previousNode.removeChild (mySubArray[j]);
}
mySubArray = new Vector.<Composite>();
}
}
}
/*
* Defines the previous object.
*/
private function setParent (c:Component):void
{
previousNode = c;
}
}
}