views:

563

answers:

3

I am loading one swf into another using the Loader class, but when the child swf finishes loading and is added to the display list, its Document Class is not instantiated. I have a few trace statements that should execute when the object is created, but nothing is happening when loaded into the parent SWF. When I compile the child SWF on its own, the Document Class runs as expected.

So I'm wondering... how do I associate a child SWF's Document Class with Loader.content?

Code updated with a solution from Kishi below.

public class Preloader extends Sprite {
    import flash.net.*;
    import flash.display.*;
    import flash.events.*;

    // code in parent SWF's Document Class (Preloader.as)
    private var swfLoader:Loader;
    public var mainMovie:MovieClip;

    public function Preloader(){   
        swfLoader = new Loader();
        swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderDone);
        swfLoader.load(new URLRequest("mainmovie.swf"));
    }

    private function loaderDone(e:Event):void {
        // Cast Loader.content to MovieClip
        mainMovie = MovieClip(swfLoader.content);

        mainMovie.addEventListener(Event.ADDED_TO_STAGE, mainMovieAddedListener);

        addChildAt(mainMovie, 0);

    }
    private function mainMovieAddedListener(e:Event):void {
       // init() not necessary
    }
}

// MainMovie.as runs after casting swfLoader.content to MovieClip

public class MainMovie extends Sprite {

    public function MainMovie(){
        trace('MainMovie WHATTUP'); 
    }

    public function init():void {
        trace('init'); 
    }
}

Cheers!

+1  A: 

You're adding the mainMovie to stage, then you add a listener which inites it. The event is not fired as it's added after the movie is on stage.

Cristi Băluță
A: 

What Cristi said is probably right, for the reason your init method isn't firing, but the more strange issue is that your loaded child swf's Constructor, MainMovie, should be firing as soon as that object is created.

Whenever I've done things like this, I've never created a new Sprite object from the contents of the loaded swf. Seems what you're doing there is using the swf like it's BitmapData, to create the Sprite mainMovie.

Try this: remove your statement that says swfLoader = null, and instead say addChild(swfLoader);. If you still want that event listener checking for it being added to the stage, put it before you do the addChild(swfLoader);, and of course put it on the swfLoader not the mainMovie object:

swfLoader.addEventListener(Event.ADDED_TO_STAGE, mainMovieAddedListener);
addChild(swfLoader);

See what you get then. [also can you paste the exact error you got from trying to access init?]

debu

debu
+1  A: 

The problem is how you are trying to access the swf instance.

First, the document class instance is referred by Loader's content property. You'd reference it like this:

var swf:DisplayObject = swfLoader.content;

But, even then you'd have to cast the DisplayObject either to it's real class (MainMovie, in this case) or a dynamic class (such as MovieClip), as you are trying to use a custom property, that's not part of DisplayObject itself. Therefore, you could call MainMovie.init() like so:

var swf:MovieClip = MovieClip(swfLoader.content);
swf.init();

Hope that helps.

MrKishi