zoukankan      html  css  js  c++  java
  • actionscript项目中自加载preloader实现

    package
    {
     import flash.display.DisplayObject;
     import flash.display.MovieClip;
     import flash.display.Sprite;
     import flash.display.StageAlign;
     import flash.display.StageScaleMode;
     import flash.events.Event;
     import flash.events.IOErrorEvent;
     import flash.events.ProgressEvent;
     import flash.utils.getDefinitionByName;
     
     
     public class Preloader extends MovieClip
     {
      private var bar:Sprite;
      
      public function Preloader()
      {
       if (stage)
       {
        stage.scaleMode = StageScaleMode.NO_SCALE;
        stage.align = StageAlign.TOP_LEFT;
       }
       addEventListener(Event.ENTER_FRAME, checkFrame);
       loaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
       loaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
       
       // TODO show loader
       bar = new Sprite();
       bar.graphics.lineStyle(1, 0x4444ff, 1, true);
       bar.graphics.drawRect(0, 0, 100, 6);
       bar.x = stage.stageWidth / 2 - bar.width / 2;
       bar.y = stage.stageHeight / 2 - bar.height / 2;
       addChild(bar);
      }
      
      private function ioError(e:IOErrorEvent):void
      {
       trace(e.text);
      }
      
      private function progress(e:ProgressEvent):void
      {
       // TODO update loader
       bar.graphics.lineStyle(0, 0, 0);
       bar.graphics.beginFill(0x8888ff);
       bar.graphics.drawRect(1, 1, (e.bytesLoaded / e.bytesTotal) * 98, 4);
       bar.graphics.endFill();
       trace("loading:" + (e.bytesLoaded / e.bytesTotal) * 100);
       trace(e.bytesTotal);
      }
      
      private function checkFrame(e:Event):void
      {
       if (currentFrame == totalFrames)
       {
        stop();
        loadingFinished();
       }
      }
      
      private function loadingFinished():void
      {
       removeEventListener(Event.ENTER_FRAME, checkFrame);
       loaderInfo.removeEventListener(ProgressEvent.PROGRESS, progress);
       loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioError);
       
       // TODO hide loader
       removeChild(bar);
       bar = null;
       
       startup();
      }
      
      private function startup():void
      {
       var mainClass:Class = getDefinitionByName("Main") as Class;
       stage.addChild(new mainClass() as DisplayObject);

       parent.removeChild(this);
      }
     
     }

    }

    再来看看主程序怎么用的:

    package
    {            
        import flash.display.Sprite;            
        import flash.events.Event;            
                                 
        [Frame(factoryClass="Preloader")]            
        public class Main extends Sprite            
        {            
            public function Main()            
            {            
                super();            
                this.addEventListener(Event.ADDED_TO_STAGE, onadded);            
            }            
                                 
            protected function onadded(event:Event):void
            {            
                trace("真实内容加载到舞台");            
            }            
        }            
    }
  • 相关阅读:
    各类运算符练习
    用if语句把24小时制转换成12小时制
    Android课程---优化ListView列表视图
    Android课程---关于ListView列表视图的学习
    Android课程---时间日期对话框
    Android课程---关于对话框的学习
    Android课程---用进度条改变图片透明度
    Android课程---进度条及菜单的学习
    Android课程---final关键字
    Android课程---日历选择器和时间选择器
  • 原文地址:https://www.cnblogs.com/autumndawn/p/3447575.html
Copyright © 2011-2022 走看看