简体   繁体   English

用类文件as3预加载外部.swf

[英]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: 所以我试图使用此代码使外部预加载器加载我的主.swf(loading.swf)文件,该文件具有名为mainLoading.as的类文件:

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. TypeError:错误#1009:无法访问空对象引用的属性或方法。 at mainLoading() 在mainLoading()

I think i'm getting the error message because i am accessing the stage in my mainLoading() class file. 我想收到错误消息是因为我正在访问mainLoading()类文件中的阶段。 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 我的initStartUpScene函数只是将介绍性场景抛出到loading.swf

any suggestions? 有什么建议么?

thanks for your help. 谢谢你的帮助。

(question) is your mainLoading extends either Sprite or Movieclip ? (问题)是你mainLoading将扩充SpriteMovieclip

EDIT 编辑

After reading your comment, I would suggest trying this : 阅读您的评论后,我建议您尝试以下操作:

Add a function call inside the swf content in your complete progress handler : 在完整的进度处理程序中的swf内容内添加一个函数调用:

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) content让你访问你的加载方法瑞士法郎主类(如mainLoading)

And replace the event handling in your mainLoading by : 并通过以下方式替换mainLoading中的事件处理:

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. 如果这是您收到的确切消息,则可以,添加ADDED_TO_STAGE侦听器应已对其进行了修复。 Remember to recompile the "loading.swf" if you make any changes to it (a step I always seem to forget) 如果您对它进行了任何更改,请记住重新编译“ loading.swf” (这一步我似乎总是忘记了)

Does "loading.swf" run just fine without any errors when running it on it's own (not loading it into the "container" SWF)? 单独运行“ loading.swf”是否正常(没有将其加载到“容器” 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): OOOOOOKKKKK,所以在承认失败大约一个星期后,再次尝试,重写了几乎一半的代码,试图使自己相信预加载器被高估了,我终于弄清楚了(请打鼓):

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; 私有var xScrollPosRight:Number = stage.stageWidth-xScrollPosLeft;

I just changed stage.stageWidth to 750 and it worked. 我只是将stage.stageWidth更改为750,它才起作用。

live and learn. 活到老,学到老。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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