0
votes

I am building a FLA file that has a document class "Main" and in its constructor i have told it to trace(stage). I added an external preloader to load this SWF but what do you know, the trace statement is showing NULL.

Here is the preloader that is currently working.

import flash.display.Loader;
import flash.events.ProgressEvent;
import flash.events.Event;
import flash.net.URLRequest;

var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
l.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
l.load(new URLRequest("Main.swf"));

function onProgress(e:ProgressEvent):void {
    preloader.mask.height = (e.bytesLoaded / e.bytesTotal) * preloader.lemon.height;
}

function onComplete(e:Event):void {
    removeChildAt(0);
}

For the Main.swf itself here is the document class:

package  {

    import Position;
    import flash.display.*;
    import flash.events.Event;

    public class Main extends MovieClip {

        public function Main():void {
            trace(stage);
        }
    }
}

////SOLVED/// I forgot to add the item to the stage but luckily in phillip's code i saw this. So just remember once the Event.COMPLETE fires, add the contents of the loader to the stage else the document class for the swf will show null.

1

1 Answers

3
votes

you have to wait till your main class is added to the stage( Event.ADDED_TO_STAGE ). The preloader is now the stage owner...

If you are loading an external SWF throug a loader you have to wait for the Event.INIT first, that is fired by the loader after loading is completed an the constructor of your loaded swf is executed. If you than add the loaders content to the display list the ADDED_TO_STAGE Event is triggered too. Before a display object is added to the Stage, the stage property it's null.

ldr //your loader

ldr.loaderInfo.addEventListener( Event.INIT, foo ); ldr.load();

function foo( e:Event ):void { var content:* e.target.content; addChild( content ); }