# Dispatching and receiving events

**URL:** https://forum.kirupa.com/t/dispatching-and-receiving-events/310938
**Category:** flash
**Created:** [June 23, 2010, 3:27pm UTC](https://forum.kirupa.com/t/dispatching-and-receiving-events/310938 "2010-06-23T15:27:40Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![wolcotter](https://avatars.discourse-cdn.com/v4/letter/w/6bbea6/32.png) [@wolcotter](https://forum.kirupa.com/u/wolcotter)
#### Post date: [June 23, 2010, 3:27pm UTC](https://forum.kirupa.com/t/dispatching-and-receiving-events/310938/1 "2010-06-23T15:27:40Z")

</div>

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):

```auto
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)?

```auto
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.
