PDA 个人数字助理

查看完全版本 : 一个可以在Flash6播放器中运行的MovieClipLoader 类


wiyiflash
2005-05-20, 09:06 PM
Flash7中的 MovieClipLoader类为我们加载影片提供了若干下载事件,方便了对加载过程的控制和调用。近来项目要增加一个flash6版本的,原来的flash7版本中大量用到此类,改起来实在麻烦,就自己写了个 MovieClipLoader类,可以说实现了MM的MovieClipLoader类中的所有功能,只是"onLoadInit"事件的确定我没有太大把握,因为我不太清楚MM是怎么确定一个影片初始化完毕的,只好以判断能否取出当前桢来确定,还望高手指教。使用的时候,要发布为 flash6,语法 2.0哦!

说明:
1、大家按Flash7里的 MovieClipLoader 类的用法调用即可
2、loadClip方法比 MM的类多了一个参数"update",如果您希望每次都加载最新版本的动画,请将update参数设为true ,否则会先查看IE缓存。如果是开发网络游戏的话,一定希望
某些文件是每次都下载最新的,此时就比较有用。


/*written by wiyiflash ,welcom to http://www.wiyiflash.com/bbs */
import mx.transitions.OnEnterFrameBeacon;
import mx.transitions.BroadcasterMX;
class MovieClipLoader {
//-----------------------------------------
//为MovieClipLoader增加事件广播功能
static var isBroadcaster = BroadcasterMX.initialize(MovieClipLoader.prototype, true);
static var isBeacon = OnEnterFrameBeacon.init();
//事件监听器数组
private var _listeners:Array;
//事件广播方法
public var broadcastMessage:Function;
//添加监听器
public var addListener:Function;
//移除监听器
public var removeListener:Function;

//-----------------------------------------
private var __target:MovieClip;
private var __loadedBytes:Number;
private var __totalBytes:Number;
private var __eventFunc:Function;
private static var __errors = ["URLNotFound", "LoadNeverCompleted"];
//-----------------------------------------
function MovieClipLoader() {
this._listeners = new Array();
}
function loadClip(url, target, update) {
//进程的参数数组:target,loadedBytes,totalBytes
__target = target;
__loadedBytes = 0;
__totalBytes = 0;
if (update == true) {
var t = (new Date()).getTime();
loadMovie(url+"?__mcl_t="+t, target);
} else {
loadMovie(url, target);
}
__eventFunc = __startFunc;
MovieClip.addListener(this);
}
function unloadClip(target) {

if(target.getBytesLoaded()<target.getBytesTotal()){
var code=1;
__returnErrorCode(code);
}
unloadMovie(target);
}
//-----------------------------------------
private function onEnterFrame() {
__eventFunc();
}
private function __startFunc() {
var t = __target.getBytesTotal();
if (t>4) {
broadcastMessage("onLoadStart", __target);
//设置totalBytes
__totalBytes = t;
__eventFunc = __progressFunc;
} else if (t == -1) {
//停止下载
MovieClip.removeListener(this);
//返回错误码
var code = 0;
__returnErrorCode(code);
}
}
private function __progressFunc() {
__loadedBytes = __target.getBytesLoaded();
var target = __target;
var loadedBytes = __loadedBytes;
var totalBytes = __totalBytes;
broadcastMessage("onLoadProgress", target, loadedBytes, totalBytes);
if (loadedBytes == totalBytes) {
__eventFunc = __completeFunc;
}
}
private function __completeFunc() {
broadcastMessage("onLoadComplete", __target);
__eventFunc = __initFunc;
}
private function __initFunc() {
if (__target._currentframe>=1) {
broadcastMessage("onLoadInit", __target);
MovieClip.removeListener(this);
}
}
//----------------------------------------
private function __returnErrorCode(code) {
broadcastMessage("onLoadError", __target, __errors[code]);
MovieClip.removeListener(this);
}
}

ai829
2005-05-22, 11:05 AM
写的非常的好~~~~~~~~

皮皮狼
2005-06-15, 02:05 PM
学习.