我正在写一个预加载器:
package {
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.text.TextField;
import flash.display.LoaderInfo;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.system.System;
public class Preloader extends Sprite {
private var t:TextField = new TextField();
private var ver:String = "1000";
public function Preloader() {
t.y = 570;
addChild( t );
t.text = ver;
addEventListener( Event.ADDED_TO_STAGE, init );
}
private function init( e:Event ):void {
this.root.loaderInfo.addEventListener( ProgressEvent.PROGRESS, onLoadingProgress );
this.root.loaderInfo.addEventListener( Event.COMPLETE, onLoadingCompleted );
// See if it's already completed
var percent:int = int( root.loaderInfo.bytesLoaded / root.loaderInfo.bytesTotal );
if ( percent == 1 )
onLoadingCompleted();
}
private function onLoadingProgress( event:Event ):void {
var percent:int = int(root.loaderInfo.bytesLoaded / root.loaderInfo.bytesTotal * 100);
t.text = "Loading.. " + percent + "%";
}
private function onLoadingCompleted( event:Event = null ):void {
root.loaderInfo.removeEventListener( ProgressEvent.PROGRESS, onLoadingProgress );
root.loaderInfo.removeEventListener( Event.COMPLETE, onLoadingCompleted );
var mainClass:Class = loaderInfo.applicationDomain.getDefinition("Main") as Class;
var main:DisplayObject = new mainClass() as DisplayObject;
parent.addChild( main );
parent.removeChild( this );
}
}
}
以此为主类:
package {
import flash.display.Sprite;
public class Main extends Sprite {
public function Main() {
}
}
}
所以这已经接近我所能做到的最准系统了。
但是,它向我打招呼:
ReferenceError: Error #1065: Variable Main is not defined.
at flash.system::ApplicationDomain/getDefinition()
at ...
我已经使用 frames.frame 来插入 Main。我正在直接使用 ant 和 linux SDK (mxmlc) 进行编译。我遗漏了什么明显的东西吗?
最佳答案
当您以这种“风格”制作预加载器时,真正发生的是预加载器放在应用程序的第一帧中,其余部分放在第二帧中。您在这里缺少的是告诉编译器您希望编译 Main 类,所以现在它甚至不存在于 swf 中。这就是 getDefinition 不起作用的原因。
您也不能简单地在预加载器中引用它,因为这会使它在预加载器显示之前的第一帧加载。因此,您需要一点自定义参数魔法。
将此行添加到您的编译器选项中,您应该可以开始了:
-frame start Main
请记住,如果您的主类在一个包中,您需要在其中获得完整的引用:
-frame start com.grapefrukt.examples.Main
getDefinition 调用也是如此。
编辑:
在查看我执行此操作的代码时,我发现我使用的方法与您所做的不同,也许这样效果更好:
var mainClass:Class = getDefinitionByName("com.grapefrukt.examples.Main") as Class;
addChild(new mainClass() as DisplayObject);
再次编辑: 如果它使用按钮工作,我猜想由于某种原因,完整的事件被触发得太早了。尽管所有字节都是 loaderd,但这可能是因为一切都没有正确初始化。尝试使用此代码来检查是否完成:
if (currentFrame == totalFrames) onLoadingCompleted()
在您的 onLoadingCompleted() 方法中添加一个 stop() 命令可能也是一个好主意,只是为了让播放头不会搞砸,但这确实是以后的问题。
关于actionscript-3 - actionscript 3 中的预加载器 - getDefinition() 上的引用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1752549/