Reputation: 38180
I used syntax I found here AS3: add event listener to loaded swf
var theLoader:Loader = new Loader();
theLoader.load(new URLRequest("mySWF.swf"));
theLoader.addEventListener(CustomEvent.ON_CUSTOM_EVENT,OnCustomEvent);
MySWF loads without problem.
It doesn't really returns the class mySWF so I wonder if this is really the right syntax. And actually I don't receive anything.
I am of course dispatching custom event from mySWF.
Flash seems really a nightmare to cope with custom events in practice compared to other platforms.
Upvotes: 0
Views: 107
Reputation: 6215
It sounds like you just need to get a reference to the content loaded by the loader. That works like this:
var movieClip:MovieClip;
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
loader.load(new URLRequest("mySwf.swf"));
function completeHandler(e:Event):void {
movieClip = MovieClip(loader.content); // this extracts the movieclip.
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler);
loader = null // prepares the loader for garbage collection.
process();
}
function process():void {
movieClip.addEventListener(MouseEvent.CLICK, clickHandler);
}
function clickHandler(e:MouseEvent):void {
trace("You just clicked your loaded .swf!");
}
From here you should just be able to listen for your custom event, too. I'll check back later to see if you're having any luck, I don't have time to build out a full test case right now.
One thing to note - you see that I'm casting loader.content as a MovieClip, that's just because it's a safe, dynamic base class for any loaded .swf. Because MovieClip is dynamic you can attempt to access methods and properties on that class without having them explicitly defined.
If you decide you need more advanced data typing you have a few options. You can create an Interface that gets compiled into both applications (the parent .swf and the child .swf) and then case loader.content as that Interface. Or (and i'm just speculating now, I've never tried this but it SHOULD work...) you can give the child .swf a public property called "myClass" which holds a reference to the base class of that .swf.
Then you'd do what I did above, but in the process() method you'd do something like this:
function process():void {
var class:Class = movieClip.myClass as Class;
var myTypedLoadedMovieClip:class = class(movieClip);
trace(myTypedLoadedMovieClip); // this SHOULD work...right?
}
As I said, I'll check back later to see if you're having any luck and I'll try to come up with some tests for this!
Upvotes: 1
Reputation:
Add a parameter to your custom event to include a reference to the instance of the class dispaytching the event. Example:
DispatchEvent(new CustomEvent(CustomEvent.TYPE, bubbles, cancellable, this));
Upvotes: 1
Reputation: 9424
your custom event won't get called, because the loader only supports a few predefined events. if you think about it, how could the loader know, when and why he should fire your custom event? it only makes sense to fire predefined events (like Event.COMPLETE, ...). have a look here: loader events.
Upvotes: 1