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 ( "真实内容加载到舞台" );            
         }            
     }            
}

转载于:https://www.cnblogs.com/autumndawn/p/3447575.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值