I dispatch a custom event from an event dispatcher (following solution Inherited a class from EventDispatcher in Flash but custom event not received) and I add a listener in both dispatcher itself and in main. trace shows that only dispatcher receives this event not main. How to make main also receive the event ?
package
{
import flash.display.Sprite;
import flash.events.Event;
public class main extends Sprite
{
private var _sliderSprite:SliderSprite;
public function main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
_sliderSprite = new SliderSprite();
_sliderSprite.x = (stage.stageWidth / 2);
_sliderSprite.y = (stage.stageHeight / 2);
addChild(_sliderSprite);
this.addEventListener(CustomEvent.CUSTOM_EVENT_TYPE, onCustomEventType);
}// end function
private function onCustomEventType(e:CustomEvent):void
{
// never triggered
trace("hello");
}// end function
}// end class
}// end package
class 2
package {
import flash.display.Sprite;
import flash.events.IEventDispatcher;
import fl.controls.Slider;
public class SliderSprite extends Sprite
{
private var _slider:Slider;
private var _eventDispatcherManager:EventDispatcherManager;
public function SliderSprite()
{
init();
}// end function
private function init():void
{
_slider = new Slider();
addChild(_slider);
_eventDispatcherManager = new EventDispatcherManager(IEventDispatcher(_slider));
}// end function
}// end class
}
class 3
package {
import fl.controls.Slider;
import fl.events.SliderEvent;
import flash.events.*;
internal class EventDispatcherManager extends EventDispatcher
{
public function EventDispatcherManager(slider:IEventDispatcher)
{
slider.addEventListener(SliderEvent.CHANGE, onSliderChange);
this.addEventListener(CustomEvent.CUSTOM_EVENT_TYPE, onCustomEventType);
}// end function
private function onSliderChange(e:SliderEvent):void
{
this.dispatchEvent(new CustomEvent(CustomEvent.CUSTOM_EVENT_TYPE, e.value));
}// end function
private function onCustomEventType(e:CustomEvent):void
{
// triggered
trace(e.value);
}// end function
}// end function
}
class 4
package {
import flash.events.Event;
internal class CustomEvent extends Event
{
public static const CUSTOM_EVENT_TYPE:String = "customEventType";
private var _value:Number;
public function get value():Number
{
return _value;
}// end function
public function CustomEvent(type:String,
value:Number,
bubbles:Boolean = false,
cancelable:Boolean = false)
{
_value = value;
super(type, bubbles, cancelable);
}// end function
override public function clone():Event
{
return new CustomEvent(type, value, bubbles, cancelable);
}// end function
}// end class
}
In order for your instance of the main class to recieve events from the EventDispatcherManager, you have to register main as a listener with the EventDispatcherManager.
Something like:
But for that to work you would have to either set the _eventDispatcherManager propterty as public och make it accessible through an accessor, or a simple get-function.
Though I would strongly recommend you to look over the structure. By the look of it you could skip the EventDispatcherManager class and the CustomEvent class, and just dispath the value directly from the SliderEvent to Main.
But this is maybe just a Demo example?
Edit:
This is what a getter function would look like:
Put that function in your SliderSprite class and call it from Main just as I described above.