ActionScript 3 用于加载和预加载外部闪存或图像的通用类
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ActionScript 3 用于加载和预加载外部闪存或图像的通用类相关的知识,希望对你有一定的参考价值。
/**
* Common class for loading an external flash or image
*
* @class UndoLoader
* @author Giovambattista Fazioli <g.fazioli@undolog.com>
* @web http://www.undolog.com
* @version 1.0
*
*/
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.net.*;
import flash.system.*;
//
public class UndoLoader extends MovieClip {
private var _preloader:Preloader;
private var _loader:Loader;
private var _bar:MovieClip;
private var _perc:TextField;
/**
* Class constructor
*
* @param void
* @return void
*/
public function UndoLoader() {
stage.scaleMode = StageScaleMode.NO_SCALE
stage.align = StageAlign.TOP_LEFT;
addEventListener( Event.ADDED_TO_STAGE, init );
}
/**
* Init bar and content. Trigged when added to stage
*
* @param {event} e Passed from addEventListener(). Can be null
* @return void
* @private
*/
private function init(e:Event = null):void {
initBar();
loadContent();
}
/**
* Create the preloader movie and init it.
*
* @param void
* @return void
* @private
*/
private function initBar():void {
_preloader = new Preloader();
_bar = _preloader.bar;
_bar.scaleX = 0;
_perc = _preloader.perc;
_perc.text = '0%';
_preloader.x = (stage.stageWidth - _preloader.width)/2;
_preloader.y = (stage.stageHeight - _preloader.height)/2;
addChild( _preloader );
}
/**
* Creare a Loader() object and init PROGRESS and COMPLETE event
*
* @param void
* @return void
* @private
*/
private function loadContent():void {
_loader = new Loader();
_loader.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS, inProgress );
_loader.contentLoaderInfo.addEventListener( Event.COMPLETE, onComplete );
_loader.load( new URLRequest( this.loaderInfo.parameters.filename ) );
addChild( _loader );
}
/**
* Trigged when loading in progress. Move the progress bar and set
* the text string
*
* @param {ProgressEvent} e ProgressEvent
* @return void
* @private
*/
private function inProgress(e:ProgressEvent = null):void {
var p:Number = Math.floor( (e.bytesLoaded / e.bytesTotal)*100 );
_perc.text = p + '%';
_bar.scaleX = p/100;
}
/**
* Trigged when loading is finished. Remove the preloader movie
* from the stage. Get the framerate from loaded movie and set
* framerate to main movie.
*
* @param {Event} e Event
* @return void
* @private
*/
private function onComplete(e:Event = null):void {
stage.frameRate = _loader.contentLoaderInfo.frameRate;
removeChild( _preloader );
}
}
以上是关于ActionScript 3 用于加载和预加载外部闪存或图像的通用类的主要内容,如果未能解决你的问题,请参考以下文章
ActionScript 3 外部文件的加载方法
ActionScript 3 基本的外部XML加载器
ActionScript 3 加载外部
ActionScript 3 从其他域加载外部swf
ActionScript 3 使用loader类加载外部swf
ActionScript 3 AS3加载外部文本文件