# Wrap my head around Dispatching a custom event but listening for it in another class

**URL:** <https://forum.kirupa.com/t/wrap-my-head-around-dispatching-a-custom-event-but-listening-for-it-in-another-class/327480>\
**Category:** Uncategorized\
**Created:** [March 3, 2012, 4:11pm UTC](https://forum.kirupa.com/t/wrap-my-head-around-dispatching-a-custom-event-but-listening-for-it-in-another-class/327480 "2012-03-03T16:11:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![icekomo](https://avatars.discourse-cdn.com/v4/letter/i/bcef8e/32.png) [@icekomo](https://forum.kirupa.com/u/icekomo)\
**Post date:** [March 3, 2012, 4:11pm UTC](https://forum.kirupa.com/t/wrap-my-head-around-dispatching-a-custom-event-but-listening-for-it-in-another-class/327480/1 "2012-03-03T16:11:59Z")

</div>

So I am able to dispatch the event from one class and trace out that the custom event was dispatched, but where I am having trouble is listening for that event in another class.  
Here is what I have:

My class that is dispatching the event:  
(I have set the code where things are getting dispatched in bold and green font color)  
package com.iOS  
{  
import flash.display.Stage;  
import flash.events.StageOrientationEvent;  
import flash.display.StageOrientation;  
import flash.display.StageScaleMode;  
import flash.display.StageAlign;  
import flash.display.Sprite;  
import flash.events.Event;

```
public class StageRotation extends Sprite
{

	public function StageRotation()
	{
		addEventListener(Event.ADDED_TO_STAGE, init);
	}

	private function onChange(event:StageOrientationEvent):void
	{
		switch (event.afterOrientation)
		{
			case StageOrientation.DEFAULT :
				//infoText.text = "Default view";
				[COLOR=#008000]**dispatchEvent(new CustomEvent(CustomEvent.FLIP_VERT, true));**[/COLOR]
				break;
			case StageOrientation.ROTATED_LEFT :
				//infoText.text = "Rotate Left";
				[COLOR=#008000]**dispatchEvent(new CustomEvent(CustomEvent.FLIP_HORT, true));**[/COLOR]
				break;
			case StageOrientation.ROTATED_RIGHT :
				//infoText.text = "Rotate Right";
				[COLOR=#008000]**dispatchEvent(new CustomEvent(CustomEvent.FLIP_HORT, true));**[/COLOR]
				break;
			case StageOrientation.UPSIDE_DOWN :
				//infoText.text = "Upside Down";
				[COLOR=#008000]**dispatchEvent(new CustomEvent(CustomEvent.FLIP_VERT, true));**[/COLOR]
				break;
		}
	}
	private function init(event:Event):void
	{
		stage.scaleMode = StageScaleMode.NO_SCALE;
		stage.align = StageAlign.TOP_LEFT;
		stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGE, onChange);
	}
}

```

}

This is my custom event class that extends the event class:  
package com.iOS  
{  
import flash.events.Event;

```
public class CustomEvent extends Event
{
	public static const ARROW_CLICK = "Arrow_Clicked";
	public static const REMOVE_FLIP = "Remove_FlipMenu";
	public static const FLIP_CLICKED = "flip_Clicked";
	**[COLOR=#008000]public static const FLIP_HORT = "Flip_Horizontal";[/COLOR]**

```

**[COLOR=#008000] public static const FLIP\_VERT = “Flip\_Verticle”;[/COLOR]**

```
	public function CustomEvent(type:String, bubbles:Boolean = true)
	{
		trace("event dispatched");
		super(type,bubbles);
	}
	
	public override function clone():Event
	{
		return new CustomEvent(type,bubbles);
	}
}

```

}

This is the class that I am trying to listen for the custom event when fired.  
package com.iOS. When i add “stage” in front of my listeners it works, but I don’t think I should have to do that, I thought that just listening for them would be enough, because they are bubbling up.  
{  
import flash.events.Event;  
import flash.display.MovieClip;  
import com.GdovinDesigns.CustomEvent;

```
public class YellowTube extends MovieClip
{
	private var landscapeGroundY:Number;
	private var landscapeGroundX:Number;

	public function YellowTube()
	{
		addEventListener(Event.ADDED_TO_STAGE, init);
	}

	private function positionTube():void
	{
		trace(landscapeGroundY);
		this.y = landscapeGroundY;
		this.x = landscapeGroundX;
	}

	private function landscapeMode(event:CustomEvent):void
	{
		this.x = 300;
	}

	private function portraitMode(event:CustomEvent):void
	{
		this.x = 100;
	}

	private function init(event:Event):void
	{
		trace(stage.stageHeight);
		landscapeGroundY = stage.stageHeight - this.height - 100;
		landscapeGroundX = stage.stageWidth - this.width - 100;
		[COLOR=#008000]**addEventListener(CustomEvent.FLIP_HORT, landscapeMode);**[/COLOR]

```

[COLOR=#008000]\*\* addEventListener(CustomEvent.FLIP\_VERT, portraitMode);\*\*[/COLOR]  
positionTube();  
}  
}  
}

Any help on this would be great, I spent the morning googling this and reading through posts, but it seems that most were set up in a way to dispatch and listen for in the class class.

Thanks
