How to modify existing AS3 events so that I can transfer data?

So, I want to configure events so that I can transmit data without creating a memory closure / leak. This, as I understand it:

package com.events { import flash.events.Event; public class CustomEvent extends Event { public static const REMOVED_FROM_STAGE:String = "removedFromStage"; public var data:*; public function CustomEvent(type:String, customData:*=null, bubbles:Boolean=false, cancelable:Boolean=false) { super(type, bubbles, cancelable); this.data = customData; } public override function clone():Event { return new CustomEvent(type, data, bubbles, cancelable); } public override function toString():String { return formatToString("CustomEvent", "type", "data", "bubbles", "cancelable", "eventPhase"); } } } 

This leads me to the following behavior:

 function testme(e:Event) { trace(e); } test_mc.addEventListener(CustomEvent.REMOVED_FROM_STAGE, testme); test_mc.dispatchEvent(new CustomEvent(CustomEvent.REMOVED_FROM_STAGE, 42)); //Traces [CustomEvent type="removedFromStage" data=42 bubbles=false cancelable=false eventPhase=2] removeChild(test_mc); //Traces [Event type="removedFromStage" bubbles=false cancelable=false eventPhase=2] 

My goal is to get the user data that I want to pass in order to get past the flash fire events, not just the one that I am running. For example, what if I wanted to pass a movie clip along with the loader.COMPLETE event to put the resulting bitmap in?

+7
events actionscript-3 listener observer-pattern
source share
2 answers

You have extended the Event class to send additional data, now if you want the Loader class to send your own type of event, extend the Loader class to do this (or any other class you want to do this with). In this example, I will redefine the URLLoader with this functionality (because the loader actually dispatches events from it contentLoaderInfo, which requires two redefined classes, and I just want it to be simple)

 package com.net { import flash.net.URLLoader; import flash.events.Event; import com.events.CustomEvent; public class CustomLoader extends URLLoader { // URLLoader already has a data property, so I used extraData public var extraData:*; override public function dispatchEvent(event: Event) : Boolean { var customEvent: CustomEvent = new CustomEvent(event.type, extraData, event.bubbles, event.cancelable); return super.dispatchEvent(customEvent); } } } 

Now, to use this with your CustomEvent class, try this code in your .fla

 import com.net.CustomLoader; import com.events.CustomEvent; var loader: CustomLoader = new CustomLoader(); loader.extraData = "Extra Data"; loader.load(new URLRequest("test.xml")); loader.addEventListener(Event.COMPLETE, loadComplete); function loadComplete(event: CustomEvent) : void { trace(event.data); // Extra Data } 

Bam! User data for your unexpected events!

+4
source share

Below is the cleanest way to create a custom event. Typically, event types have public static links entered in all letters of the capitol. When an event is dispatched, it passes an Event or CustomEvent object for the event handler method. Here you can get the passed value.

 package com.hodgedev.events { import flash.events.Event; public class CustomEvent extends Event { public static const VALUE_CHANGED:String = "VALUE_CHANGED"; public var value:Number; public function CustomEvent(pValue:Number) { super(CustomEvent.VALUE_CHANGED); value = pValue; } public override function clone():Event { return new CustomEvent(value); } } } 

When we dispatch events, we create a new instance of the event, which should be transmitted as such.

 private var _someValue:int = 12; dispatchEvent(new CustomEvent(_somevalue)); 
0
source share

All Articles