简体   繁体   中英

preloading external .swf with class file as3

so i'm trying to make an external preloader to load my main .swf (loading.swf) file that has a class file named mainLoading.as using this code:

var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loop);
l.contentLoaderInfo.addEventListener(Event.COMPLETE, done);
l.load(new URLRequest("loading.swf"));
var loadingPage:loading = new loading;

function loop (e:ProgressEvent):void{
    addChild(loadingPage);
    loadingPage.x = stage.stageWidth/2;
    loadingPage.y = stage.stageHeight/2;

}

function done (e:Event):void{
    removeChild(loadingPage);
    addChild(l);
}

so I'm getting an error message saying:

TypeError: Error #1009: Cannot access a property or method of a null object reference. at mainLoading()

I think i'm getting the error message because i am accessing the stage in my mainLoading() class file. I tried adding this to the constructor in my class file but it didn't work:

public function mainLoading () {
    addEventListener(Event.ADDED_TO_STAGE, init);   
}
private function init(e:Event): void {
    initStartUpScene ();

}

my initStartUpScene function just throws the intro scene to the loading.swf

any suggestions?

thanks for your help.

(question) is your mainLoading extends either Sprite or Movieclip ?

EDIT

After reading your comment, I would suggest trying this :

Add a function call inside the swf content in your complete progress handler :

function done (e:Event):void{
    removeChild(loadingPage);
    addChild(l);
    Object(l.content).initMethod();
}

content let you access the methods in your loaded .swf main class (eg mainLoading)

And replace the event handling in your mainLoading by :

public function mainLoading () {
    //no event handling in constructor
}

public function initMethod():void {
    //here you go
    init();
}

public function init():void { ... //No more event here

Btw it's not the cleanest way to solve your problem.

If that's the exact message you are getting, then yes, adding the ADDED_TO_STAGE listener should have fixed it. Remember to recompile the "loading.swf" if you make any changes to it (a step I always seem to forget)

Does "loading.swf" run just fine without any errors when running it on it's own (not loading it into the "container" SWF)?

This may be unrelated to the question you asked, but you might get better results and avoid some errors by structuring your code like this:

var loadingPage:loading = new loading;
addChild(loadingPage);
loadingPage.x = stage.stageWidth/2;
loadingPage.y = stage.stageHeight/2;

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

//I renamed this function since I believe "loop" is a reserved keyword.
function onProgress (e:ProgressEvent):void{
    //No code needed
}

function onComplete (e:Event):void{
    removeChild(loadingPage);
    addChild(l);
}

You can remove the "onProgress" function if you won't be needing it as well.

OOOOOOKKKKK, so after about a week of admitting defeat, trying it again, rewriting almost half of my code, trying to convince myself that preloaders are overrated, i finally figured it out (drum roll please):

I had a variable that was referencing the stage being called before my constructor method was called. like this:

private var xScrollPosRight:Number = stage.stageWidth - xScrollPosLeft;

I just changed stage.stageWidth to 750 and it worked.

live and learn.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM