摘要:要想在Flex的AS工程,实现加载进度的显示, 只需添加一个类(继承自MovieClip) 实现预加载的相关功能,然后把这个类设置成默认启动, 主逻辑写在主类上即可。
Adobe Flex 工程的 Application 为2帧动画,第1帧为 Pre-load,第2帧为 Application,如果想替换 Adobe Flex 原有的 Pre-loader,那么制作将非常的方便。制作一个新的 preload Component,在 Application 的 preloader 属性中进行相关的引用就可以完成。
这次讨论的不是在 AS3 工程中直接加载 SWF,而是通过 getDefinitionByName(name:String) 的方式对 Application 进行动态加载。
例子是 Unique Instance,详细请见 Application.as 关于 Instance 的写法。
- 新建ActionScript工程:PreloaderApp
- 设定Default Application:PreloaderApp.as
- 新建需要动态加载的主工程代码:Application.as
- 设置工程属性:Properties->ActionScript Compiler->Additional compiler arguments: -frame start Application
代码:
PreloaderApp.as
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.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.utils.getDefinitionByName;
public class PreloaderApp extends MovieClip
{
/**
* PreloaderApp Constructor.
*/
public function PreloaderApp()
{
this.addEventListener(Event.ADDED_TO_STAGE, handleToStage);
}
/**
* Handle ADDED_TO_STAGE event.
* @param event
*/
private function handleToStage(event:Event):void
{
this.removeEventListener(Event.ADDED_TO_STAGE, handleToStage);
//stage setting
stage.showDefaultContextMenu = false;
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
createChildren();
addEventListener(Event.ENTER_FRAME, loadApplication);
this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, handleProgress);
this.loaderInfo.addEventListener(Event.COMPLETE, handleComplete);
}
private var _preLoadingText:TextField;
/**
* createChildren
*/
private function createChildren():void
{
_preLoadingText = new TextField();
_preLoadingText.text = "Loading...";
_preLoadingText.textColor = 0x000000;
_preLoadingText.x = (stage.stageWidth / 2) - (_preLoadingText.width / 2);
_preLoadingText.y = (stage.stageHeight / 2) - (_preLoadingText.height / 2);
_preLoadingText.autoSize = TextFieldAutoSize.CENTER;
addChild(_preLoadingText);
}
/**
* Handle progress.
* @param event
*/
private function handleProgress(event:ProgressEvent):void
{
var percent:int = Math.floor(event.bytesLoaded / event.bytesTotal * 100);
_preLoadingText.text = "Loading..." + percent + "%";
}
/**
* Handle load complete.
* @param event
*/
private function handleComplete(event:Event):void
{
this.loaderInfo.removeEventListener(ProgressEvent.PROGRESS, handleProgress);
this.loaderInfo.removeEventListener(Event.COMPLETE, handleComplete);
}
/**
* Load main application.
* @param e
*/
private function loadApplication(event:Event):void
{
if (currentFrame == totalFrames)
{
removeEventListener(Event.ENTER_FRAME, loadApplication);
stop();
var cls:Class = getDefinitionByName("Application") as Class;
addChild(new cls() as DisplayObject);
removeChild(_preLoadingText);
_preLoadingText = null;
}
}
}
}
{
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.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.utils.getDefinitionByName;
public class PreloaderApp extends MovieClip
{
/**
* PreloaderApp Constructor.
*/
public function PreloaderApp()
{
this.addEventListener(Event.ADDED_TO_STAGE, handleToStage);
}
/**
* Handle ADDED_TO_STAGE event.
* @param event
*/
private function handleToStage(event:Event):void
{
this.removeEventListener(Event.ADDED_TO_STAGE, handleToStage);
//stage setting
stage.showDefaultContextMenu = false;
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
createChildren();
addEventListener(Event.ENTER_FRAME, loadApplication);
this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, handleProgress);
this.loaderInfo.addEventListener(Event.COMPLETE, handleComplete);
}
private var _preLoadingText:TextField;
/**
* createChildren
*/
private function createChildren():void
{
_preLoadingText = new TextField();
_preLoadingText.text = "Loading...";
_preLoadingText.textColor = 0x000000;
_preLoadingText.x = (stage.stageWidth / 2) - (_preLoadingText.width / 2);
_preLoadingText.y = (stage.stageHeight / 2) - (_preLoadingText.height / 2);
_preLoadingText.autoSize = TextFieldAutoSize.CENTER;
addChild(_preLoadingText);
}
/**
* Handle progress.
* @param event
*/
private function handleProgress(event:ProgressEvent):void
{
var percent:int = Math.floor(event.bytesLoaded / event.bytesTotal * 100);
_preLoadingText.text = "Loading..." + percent + "%";
}
/**
* Handle load complete.
* @param event
*/
private function handleComplete(event:Event):void
{
this.loaderInfo.removeEventListener(ProgressEvent.PROGRESS, handleProgress);
this.loaderInfo.removeEventListener(Event.COMPLETE, handleComplete);
}
/**
* Load main application.
* @param e
*/
private function loadApplication(event:Event):void
{
if (currentFrame == totalFrames)
{
removeEventListener(Event.ENTER_FRAME, loadApplication);
stop();
var cls:Class = getDefinitionByName("Application") as Class;
addChild(new cls() as DisplayObject);
removeChild(_preLoadingText);
_preLoadingText = null;
}
}
}
}
Application.as
package
{
import flash.display.Sprite;
import flash.events.Event;
public class Application extends Sprite
{
private static var _instance:Application;
/**
* Unique instance.
* @return
*/
public static function getInstance():Application
{
return _instance;
}
/**
* Application Constructor.
*/
public function Application()
{
super();
addEventListener(Event.ADDED_TO_STAGE, initOnStage);
}
/**
* Handle ADDED_TO_STAGE event.
* @param event
*/
protected function initOnStage(event:Event):void
{
//remove ADDED_TO_STAGE listener.
this.removeEventListener(Event.ADDED_TO_STAGE, initOnStage);
//Set unique instance.
if (!_instance)
_instance = this;
//...
}
}
}
{
import flash.display.Sprite;
import flash.events.Event;
public class Application extends Sprite
{
private static var _instance:Application;
/**
* Unique instance.
* @return
*/
public static function getInstance():Application
{
return _instance;
}
/**
* Application Constructor.
*/
public function Application()
{
super();
addEventListener(Event.ADDED_TO_STAGE, initOnStage);
}
/**
* Handle ADDED_TO_STAGE event.
* @param event
*/
protected function initOnStage(event:Event):void
{
//remove ADDED_TO_STAGE listener.
this.removeEventListener(Event.ADDED_TO_STAGE, initOnStage);
//Set unique instance.
if (!_instance)
_instance = this;
//...
}
}
}