为什么我只能接收自定义事件的EventDispatcher而不是主? [英] Why I do only receive custom event in EventDispatcher and not in main?

查看:248
本文介绍了为什么我只能接收自定义事件的EventDispatcher而不是主?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从调度的事件调度自定义事件(以下解决方案<一href="http://stackoverflow.com/questions/5586830/inherited-a-class-from-eventdispatcher-in-flash-but-custom-event-not-received">Inherited从EventDispatcher受一类在Flash中,但自定义事件没有收到),我在这两个调度员本身和主要添加一个侦听器。跟踪显示,只有调度员接收到这个事件不是主要的。如何使主也收到事件?

 包
{
    进口flash.display.Sprite;
    进口对象类型:flash.events.Event;

    公共类主要的扩展Sprite
    {
        私人VAR _sliderSprite:SliderSprite;

        公共函数main():无效
        {
            如果(阶段)的init();
            其他的addEventListener(Event.ADDED_TO_STAGE,INIT);

        } //函数结束

        私有函数初始化(E:事件= NULL):无效
        {
            removeEventListener(Event.ADDED_TO_STAGE,INIT);

            _sliderSprite =新SliderSprite();
            _sliderSprite.x =(stage.stageWidth / 2);
            _sliderSprite.y =(stage.stageHeight / 2);
            的addChild(_sliderSprite);
            this.addEventListener(CustomEvent.CUSTOM_EVENT_TYPE,onCustomEventType);

        } //函数结束

        私有函数onCustomEventType(五:自定义事件):无效
        {
                    //从不引发
            跟踪(你好);

        } //函数结束

    } //结束类


} //结束包
 

2级

 包{

    进口flash.display.Sprite;
    进口flash.events.IEventDispatcher;
    进口fl.controls.Slider;

    公共类SliderSprite扩展Sprite
    {
        私人VAR _slider:滑块;
        私人VAR _eventDispatcherManager:EventDispatcherManager;

        公共职能SliderSprite()
        {
            在里面();

        } //函数结束

        私有函数的init():无效
        {
            _slider =新的Slider();
            的addChild(_slider);

            _eventDispatcherManager =新EventDispatcherManager(IEventDispatcher,请(_slider));

        } //函数结束

    } //结束类

}
 

3级

 包{

进口fl.controls.Slider;
进口fl.events.SliderEvent;
进口flash.events *。

内部类EventDispatcherManager扩展EventDispatcher
{
    公共职能EventDispatcherManager(滑块:IEventDispatcher,请)
    {
        slider.addEventListener(SliderEvent.CHANGE,onSliderChange);
        this.addEventListener(CustomEvent.CUSTOM_EVENT_TYPE,onCustomEventType);

    } //函数结束

    私有函数onSliderChange(E:的SliderEvent):无效
    {
        this.dispatchEvent(新自定义事件(CustomEvent.CUSTOM_EVENT_TYPE,e.value));

    } //函数结束

    私有函数onCustomEventType(五:自定义事件):无效
    {
        //触发
        跟踪(e.value);

    } //函数结束

} //函数结束

}
 

4级

 包{
进口对象类型:flash.events.Event;

内部类自定义事件扩展事件
{
    公共静态常量CUSTOM_EVENT_TYPE:字符串=customEventType;
    私人VAR _value:数字;

    公共职能的get()值:数字
    {
        返回_value;

    } //函数结束

    公共功能自定义事件(类型:字符串,
                                值:号码,
                                泡沫:布尔=假,
                                取消:布尔= FALSE)
    {
        _value =值;

        超(类型,泡沫,取消);

    } //函数结束

    重写公共职能的clone():事件
    {
        返回新的自定义事件(类型,值,泡沫,取消);

    } //函数结束

} //结束类

}
 

解决方案

为了让您的主类,从EventDispatcherManager免费获赠事件的情况下,你必须注册为主体与EventDispatcherManager的监听器。

是这样的:

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

不过,对于工作,你就必须要么设置_eventDispatcherManager propterty公共OCH使其通过访问,或者一个简单的GET功能访问。

虽然我强烈建议你看看在结构上。通过它的外观,你可以跳过EventDispatcherManager类和自定义事件类,只是直接从的SliderEvent主dispath的价值。

不过这也许只是一个演示的例子?

编辑:

这是一个getter函数看起来像:

 功能得到eventDispatcherManager():EventDispatcherManager {
       返回_eventDispatcherManager;
}
 

把该功能在你的SliderSprite类,并从主调用它,就像我上面描述。

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:

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

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:

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

Put that function in your SliderSprite class and call it from Main just as I described above.

这篇关于为什么我只能接收自定义事件的EventDispatcher而不是主?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆