简体   繁体   English

外部预加载器导致TypeError

[英]External Preloader Causes TypeError

I am building a FLA file that has a document class "Main" and in its constructor i have told it to trace(stage). 我正在构建一个具有文档类“ Main”的FLA文件,并且在其构造函数中我已告知它trace(stage)。 I added an external preloader to load this SWF but what do you know, the trace statement is showing NULL. 我添加了一个外部预加载器来加载此SWF,但是您知道,trace语句显示为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: 对于Main.swf本身,这是文档类:

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. //// SOLVED ////我忘了将该项目添加到舞台上,但幸运的是,在phillip的代码中,我看到了这一点。 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. 因此,请记住,一旦Event.COMPLETE触发,请将加载程序的内容添加到阶段中,否则swf的文档类将显示为null。

you have to wait till your main class is added to the stage( Event.ADDED_TO_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. 如果要通过加载程序加载外部SWF,则必须首先等待Event.INIT,该事件在加载完成后由加载程序触发,并执行加载的swf的构造函数。 If you than add the loaders content to the display list the ADDED_TO_STAGE Event is triggered too. 如果不将加载程序的内容添加到显示列表,则也会触发ADDED_TO_STAGE事件。 Before a display object is added to the Stage, the stage property it's null. 在将显示对象添加到舞台之前,stage属性为null。

ldr //your loader

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

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

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

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