Generic events and additional parameters in Actionscript 3.0?

不打扰是莪最后的温柔 提交于 2019-12-31 03:55:10

问题


Assuming the following pattern:

someObjectInstance.addEventListener(MyDisplayObject.EVENT_CONSTANT, _handleMyEvent);


private function _handleMyEvent( event:Event = null ):void
{
  // Event handler logic...
}

If I wanted to add a required parameter to the handler function am I able to do this and still use a "generic" event and event listener? Or am I correct in assuming that I need to create a custom event class that has the parameter in it and reference that through the event object passed into the handler function?

To phrase this another way... If I have a handler function that looks like this:

private function _handleMyEvent( data:Object, event:Event = null ):void
{
  if (data == null)
  {
      return;
  }
  // Event handler logic...
}

Then what does the addEventListener function need to look like? Is there a more advanced syntax? Is there a way to do this with closures?

Looking for a clear code example and/or documentation reference. Just trying to understand if I absolutely have to override the generic Event class in this scenario.


回答1:


If you need custom data to travel with your event, yes, you need to create a custom event class.

Here's a simple example:

package {
    import flash.events.Event;

    public class ColorEvent extends Event {
        public static const CHANGE_COLOR:String = "ChangeColorEvent";

        public var color:uint;

        public function ColorEvent(type:String, color:uint, bubbles:Boolean = false, cancelable:Boolean = false) {
            this.color = color;
            super(type, bubbles, cancelable);
        }

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

Please note that the clone method is NOT optional. You must have this method in your custom class for your event to ever be re-broadcast properly (say when one object gets the event, and then re-dispatches it as it's own).

Now as for dispatching the event, that would work like this (obviously this code would go in a method of class that extends EventDispatcher).

dispatchEvent(new ColorEvent(ColorEvent.CHANGE_COLOR, 0xFF0000));

And finally, to subscribe to the event:

function onChangeColor(event:ColorEvent):void {
    trace(event.color);
}

foo.addEventListener(ColorEvent.CHANGE_COLOR, onChangeColor);



回答2:


There is a way of passing custom data to the handler method without creating a custom event.

private function test() {
    var data : SomeObject = new SomeObject;
    var a:SomeEventDispatcher = new SomeEventDispatcher();
    a.addEventListener(Event.COMPLETE, handle(data));   
    a.dispatchCompleteEvent();
}

private function handle(data : SomeObject) : Function {
    return function(e : Event) : void {
       IEventDispatcher(e.target).removeEventListener(Event.COMPLETE, arguments.callee);
       trace(e + ", " + data);
    };
}



回答3:


Wait a minute, everybody. See this answer.

You don't need to customize anything at all to reference the parameter through the listener. So, yes, Gordon, you're able to do this with your average event and event listener.

And it's way simple! The addEventListener function stays the same. Its listener is what will be adjusted:

var functionHandleMyEvent:Function = _handleMyEvent(data);
someObjectInstance.addEventListener(MyDisplayObject.EVENT_CONSTANT, functionHandleMyEvent);
// Later, when you need to remove it, do:
//someObjectInstance.removeEventListener(MyDisplayObject.EVENT_CONSTANT, functionHandleMyEvent);

private function _handleMyEvent(data:Object):Function {
  return function(event:Event):void {
    if (data == null) {
      // Event handler logic here now has both "event" and "data" within your reach
    }
  }
}

No advanced syntax, but a relatively advanced concept: variable functions. You can do closures with what we have here, but you absolutely won't need to. And like that, you also absolutely won't need to override the Event class.

I hope these examples and documentation are clarifying for you!



来源:https://stackoverflow.com/questions/1431230/generic-events-and-additional-parameters-in-actionscript-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!