简体   繁体   English

调度自定义事件在haxe中不起作用

[英]dispatch custom event don't work in haxe

i have a weird problem... i try to build a button that onClick doing something(i dont use in regular event because i need to transfer a data with the event). 我有一个奇怪的问题...我尝试建立一个onClick做某事的按钮(我不在常规事件中使用,因为我需要在事件中传输数据)。 i built a UIButon class and create an instance of him in the parent class. 我建立了一个UIButon类,并在父类中创建了他的一个实例。 i create a custom Event class: 我创建一个自定义事件类:

package ;
import openfl.events.Event;

/**
 * ...
 * @author Michael
 */
class ChangeWinEvent extends Event
{
    public static inline var CHANGE_WINDOW:String = "changeWindow";



    public var _winToClose:String;
    public function new(name:String, winToClose:String, bubbles:Bool=false, cancelable:Bool=false) 
    {
        super(type, bubbles, cancelable);       
        _winToClose = winToClose;
    }

}

and i dispatch CHANGE_WINDOW event like this: 我分派CHANGE_WINDOW事件,如下所示:

dispatchEvent(new ChangeWinEvent(ChangeWinEvent.CHANGE_WINDOW,"LoginWin"));

and listen to this event in parent class: 并在父类中收听此事件:

_loginBtn.addEventListener(ChangeWinEvent.CHANGE_WINDOW, handleChangeWindows);

thank you helpers! 谢谢帮手! michael 迈克尔

You also have to override the clone method. 您还必须重写clone方法。 Take a look at this custom event class i'm currently using: 看看我当前正在使用的这个自定义事件类:

/**
 * Custom button event used for communication
 * between button classes and their respective
 * views.
 * @author Tiago Ling Alexandre
 */
class ButtonEvent extends Event
{
    public static var ACTIVATE:String = "Activate";

    public var data:Dynamic;

    public function new(type:String, data:Dynamic, bubbles:Bool = false, cancelable:Bool = false) 
    {
        super(type, bubbles, cancelable);
        this.data = data;
    }

    override public function clone():Event
    {
        return new ButtonEvent(type, bubbles, cancelable);
    }
}

The only difference from your class is the clone() method. 与类的唯一区别是clone()方法。 That's the missing piece. 那是缺失的那一块。

Regards, 问候,

super(type, bubbles, cancelable); 超级(类型,气泡,可取消);

...apparently takes type variable from type instance field (as you have constructor parameter named name, not type), so it is null and you haven't registered any listener for null event type. ...显然从类型实例字段中获取类型变量(因为您有名为name的构造函数参数,而不是类型),因此它为null,并且尚未为null事件类型注册任何侦听器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM