flash AS +xml 加载图片(使用MovieClipLoader实现LOADING)

该flash一共有两层,一层的第一帧用来添加要使用的按钮(btnPlay,btnBack)和动态文本框(其变量为ratio,note);二层的第一帧用来添加AS(如下所示):
stop();
//舞台设置为450*300;配置文件为configuration1.xml;
var doc = new XML();
sWidth = 450;
sHeight = 300;
doc.ignoreWhite = true;
myXML = null;
rootNode = null;
nodeNum = 0;
path = \"configuration1.xml\";
stopFlag = false;
overNum = 0;
totalWidth = 0;
posStr = \"\";
ratio = 0;
note = \"\";
tp = 1;
this.onLoad = function() {
doc.load(path);//加载xml文件。
};
doc.onLoad = function(success) {
if (success) {
myXML = doc.firstChild;
rootNode = myXML.firstChild;
node = rootNode;
k = 1;
while (node.nextSibling) {
_root[\"myload\"+k] = new MovieClipLoader();
_root[\"myListener\"+k] = new Object();
_root[\"myListener\"+k].onLoadStart = function(target_mc) {//加载开始时发生该事件
//trace(\"start \"+target_mc._parent._name);
};
_root[\"myListener\"+k].onLoadProgress = function(target_mc, loadedBytes, totalBytes) {//在onLoadStart和onLoadComplete之间的下载过程,要show loading效果的话,就要使用该事件。如果下载速度很快的话是看不到过程的哦。
_root.ratio = Math.round(loadedBytes/totalBytes*100);
_root.note = \"loading \"+target_mc._parent._name.substr(3, 1)+\"th pic\";
};
_root[\"myListener\"+k].onLoadInit = function(target_mc) {//MOVIECLIP被加载完毕后播放第一帧的时候发生该事件。
var tmpK = Number(target_mc._parent._name.substr(3, 1));
target_mc._parent._x = 30;
target_mc._parent._y = 30;
target_mc._parent._alpha = 0;
_root[\"pW\"+tmpK] = target_mc._width;
_root[\"pH\"+tmpK] = target_mc._height;
target_mc._parent._width = 450/_root[\"pH\"+tmpK]*300;
target_mc._parent._height = 200;
};
_root[\"myListener\"+k].onLoadError = function(target_mc, errorcode) {//加载过程发生错误时发生该事件
//trace(\"error\"+errorcode);
_root.note = target_mc._parent._name.substr(3, 1)+\"cann\'t be loaded!\";
};
_root[\"myListener\"+k].onLoadComplete = function(target_mc) {//加载过程完毕发生该事件。
var tmp = Number(target_mc._parent._name.substr(3, 1))+1;
_root.note = (tmp-1)+\"th pic \"+\"loaded!\";
_root[\"myload\"+tmp].loadClip(_root[\"urlForPic\"+tmp], _root[\"pic\"+tmp].picChild);
};
_root[\"myload\"+k].addListener(_root[\"myListener\"+k]);
_root.createEmptyMovieClip(\"pic\"+k, k*10);
_root[\"pic\"+k].createEmptyMovieClip(\"picChild\", k*100);//为什么要再创建一个空的movieclip呢,是因为只能使用被加载的movieclip(_root[\"pic\"+k].picChild)的_parent(_root[\"pic\"+k])才能控制被加载的图片或swf的属性,如width和height,
_root[\"pic\"+k].picChild._x = 0;
_root[\"pic\"+k].picChild._y = 0;
_root[\"pic\"+k].onRollOver = function() {
_root.overNum = Number(this._name.substr(3, 1));
_root.stopFlag = true;
};
_root[\"pic\"+k].onEnterFrame = function() {
if (tp == Number(this._name.substr(3, 1)) && this._alpha<100) {
//if(tp==1)trace(this._alpha);
this._alpha = int(this._alpha+2);
} else if (this._alpha>0) {
this._alpha = int(this._alpha-2);
}
};
_root[\"pic\"+k].onRollOut = function() {
_root.stopFlag = false;
overNum = 0;
};
_root[\"pic\"+k].onRelease = function() {
getURL(_root[\"str\"+this._name.substr(3, 1)]);
};
_root[\"str\"+k] = node.nextSibling.attributes.url;
_root[\"txt\"+k] = node.nextSibling.attributes.des;
_root[\"urlForPic\"+k] = node.nextSibling.attributes.pic;
node = node.nextSibling;
k++;
}
} else {
trace(\"There was an error parsing the XML data\");
}
nodeNum = k-1;
_root[\"myload\"+1].loadClip(_root[\"urlForPic\"+1], _root[\"pic\"+1].picChild);
};
_root.btnPlay.onRelease = function() {
tp++;
if (tp>nodeNum) {
tp = 1;
}
};
_root.btnBack.onRelease = function() {
tp--;
if (tp<=0) {
tp = nodeNum;
}
};



configuration1.xml的结构(贴出原码会被浏览器给解析并显示出来):

confiugration
                 entry
                         des
                         url //点击图片要转到的地址。
                         pic //所加载图片的地址
posted @ 2007-11-06 12:39  狂歌  阅读(1319)  评论(0编辑  收藏  举报