loaderInfo.addEvenListener不起作用

时间:2013-09-11 13:24:32

标签: actionscript-3 flash

我为预加载器制作了一个代码,当它在帧动作中编写时起作用。但后来我决定将所有代码移到类中并稍微改写它。现在它不起作用。只是没有调用监听器应该调用的函数,并且在这个未调用的函数中,当address to argument事件时,我开始收到错误。

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 ... part。我明白了:

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.

2 个答案:

答案 0 :(得分:0)

  1. loaderInfo方法中将root.loaderInfo更改为init以侦听root loaderInfo对象。当您的脚本在时间轴this对象中时,对象同时是documentClass和root,因此您的代码已正常运行。

  2. 错误如何,只需在e方法中将Event参数的类型从ProgressEvent更改为showProgress,因为Event不会给了这样的属性。

答案 1 :(得分: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() );
        }
    }
}