为什么我只在EventDispatcher中接收自定义事件而不在main中?

时间:2011-04-08 18:57:08

标签: flash actionscript-3

我从事件调度程序(解决方案Inherited a class from EventDispatcher in Flash but custom event not received)调度自定义事件,并在调度程序本身和main中添加一个侦听器。跟踪显示只有调度程序才接收此事件而不是主要事件。如何使主要也接收事件?

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

第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

}

第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

}

第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

}

2 个答案:

答案 0 :(得分:3)

为了让您的主类实例从EventDispatcherManager接收事件,您必须使用EventDispatcherManager将main注册为侦听器。

类似的东西:

//in main:
sliderSprite.eventDispatcherManager.addEventListener(CustomEvent.CUSTOM_EVENT_TYPE,onCustomEvent);

但为了工作,你必须将_eventDispatcherManager属性设置为public och,使其可以通过访问器或简单的get函数访问。

虽然我强烈建议你查看结构。通过它看,您可以跳过EventDispatcherManager类和CustomEvent类,并直接将值从SliderEvent调度到Main。

但这可能只是一个演示示例?

编辑:

这就是getter函数的样子:

function get eventDispatcherManager():EventDispatcherManager {
       return _eventDispatcherManager;
}

将该函数放入SliderSprite类中,并按照上面的描述从Main调用它。

答案 1 :(得分:0)

您应该在ActionScript 3中阅读event phases。 如果很快,您的EventDispatcherManager不是DisplayObject,并且不在显示列表中。这就是为什么,您的自定义事件不会冒泡而且无法到达您的主要事件。