0

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). i built a UIButon class and create an instance of him in the parent class. 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:

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

Michael
  • 896
  • 10
  • 28

2 Answers2

3

You also have to override the clone method. 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. That's the missing piece.

Regards,

TiagoLing
  • 131
  • 3
0

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.

stroncium
  • 1,430
  • 9
  • 8