PDA

View Full Version : Nested Event Listeners with Custom Events - Time Sensitive



prstudio
May 30th, 2009, 02:00 AM
Hey everyone - need some help -
Here's a hodge-podge of information:

Custom Event Class:

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");
}
}
}


Using the following:

var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.addEventListener(Event.COMPLETE, onSwfLoaded);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
urlLoader.addEventListener(SecurityErrorEvent.SECU RITY_ERROR,
onSecurityError);
try {
urlLoader.load(new URLRequest(txtPath.text));
} catch (e:Error) {
resetBrowseControls();
txtError.text = "Error:\n" + e.toString();
urlLoader.removeEventListener(Event.COMPLETE, onSwfLoaded);
urlLoader.removeEventListener(IOErrorEvent.IO_ERRO R, onIoError);
urlLoader.removeEventListener(SecurityErrorEvent.S ECURITY_ERROR,
onSecurityError);
}

}

private function onSwfLoaded(e:Event):void {
var urlLoader:URLLoader = URLLoader(e.currentTarget);
urlLoader.removeEventListener(Event.COMPLETE, onSwfLoaded);
urlLoader.removeEventListener(IOErrorEvent.IO_ERRO R, onIoError);
urlLoader.removeEventListener(SecurityErrorEvent.S ECURITY_ERROR,
onSecurityError);

_bytes= ByteArray(URLLoader(e.currentTarget).data);

_loader = new Loader();
var loaderContext:LoaderContext = new LoaderContext();
_loader.contentLoaderInfo.addEventListener(Event.C OMPLETE,
onLoaderInit);
_loader.loadBytes(_bytes, loaderContext);
}

private function onLoaderInit(e:Event):void {
resetBrowseControls();
populateList(_bytes);
}



I made a custom loader to replace the urlLoader and even tried a
replacement for the _loader using the same custom event.

Works great for the first addEventListener:

urlLoader.addEventListener(Event.COMPLETE, onSwfLoaded);

But stops on the second addEventListener.

If I just apply the custom event to the following...

_loader.addEventListener(Event.COMPLETE, onLoaderInit);
VERSUS

_loader.contentLoaderInfo.addEventListener(Event.C OMPLETE, onLoaderInit);
it works just fine.

As soon as I use it on the contentLoaderInfo line, it throws that type
error
cannot convert CustomEvent to Event error.

I also double checked that my custom event listener was overriding clone()
properly and it looks like it does.

Any ideas as to why it's different?

Thanks :)

elliotrock
May 31st, 2009, 12:24 AM
Jason,

My first thought is the fussiness of the contentLoaderInfo in that you are applying the listeners before the load call. Generally it is not an issue but try to move the listener to after the loadBytes. In a simple sense _loader.contentLoaderInfo doesn't exist when that addEventListener is defined.

From your comments that it works off normal events could mean something else as well. I cannot see anything else that is obvious without running the class and debugging it.

Cheers
Elliot