Inherited a class from EventDispatcher in Flash but custom event not received

后端 未结 4 1879
一向
一向 2020-12-18 17:03

I have a custom event that is dispatched when a slider is moved but I receive no event from inherited dispatcher class I created whereas I followed the same syntax as soluti

相关标签:
4条回答
  • 2020-12-18 17:31

    I think your understanding of the event flow is a bit off. The answer I already gave in My flash custom event doesn't trigger (that I'm assuming you didn't read) I think is the proper way to use custom Event and IEventDispatcher objects:

    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);
    
            }// end function
        }// end class
    
    }// end package
    
    import fl.controls.Slider;
    import fl.events.SliderEvent;
    import flash.display.Sprite;
    
    internal class SliderSprite extends Sprite
    {
        private var _slider:Slider;
    
        public function SliderSprite()
        {
            init();
    
        }// end function
    
        private function init():void
        {
            _slider = new Slider();
            addChild(_slider);
    
            _slider.addEventListener(SliderEvent.CHANGE, onSliderChange);
            addEventListener(CustomEvent.CUSTOM_EVENT_TYPE, onSliderSpriteCustomEventType);
    
        }// end function
    
        private function onSliderSpriteCustomEventType(e:CustomEvent):void
        {
            trace(e.value);
    
        }// end function
    
        private function onSliderChange(e:SliderEvent):void
        {
            dispatchEvent(new CustomEvent(CustomEvent.CUSTOM_EVENT_TYPE, e.value));
    
        }// end function
    
    }// end class
    
    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
    

    [UPDATE] [EDITED 08/04/2011 08:22]

    I've modified the code to encapsulate all code involving dispatching and listening to/for events, as well as excuting event handlers, into another class(EventDispatcherManager).

    import flash.display.Sprite;
    import flash.events.IEventDispatcher;
    
    internal 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
    
    import fl.controls.Slider;
    import fl.events.SliderEvent;
    import flash.events.EventDispatcher;
    
    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
        {
            trace(e.value);
    
        }// end function
    
    }// end function
    
    0 讨论(0)
  • 2020-12-18 17:31

    shouldn't you be importing CustomEventDispatcher class in your test file

    0 讨论(0)
  • 2020-12-18 17:40

    Is hello being traced?

    It looks like maybe there is a vague reference as to which dispatcher is calling the disptachEvent() method. Try specifying slider.dispatchEvent() and use slider.addEventListener() to listen for your custom event.

    0 讨论(0)
  • 2020-12-18 17:49

    event.value doesn't exist
    try tracing event alone and see what you get
    Pretty sure you need something like event.target.value

    0 讨论(0)
提交回复
热议问题