I am attempting to dispatch an event on the click of any of the buttons in a button array in the document class of my application. The intent is to have an external object receive this dispatch so as to remove a listener for the EnterFrame event in this class.
Here is the Main.as code (document class):
package {
import flash.display.*;
import flash.events.*;
public class Main extends Sprite {
private var one:MovieClip;
private var two:MovieClip;
private var three:MovieClip;
private var buttonArray:Array;
public function Main() {
// place buttons in an array
buttonArray = [one,two,three];
// add listeners to buttons
for (var i:int = 0; i < buttonArray.length; i++) {
buttonArray*.addEventListener(MouseEvent.CLICK, onClick);
}
}
private function onClick(e.MouseEvent):void {
// dispatch "buttonClick"
dispatchEvent(new Event("buttonClick"));
}
}
}
Question is how do I correctly add a listener to the Document class so as to receive the dispatch (hence removing the listener)?
package {
import flash.display.*;
import flash.events.*;
public class External extends Sprite {
private var buttonArray:Array;
public function External() {
// add listener
addEventListener(Event.ENTER_FRAME, followMouse);
// How do I access the document class from here?
Main(root).addEventListener("buttonClick", killEnterFrame);
}
private function followMouse(e:Event):void {
// mouse follow code
}
private function killEnterFrame():void {
removeEventListener(Event.ENTER_FRAME, followMouse);
}
}
}
Any help is greatly appreciated. Thanks.