preloader在ActionScript 3 - 对getDefinition引用错误()错误、preloader、ActionScript、getDefinition

2023-09-08 13:47:00 作者:我六毛你六毛咱俩一块二

我正在写一个preloader:

I'm writing a preloader:

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 );
            }
    }
}

本作为主类:

with this as the Main class:

package {
    import flash.display.Sprite;

    public class Main extends Sprite {

            public function Main() {
            }
    }
}

所以这是接近的准系统,因为我可以做到这一点。

so this is close to as barebones as I could make it.

然而,迎接我了:

ReferenceError: Error #1065: Variable Main is not defined.
at flash.system::ApplicationDomain/getDefinition()
at ...

我用frames.frame已经插入主。我编译使用Ant和Linux的SDK直接(mxmlc的)。我缺少什么明显?

I use frames.frame to insert Main already. I am compiling using ant and the linux SDK directly (mxmlc). Am I missing anything obvious?

推荐答案

当你正在做一个preloader在这个风真正发生的事情是,preloader放在的第一帧应用,其余的在第二帧中。什么,你就错过这里还是要告诉你希望你的主类编译,编译器,所以现在它甚至没有在SWF存在。这就是为什么getDefinition将无法正常工作。

When you're making a preloader in this "style" what really happens is that the preloader is put in the first frame of the application, and the rest in a second frame. What you're missing here is to tell the compiler that you want your Main class compiled in, so right now it doesn't even exist in the swf. That's why getDefinition won't work.

你也不能简单地引用它在preloader,因为这将使其在第一帧加载preloader,才能显示。所以,你需要一点点的自定义参数的魔法。

Nor can you simply refer to it in the preloader since that would make it load in the first frame before the preloader can be shown. So, you need a little custom argument magic.

将此行添加到您的编译器选项,你应该是好去:

Add this line to your compiler options and you should be good to go:

-frame start Main

请记住,如果你的主类是在一个包,你需要得到一个完整的参考有:

Remember that if your Main class is in a package you need to get a full reference in there:

-frame start com.grapefrukt.examples.Main

同样适用于getDefinition电话。

Same goes for the getDefinition call.

编辑:

当看在我的code,这是否我看到我使用你做了什么不同的方法,这也许效果会更好:

When looking over my code that does this I see that I use a different approach from what you did, maybe this works better:

var mainClass:Class = getDefinitionByName("com.grapefrukt.examples.Main") as Class;
addChild(new mainClass() as DisplayObject);

再次编辑: 如果它的工作原理使用一个按钮,我猜整个事件被炒得早因为某种原因。这可能是让一切不inited正常情况下,虽然所有的字节loaderd。尝试使用这种code,检查完成,而不是:

EDIT AGAIN: If it works using a button I'd guess the complete event is fired too early for some reason. It may be so that everything is not inited properly event though all the bytes are loaderd. Try using this code to check for completion instead:

if (currentFrame == totalFrames) onLoadingCompleted()

这也可能是您的onLoadingCompleted(添加一个stop()命令)方法只是以使播放头不会被拧的东西是一个好主意,但这是以后的问题,真的。

It might also be a good idea to add a stop() command in your onLoadingCompleted() method just to make the playhead won't be screwing things up, but that's a later issue really.