What is the appropriate form when sending events to AS3?

I was wondering what was the appropriate form when creating custom events? If you need to create a CustomEvent class, then create a temporary dispatcher in this function and send the CustomEvent. or it’s better to try to create a CustomEventDispatcher class and create a CustomEvent class as an inner class of this class, for example:

package { public class CustomEventDispatcher extends EventDispatcher { public function CustomEventDispatcher() { super(new CustomEvent()); } } } class CustomEvent { public function CustomEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false) { super(type, bubbles, cancelable) } } 
+2
source share
3 answers

There are two main questions that need to be answered when thinking of the mechanics of events.

1) How to create a dispatcher instance for my events?

General parameters: an EventDispatcher extension or an aggregate dispatcher instance.

In most basic and generally accepted practices (and official docs also indicate), the EventDispatcher class expands, giving your classes the ability to dispatch events.

Pros : simple to implement - only the type extends EventDispatcher, and you're done.

Cons : you cannot expand anything else. Apparently, for this reason, many native classes are grandchildren of EventDispatcher. Just to save us trouble, I think.

The second general approach is to merge a dispatcher instance.

 package { import flash.events.Event; import flash.events.EventDispatcher; import flash.events.IEventDispatcher; public class ClassA implements IEventDispatcher { private var dispatcher:EventDispatcher; public function ClassA() { initialize(); } private function initialize():void { dispatcher = new EventDispatcher(this); } public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void { dispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference); } public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void { dispatcher.removeEventListener(type, listener, useCapture); } public function dispatchEvent(event:Event):Boolean { return dispatcher.dispatchEvent(event); } public function hasEventListener(type:String):Boolean { return dispatcher.hasEventListener(type); } public function willTrigger(type:String):Boolean { return dispatcher.willTrigger(type); } } } 

Note. We pass the link to the class aggregation to the dispatcher constructor. This is to ensure that event.target refers to your instance of the class, and not to the dispatcher instance itself.

Pros : you can expand whatever you want. You can do some tricks with dispatch hooks, such as a listener list or something similar.

Cons : not as easy as the first approach.

2) How to transfer user data using my events?

General parameters: transfer data in the event instance or use the event.target link in the event handler to access some data from the source.

If you decide to access all the necessary data using event.target - there was no additional work, just apply this link in the event handler to the corresponding class.

If you want to pass some data along with the event, you subclass Event, and this class must be publicly available for code that processes events, as mentioned above. AS3 is all about strong and strong typing, so why do you resist this?

The overriding clone () method in the Event subclass is needed only if you intend to perform repeated sessions of the processed events. White papers say you should do this every time you create your own class of events to be safe.

0
source

Unilaterally, it is better to make events publicly available. This way, you can recruit your listeners (useful for hinting and debugging code) and have an event that has public static constants (which you might also want to see).

+2
source

Remember to redefine the clone. also nice to override toString for debugging.

here is an example of one of my custom events:

 package com.mattie.events { //Imports import flash.events.Event; //Class public class SearchFieldEvent extends Event { //Constants public static const SEARCH_COMPLETE:String = "search complete"; //Variables public var totalResults:uint; public var duration:uint; public var searchText:String; //Constructor public function SearchFieldEvent(type:String, totalResults:uint = 0, duration:uint = 0, searchText:String = "") { super(type); this.totalResults = totalResults; this.duration = duration; this.searchText = searchText; } //Override clone public override function clone():Event { return new SearchFieldEvent(type, totalResults, duration, searchText); } //Override toString public override function toString():String { return formatToString("SearchFieldEvent", "type", "totalResults", "duration", "searchText"); } } } 
0
source

All Articles