外部预加载器导致类型错误
我正在构建一个 FLA 文件,该文件具有文档类“Main”,并且在其构造函数中我告诉它进行跟踪(阶段)。我添加了一个外部预加载器来加载此 SWF,但您知道吗,跟踪语句显示 NULL。
这是当前正在工作的预加载器。
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);
}
对于 Main.swf 本身,这里是文档类:
package {
import Position;
import flash.display.*;
import flash.events.Event;
public class Main extends MovieClip {
public function Main():void {
trace(stage);
}
}
}
////SOLVED/// 我忘记将项目添加到舞台上,但幸运的是在菲利普的代码中我看到了这一点。因此,请记住,一旦 Event.COMPLETE 触发,请将加载器的内容添加到舞台,否则 swf 的文档类将显示 null。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你必须等到你的主类被添加到舞台(Event.ADDED_TO_STAGE)。预加载器现在是阶段所有者...
如果您通过加载器加载外部 SWF,则必须首先等待 Event.INIT,该事件在加载完成后由加载器触发,并执行加载的 swf 的构造函数。如果您将加载程序内容添加到显示列表中,也会触发 ADDED_TO_STAGE 事件。在将显示对象添加到舞台之前,舞台属性为 null。
ldr.loaderInfo.addEventListener( Event.INIT, foo );
ldr.load();
函数 foo( e:Event ):void {
var 内容:* e.target.content;
addChild( 内容 );
}
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.loaderInfo.addEventListener( Event.INIT, foo );
ldr.load();
function foo( e:Event ):void {
var content:* e.target.content;
addChild( content );
}