(Flash ActionScript 3)  By Tony Pan

There were times that we’ll have to assign mouse event to number of buttons or movieclips on the stage, ie.  navigation elements.  It is perfectly fine to implement the addEventListener individually to each of these buttons and movieclips.   But what if there are a large number of these buttons, and needed to use on few different individual swf files.   Here is the example code for importing  the same class file and create an storage array for the buttons and movieclips.   Then called the class function passing the storage array.

Sample Screen: (Roll mouse cursor over and click on them)

Code on the FLA file (buttons.fla):

import com.buttonEvent;
var addButtonEvent:buttonEvent = new buttonEvent(this);
var buttonArray:Array = new Array();
buttonArray = [button1, button2, button3, button4, button5, button6];
addButtonEvent.iniButtons(buttonArray);

Code on the package file (buttonEvnet.as):


package com{
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.display.InteractiveObject;
import flash.display.DisplayObjectContainer;
import flash.external.ExternalInterface;
import flash.display.*;
import flash.text.*;
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;

public class buttonEvent extends MovieClip {
private var Navbuttons:Array = new Array();
public var _MC:MovieClip;
private var wClicked:Object;
public function buttonEvent(mc:MovieClip):void {
_MC = mc;// stage / timeline reference
}
public function iniButtons(theButtons:Array):void {
Navbuttons = theButtons;
addListeners();
}
private function addListeners():void {
for (var i:int = 0; i < Navbuttons.length; i++) {
var className:String = flash.utils.getQualifiedClassName(Navbuttons[i]);
var objectClass:Class = flash.utils.getDefinitionByName(className) as Class;

if (Navbuttons[i] is MovieClip) { Navbuttons[i].buttonMode = true; }
Navbuttons[i].addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
Navbuttons[i].addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
Navbuttons[i].addEventListener(MouseEvent.CLICK, mouseClickHandler);
}
}
private function mouseOverHandler(event:MouseEvent):void {
var rollOverObject:InteractiveObject = InteractiveObject(event.currentTarget);
wClicked = rollOverObject;
if (wClicked is MovieClip) {
wClicked.gotoAndStop(“over”);
}
try {
_MC.doMouseAction(wClicked.name, event.type);
} catch (error:TypeError) {

trace(“TypeError catch: ” + error);                                                                                                                                } catch (error:Error) {
trace(“Error catch: ” + error);

} finally {
trace(“success!”);
}
}
private function mouseOutHandler(event:MouseEvent):void {
trace(event.type);
var rollOverObject:InteractiveObject = InteractiveObject(event.currentTarget);
wClicked = rollOverObject;
// when mouse cursor over a movie clip, we tell it to go the frame labled “up”, it is good practice to standardize the lable for each mouse state on movieclips used as hotspot or buttons.
if (wClicked is MovieClip) {
wClicked.gotoAndStop(“up”);
}
try {
_MC.doMouseAction(wClicked.name, event.type);
} catch (error:TypeError) {
trace(“TypeError catch: ” + error);

} catch (error:Error) {
trace(“Error catch: ” + error);

} finally {
trace(“success!”);
}
}

private function mouseClickHandler(event:MouseEvent):void {
trace(event.type);
var rollOverObject:InteractiveObject = InteractiveObject(event.currentTarget);
wClicked = rollOverObject;
// when mouse cursor over a movie clip, we tell it to go the frame labled “down”, it is good practice to standardize the lable for each mouse state on movieclips used as hotspot or buttons.
if (wClicked is MovieClip) {
wClicked.gotoAndStop(“down”);
}
try {
_MC.doMouseAction(wClicked.name, event.type);
} catch (error:TypeError) {
trace(“TypeError catch: ” + error);

} catch (error:Error) {
trace(“Error catch: ” + error);

} finally {
trace(“success!”);
}

}
public function removeLen(wAction:String,wItem:Object ):void {
switch (wAction) {
case “ALL” :
for (var i:int = 1; i < Navbuttons.length; i++) {
Navbuttons[i].removeEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
Navbuttons[i].removeEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
}
break;
case “Single” :
wItem.removeEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
wItem.removeEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
break;
}
}
}
}

The sample code  can be downloaded as the zip file here: buttonevent