AS3工程中的Loading的应用
今天又来介绍Loading...(-_-!!!...好像整天都介绍Loading..希望没误导各位...)
首先..由于AS工程没有帧..所以不能用常用的方法来做Loading..
这里介绍的方法使用元标签Frame(应该是元标签吧?还是叫元数据标签)
网上对Frame的介绍是...使用指定的类替换文档类...
并把其它的东西都丢到了该类的第二帧...
概念就不多说了...英文好的可以看看这个文章...http://www.bit-101.com/blog/?p=946
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;
}
}
}
}