0

我为预加载器编写了一个代码,该代码在以框架动作编写时有效。但是后来我决定将所有代码移到类中并对其进行了一些修改。现在它不起作用了。侦听器应该调用的函数没有被调用,并且在这个未被调用的函数中指向参数事件时我开始收到错误。

package  {
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.ProgressEvent;

    public class Preloader extends MovieClip {
        public function Preloader() {
            addEventListener(Event.ADDED_TO_STAGE, init);
        }
        private function init(e:Event):void {
            trace("init started");
            removeEventListener(Event.ADDED_TO_STAGE, init);
            loaderInfo.addEventListener(ProgressEvent.PROGRESS, showProgress);  
            trace("init completed");
        }
        private function showProgress(e:Event):void {
            trace(loaderInfo.bytesLoaded);
            /*info_txt.text = Math.floor(e.bytesLoaded/e.bytesTotal*100) + "%"
            if (e.bytesLoaded==e.bytesTotal) {
            loaderInfo.removeEventListener(ProgressEvent.PROGRESS, showProgress);   
            finalizeLoading();
            }
            /**/
        }
        private function finalizeLoading():void {
            removeChild(info_txt);
            var startGame_btn = new StartGame_btn();
            addChild(startGame_btn);startGame_btn.x=395;startGame_btn.y=290;
        }
    }       
}

当我取消注释 /*info_txt... 部分时。我明白了:

Access of possibly undefined property bytesLoaded through a reference with static type flash.events:Event.
Access of possibly undefined property bytesTotal through a reference with static type flash.events:Event.
Access of possibly undefined property bytesLoaded through a reference with static type flash.events:Event.
Access of possibly undefined property bytesTotal through a reference with static type flash.events:Event.
4

2 回答 2

0

你的代码有很多问题。

这是一个简单的加载器,其中添加了一些代码。在最坏的情况下,它应该为您指明正确的方向。

package  {
    import flash.display.MovieClip;
    import flash.errors.IOError;
    import flash.events.AsyncErrorEvent;
    import flash.events.ErrorEvent;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.events.ProgressEvent;
    import flash.events.SecurityErrorEvent;

    public class myPreloader extends MovieClip {
        public var context:LoaderContext = new LoaderContext();
        public var myLoader:Loader = new Loader();

        public var url:URLRequest;
        public function myPreloader() {
            context = new LoaderContext();
            context.checkPolicyFile = true; 
            myLoader = new Loader();

            myLoader.addEventListener(AsyncErrorEvent.ASYNC_ERROR, errorHandlerAsyncErrorEvent);
            myLoader.addEventListener(IOErrorEvent.IO_ERROR, errorHandlerIOErrorEvent);
            myLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandlerSecurityErrorEvent);
            myLoader.contentLoaderInfo.addEventListener(Event.INIT, initHandler);
            myLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, infoIOErrorEvent);
            myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressListener);
            myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);

            this.addEventListener(Event.ADDED_TO_STAGE, init);
        }
        private function init(e:Event):void {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            url = new URLRequest("test.swf"); // change this to whatever it is you are loading
            myLoader.load( url,context );
            myLoader.load( url);
        }
        public function progressListener (e:ProgressEvent):void{
            trace("Downloaded " + e.bytesLoaded + " out of " + e.bytesTotal + " bytes");
            info_txt.text = info_txt.text = Math.floor(e.bytesLoaded/e.bytesTotal*100) + "%"
        }
        public function onLoadComplete( e:Event ):void{
            trace( 'onLoadComplete' );
            removeChild(info_txt);
            var startGame_btn = new StartGame_btn();
            addChild(startGame_btn);
            startGame_btn.x = 395;
            startGame_btn.y=290;
        }
        public function initHandler( e:Event ):void{
            trace( 'load init' );
        }
        public function errorHandlerErrorEvent( e:ErrorEvent ):void{
            trace( 'errorHandlerErrorEvent ' + e.toString() );
        }
        public function infoIOErrorEvent( e:IOErrorEvent ):void{
            trace( 'infoIOErrorEvent ' + e.toString() );
        }
        public function errorHandlerIOErrorEvent( e:IOErrorEvent ):void{
            trace( 'errorHandlerIOErrorEvent ' + e.toString() );
        }
        public function errorHandlerAsyncErrorEvent( e:AsyncErrorEvent ) :void{
            trace( 'errorHandlerAsyncErrorEvent ' + e.toString() );
        }
        public function errorHandlerSecurityErrorEvent( e:SecurityErrorEvent ):void{
            trace( 'errorHandlerSecurityErrorEvent ' + e.toString() );
        }
    }
}
于 2013-09-11T14:17:15.360 回答
0
  1. 在监听根loaderInfo对象的方法中loaderInfo改为。当您的脚本位于时间轴对象中时,documentClass 和 root 同时存在,因此您的代码可以正常工作。root.loaderInfoinitthis

  2. 错误呢,只需在方法中将e参数类型从Eventto更改为,因为没有给出这样的属性。ProgressEventshowProgressEvent

于 2013-09-11T13:50:45.317 回答