0
点赞
收藏
分享

微信扫一扫

AS3工程中Loading的应用(Frame标签的使用)


Demo1:
原理的话就是使用[frame()]标签,改变文档类指向。
默认程序入口:

package
{
import flash.display.Sprite;
import mx.core.BitmapAsset;

[Frame(factoryClass="SystemManager",label="hello")]

public class MyApplication extends Sprite {

   [Embed(source="test.png")]
   private var bigPicCls:Class;

   public function MyApplication() {
    var b:BitmapAsset = new bigPicCls() as BitmapAsset;
    addChild(b);
   }
}
}


被指向的主文档类:


package {
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.utils.getDefinitionByName;

public class SystemManager extends MovieClip 
{
   private var preLoader:PreLoader;

   public function SystemManager() {
    stage.scaleMode = StageScaleMode.NO_SCALE;
    stage.align = StageAlign.TOP_LEFT;

    stop();

    preLoader = new PreLoader();
    addChild(preLoader);

    preLoader.x = stage.stageWidth/2 - preLoader.width/2;
    preLoader.x = stage.stageHeight/2 - preLoader.height/2;

    loaderInfo.addEventListener(ProgressEvent.PROGRESS,progressHandle);
    loaderInfo.addEventListener(Event.COMPLETE,completeHandler);
   }

   private function progressHandle(e:ProgressEvent):void 
   {
    preLoader.setProgress(e.bytesLoaded,e.bytesTotal);
   }

   private function completeHandler(e:Event):void {
    loaderInfo.removeEventListener(ProgressEvent.PROGRESS,progressHandle);
    loaderInfo.removeEventListener(Event.COMPLETE,completeHandler);

    removeChild(preLoader);
    preLoader = null;

    nextFrame();

    initApplication();
   }

   private function initApplication():void {
    /**这里不能直接写成:
    var app:Application = new Application();
    这样的由于引用到 Application,Application中所有的资源都会被编译到第一帧来
    这样的话 PreLoader就没有意义了,你也看不到PreLoader,就跳到第二帧了
    **/
    var appCls:Class = getDefinitionByName("MyApplication") as Class;
    var app:DisplayObject = new appCls() as DisplayObject;
    addChild(app);
   }
}
}


进度条:


package 
{
import flash.display.Sprite;
import flash.text.TextField;

public class PreLoader extends Sprite 
{
   public function PreLoader() 
   {
    var t:TextField = new TextField();
    t.border = true;
    t.text = "loading......";

    addChild(t);
   }

   public function setProgress(loaded:uint,total:uint):void 
   {
    var t:TextField = getChildAt(0) as TextField;
    t.text = "load: "+ loaded/1000 + "/" + total/1000;

    trace("load: "+ loaded/1000 + "/" + total/1000);
   }
}
}


Demo2:


由于AS工程没有帧..所以不能用常用的方法来做Loading,这里介绍的方法使用元标签Frame(应该是元标签吧?还是叫元数据标签),网上对Frame的介绍是...使用指定的类替换文档类,并把其它的东西都丢到了该类的第二帧。


[quote]Preloader类


  package


  {

  import flash.display.DisplayObject;

  import flash.display.MovieClip;

 import flash.display.StageScaleMode;

  import flash.events.Event;

  import flash.events.ProgressEvent;

  import flash.text.TextField;

  import flash.text.TextFormat;

  import flash.utils.getDefinitionByName;

  /**


  * 加载类,由于原内容会放到此类的第二帧~所以需要使用MovieClip;


  * @author L4cd.Net


  */

  public class Preloader extends MovieClip

  {

  [Embed(source="l4cd_48_48.jpg")]

   private var LogoClass:Class;

  private var text:TextField;

  public function Preloader()

  {

  //不多说了

  stage.scaleMode = StageScaleMode.NO_SCALE;

  stage.showDefaultContextMenu = false;

  //显示一张图片..非必要

  with(addChild(new LogoClass()))

  {

  x=(500-48)/2;

  y=(380-48)/2;

  }

  //加一个textField显示进度

  text = addChild(new TextField()) as TextField;

  with(text)

  {

  defaultTextFormat = new TextFormat("宋体",12,0,null,null,null,null,null,"center");


  mouseEnabled = false;


  height = 16;


  x = (500-100)/2;


  y = 35+(380-15)/2;


  }


  //重要~侦听~


  loaderInfo.addEventListener(ProgressEvent.PROGRESS,progress);


  loaderInfo.addEventListener(Event.COMPLETE,complete);


  }


  //显示进度


  private function progress(e:ProgressEvent):void


  {


  text.text = (e.bytesLoaded/e.bytesTotal*100).toFixed(2)+"% Loaded";


  }


  private function complete(e:Event):void


  {


  //重要..加载完后...我们跳转到第二帧


  gotoAndStop(2);


  //获取文档类...然后显示,对于此步操作..有多种方法..有人习惯直接把原文档类addChild到此类当中..


  var mainClass:Class = Class(getDefinitionByName("AsProjectLoadingExample"));


  stage.addChild(new mainClass() as DisplayObject);


  //删除此类


  destroy();


  }


  private function destroy():void


  {


  loaderInfo.removeEventListener(ProgressEvent.PROGRESS,progress);


  loaderInfo.removeEventListener(Event.COMPLETE,complete);


  parent.removeChild(this);


  }


  }


  }


  AsProjectLoadingExample类


  package {


  import flash.display.Bitmap;


  import flash.display.BitmapData;


  import flash.display.Sprite;


  import flash.geom.Matrix;


  [SWF(width="500",height="380",backgroundColor="#FFFFFF",frameRate="24")]


  //注意此行,Preloader为类名


  [Frame(factoryClass="Preloader")]


  public class AsProjectLoadingExample extends Sprite


  {


  [Embed(source="CD艳照.png")]


  private var ImageClass:Class;


  //嵌入一个2M的文件以显示加载效果


  [Embed(source="something.wma",mimeType="application/octet-stream")]


  private var SomeClass:Class;

 public function AsProjectLoadingExample()

  {

 //马赛格ing...

  var bmp:Bitmap = new ImageClass();

 var bit:BitmapData = new BitmapData(bmp.width/10,bmp.height/10,true,0);

  bit.draw(bmp,new Matrix(.1,0,0,.1));

  with (addChild(new Bitmap(bit)))

  {

  scaleX = scaleY = 10;

   }

  }

  }

 }

[/quote]


举报

相关推荐

0 条评论