views:

2715

answers:

1

I'm trying to load a local SWF then catch a few events that are fired but I can't see why this isn't working.

Here's the code

Parent.swf

_mcl = new MovieClipLoader();
_mcl.addListener(this);
_mcl.loadClip("Child.swf", rotator_mc);

function onLoadComplete(clip) {
    clip.addEventListener("initAsLoaded", function() {trace("childLoaded loaded 1")});
}

function onLoadInit(clip) {
    clip.addEventListener("initAsLoaded", function() {trace("childLoaded loaded 2")});
}

Child.swf

import mx.events.EventDispatcher;

function dispatchEvent() {};
function addEventListener() {};
function removeEventListener() {};

EventDispatcher.initialize(this);

trace("Child dispatching: childLoaded");
dispatchEvent({type:"childLoaded", target: this});

Now I was hoping that this would work, and the Parent would have "childLoaded caught 2" in the trace, but it doesn't.

Is there any way to achieve what I'm trying to do?

+1  A: 

The first thing I noticed was the fact that in the parent you are listing for an event called initAsLoaded but in the child you're dispatching an event called childLoaded. Listening for an event that will never be dispatched might not be a good idea.

However, this is not the actual issue because fixing the events doesn't solve the problem. The main problem is that the onLoadInit event from the MovieClipLoader is dispatched after the code of the first frame of child.swf has been executed. This means that in child.swf you're dispatching an event on the first frame that know one is listening too because the onLoadInit hasn't been called yet.

To fix this you can only start dispatching events in the child.swf from the second frame and onwards.

Parent first frame:

var rotator_mc = _root.createEmptyMovieClip("rotator_mc", _root.getNextHighestDepth() );

_mcl = new MovieClipLoader();
_mcl.addListener(this);
_mcl.loadClip("child.swf", rotator_mc);

function onLoadComplete(clip) {
    trace("onLoadComplete " + clip + ", " + clip.addEventListener );
    clip.addEventListener("initAsLoaded", function() {trace("childLoaded loaded 1")});
}

function onLoadInit(clip) {
    trace("onLoadInit " + clip + ", " + clip.addEventListener );
    clip.addEventListener("initAsLoaded", function() {trace("childLoaded loaded 2")});
}

This is my version of your parent's first frame. I had to create an empty movie clip to serve as a container and I added some traces to the event handlers.

Child first frame:

import mx.events.EventDispatcher;

EventDispatcher.initialize( this );

Child second frame:

stop();

this.dispatchEvent({type:"initAsLoaded", target: this});

trace("Child dispatching: childLoaded");

Hope this helps...

Luke
Thank you very much, that solved my problem.Apologies for the contrived example :)