Ultrashock Forums > Flash > ActionScript
AS3 - Nested Event Listeners with Custom Events - Time Sensitive

You are currently viewing our website as a guest which gives you limited access to forums, files and other resources.

Click here to join now for free, and start interacting with our members, download files and much more!

Click here if you are looking for our Flash files and other professional assets.
 
Post Reply | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes

#1
Bookmark and Share!
AS3 - Nested Event Listeners with Custom Events - Time Sensitive
Old 2009-05-30

Hey everyone - need some help -
Here's a hodge-podge of information:

Custom Event Class:
Code:
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:
Code:
var urlLoader:URLLoader = new URLLoader();
            urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
            urlLoader.addEventListener(Event.COMPLETE, onSwfLoaded);
            urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
            urlLoader.addEventListener(SecurityErrorEvent.SECURITY_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_ERROR, onIoError);
                urlLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,
onSecurityError);
            }

        }

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

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

            _loader = new Loader();
            var loaderContext:LoaderContext = new LoaderContext();
            _loader.contentLoaderInfo.addEventListener(Event.COMPLETE,
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:
Code:
urlLoader.addEventListener(Event.COMPLETE, onSwfLoaded);
But stops on the second addEventListener.

If I just apply the custom event to the following...
Code:
_loader.addEventListener(Event.COMPLETE, onLoaderInit);
VERSUS
Code:
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, 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
postbit arrow 3 comments | 373 views postbit arrow Reply: with Quote   
Registered User
RandallPotter is offline
seperator
Posts: 2
2009-05-30
seperator

Ultrashock Member Comments:
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2009-05-30 #2 Old  
Add the clone method to your CustomEvent class:

Code:
override public function clone():Event
{
    return new CustomEvent( type, data, bubbles, cancelable );
}
Reply With Quote  
RandallPotter RandallPotter is offline 2009-05-30 #3 Old  
I've already got that in there - thank you Nutrox for replying

public override function clone():Event {
return new CustomEvent(type, data, bubbles, cancelable);
}
Reply With Quote  
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2009-05-30 #4 Old  
Silly me.

Hmm... when and how are you dispatching your custom event?
Reply With Quote  
Thread Tools
Display Modes Rate This Thread
Rate This Thread: