Flash AS3 连续加载外部若干图片
//载入图片
public function loadClassBgPic():void
{
//载入图片没有完毕
bloadBGover = false;
//从xml数据中读取并计算总的下载量
bmpUrlArr.push(classXml["class"]["pad"]["background"]);
bmpUrlArr.push(classXml["class"]["title"]["background"]);
for(var i:uint = 0; i < classXml["class"]["page"].@count; i++)
{
bmpUrlArr.push(classXml["class"]["page"]["content"][i]["background"]);
}
itotalBmpCount = bmpUrlArr.length;
iloadedBmpCount = 0;
//载入第一个图片
loadBgPic(bmpUrlArr[iloadedBmpCount]);
}
public function loadBgPic(myURL:String):void
{
var rd:int = Math.random()*9999;
//var myURL:String="/file/imgs/upload/202301/31/phgnbyox3zu.jpg";//?"+rd;
var myRequest:URLRequest = new URLRequest(myURL);
var myLoader:Loader = new Loader();
var myVariables:URLVariables = new URLVariables();
myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,loadBgPicProgress);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadBgPicOK);
myLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loadBgPicError);
myRequest.method = URLRequestMethod.POST;
myLoader.load(myRequest);
//trace("start load");
}
public function loadBgPicProgress(e:ProgressEvent):void
{
var iloaded:uint = e.bytesLoaded;
var itotal:uint = e.bytesTotal;
text_progress.text = "正在载入图片数据..."+iloaded+" / "+itotal;
}
public function loadBgPicOK(e:Event):void
{
var mload:Loader = Loader(e.target.loader);
var bmp:Bitmap = e.target.content as Bitmap;
//mc_bg.addBmp(bmp);
//mc_bg.mc_content.addChild(bmp);
bmp.width = 800;
bmp.height = 600;
mload.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,loadBgPicProgress);
mload.contentLoaderInfo.removeEventListener(Event.COMPLETE,loadBgPicOK);
//图片载入完毕,存储到MC数组中
bmpArr[iloadedBmpCount] = new MovieClip();
bmpArr[iloadedBmpCount].addChild(bmp);
//将该图片mc设定到背景里面去
mc_bg.addChild(bmpArr[iloadedBmpCount]);
bmpArr[iloadedBmpCount].visible = false;
iloadedBmpCount++;
if(iloadedBmpCount < itotalBmpCount)
{
//载入下一个图片
loadBgPic(bmpUrlArr[iloadedBmpCount]);
}else
{
iloadedBmpCount = 0;
bloadBgOver = true;
}
showClassBgOver();
}
public function loadBgPicError(e:IOErrorEvent):void
{
//图片载入失败,创建空mc
bmpArr[iloadedBmpCount] = new MovieClip();
//将该图片mc设定到背景里面去
mc_bg.addChild(bmpArr[iloadedBmpCount]);
bmpArr[iloadedBmpCount].visible = false;
var txt:TextField = new TextField();
txt.text = "该图片载入失败";
bmpArr[iloadedBmpCount].addChild(txt);
iloadedBmpCount++;
//载入下一个图片
if(iloadedBmpCount < itotalBmpCount)
{
//载入下一个图片
loadBgPic(bmpUrlArr[iloadedBmpCount]);
}else
{
iloadedBmpCount = 0;
bloadBgOver = true;
}
showClassBgOver();
}
//载入背景图片完毕,根据当前所处状态切换显示不同背景
public function showClassBgOver():void
{
for(var j:uint = 0; j < bmpArr.length; j++)
{
bmpArr[j].visible = false;
}
switch(gameState)
{
case "classPad":
//题目面板
bmpArr[0].visible = true;
break;
case "inclass":
//题目主题界面
bmpArr[1].visible = true;
break;
case "classpage":
//课程页面,根据当前不同页号显示不同背景
var temp:uint = ipage + 2;
bmpArr[temp].visible = true;
break;
}
}
以上!
相关文章