In AS3.0 the correct way to do it is to replicate what we did for the stage object with the difference that this time we are attaching the listener to a movie clip.
function myButtonAction(eventObject:MouseEvent) { trace("you clicked on the button "); } myButton.addEventListener(MouseEvent.CLICK, myButtonAction); |
For example you can access the type property of the eventObject using the syntax below:
function myButtonAction(eventObject:MouseEvent) { trace(eventObject.type); // output " click" } myButton.addEventListener(MouseEvent.CLICK, myButtonAction); |
You can see the code below:
// we hide the submenu items, notice how in AS 3.0 the underscore in front of the visible property is gone
mainItem.subItem1.visible = false;
mainItem.subItem2.visible = false;
mainItem.subItem3.visible = false;
// we attach a mouse over, mouse out and click event to the mainItem button
mainItem.addEventListener(MouseEvent.MOUSE_OVER,showMenu);
mainItem.addEventListener(MouseEvent.MOUSE_OUT,showMenu);
mainItem.addEventListener(MouseEvent.CLICK,buttonClicked);
// here we create a little loop to speed up the process of creating event listener for the submenu buttons nested inside the main button
for(var i=1;i<=3;i++){
mainItem["subItem"+i].addEventListener(MouseEvent.MOUSE_OVER,showSubMenu);
mainItem["subItem"+i].addEventListener(MouseEvent.MOUSE_OUT,showSubMenu);
}
// this function is called when the mouse roll over the main menu item
function showMenu(e:MouseEvent){
// we detect the type of event which is either a mouse over or a mouse out
if(e.type == "mouseOut"){
// if it is a mouse out we hide all the sub menus
e.currentTarget.subItem1.visible = false;
e.currentTarget.subItem2.visible = false;
e.currentTarget.subItem3.visible = false;
}
// if it is a mouse overwe show all the sub menus
if(e.type == "mouseOver"){
e.currentTarget.subItem1.visible = true;
e.currentTarget.subItem2.visible = true;
e.currentTarget.subItem3.visible = true;
}
}
// the currentTarget property above refers to the object that registered the listener
// the target property we use in the last function can refer to the object that registered the listener but also to any of the objects that are registering the event
function showSubMenu(e:MouseEvent){
if(e.type == "mouseOut"){
e.currentTarget.gotoAndStop(1);
}
if(e.type == "mouseOver"){
e.currentTarget.gotoAndStop(2);
}
}
function buttonClicked(e:MouseEvent){
itemClicked.text = e.target.name;
}