我们做项目的时候,有时候就因为load的异步郁闷。as3也没有其他语言里边的sleep。要想不烦恼,不郁闷,怎么办呢。那我们就将用一定的途径来实现。既然我们load的东西,都在load里边。那么我们只要将load添加到显示列表不就完事了。特别对于加载图片最有效果。如果加载的是swf动画,要让swf播放怎么办呢。那就在你获得loader后,就用一个时间侦听来观察loader的content对象或contentLoaderInfo.content对象。如果其不为null,那么就说明你之前load的对象已经load完毕了。这个时候你就可以用它了,让它播放吧。
as_code:
View Code
package com.vini123 { import flash.display.Sprite; import flash.display.Loader; import flash.net.URLRequest; import flash.events.Event; import flash.events.TimerEvent; import flash.utils.Timer; import flash.display.MovieClip; import flash.system.System; import flash.events.MouseEvent; import com.vini123.utils.ViniBtn; public class Main extends Sprite { private var myLoader:Loader; private var myTimer:Timer; private var testLoader:Loader; private var tempNum:int = 0; private var btnArr:Array = ["添加SWF","删除SWF"]; private var allBtn:Vector.<ViniBtn>=new Vector.<ViniBtn>(); public function Main() { myTimer = new Timer(1000,0); myTimer.addEventListener(TimerEvent.TIMER,timerHandler); for (var i:int=0; i<btnArr.length; i++) { allBtn.push(new ViniBtn(btnArr[i])); allBtn[i].buttonMode = true; allBtn[i].addEventListener(MouseEvent.ROLL_OVER,overHandler); allBtn[i].addEventListener(MouseEvent.ROLL_OUT,outHandler); allBtn[i].addEventListener(MouseEvent.CLICK,clickHandler); trace(allBtn[i].width); allBtn[i].x =10 allBtn[i].y = 5 allBtn[i].id = i; addChild(allBtn[i]); } allBtn[1].visible = false; } private function overHandler(e:MouseEvent) { e.currentTarget.overHandler(); } private function outHandler(e:MouseEvent) { e.currentTarget.outHandler(); } private function clickHandler(e:MouseEvent) { switch (e.currentTarget.id) { case 0 : allBtn[0].visible = false; allBtn[1].visible = true; getLoader(); break; case 1 : allBtn[0].visible = true; allBtn[1].visible = false; removeLoader(); break; } } private function getSwf(url:String):Loader { if (myLoader==null) { myLoader=new Loader(); } var i:int=0; myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler); myLoader.load(new URLRequest(url)); return myLoader; } private function completeHandler(e:Event) { trace("加载结束!!"); } private function timerHandler(e:TimerEvent) { var num1:Number=System.freeMemory/(1024*1024); var num2:Number=System.totalMemoryNumber/(1024*1024); var num3:Number=System.totalMemory/(1024*1024); var num4:Number=System.privateMemory/(1024*1024); trace(num1,num2,num3,num4); if (testLoader!=null) { echo(); if (testLoader.contentLoaderInfo.content != null) { tempNum++; if (tempNum>(testLoader.contentLoaderInfo.content as MovieClip).totalFrames) { tempNum = 1; } (testLoader.contentLoaderInfo.content as MovieClip).gotoAndStop(tempNum); } } } private function getLoader() { tempNum = 0; testLoader = getSwf("木兰花.swf"); addChild(testLoader); myLoader.unload(); myLoader = null; myTimer.start(); echo(); } private function removeLoader() { removeChild(testLoader); testLoader = null; myTimer.stop(); } private function echo() { if (testLoader!=null) { trace(testLoader); trace(testLoader.content); trace(testLoader.contentLoaderInfo); trace(testLoader.contentLoaderInfo.content); trace("_________________"); } } } }
View Code
package com.vini123.utils { import flash.display.Sprite; import flash.text.TextField; import flash.text.TextFormat; import flash.text.TextFieldAutoSize; public class ViniBtn extends Sprite { private var sp:Sprite = new Sprite ; private var myTxtFormat:TextFormat = new TextFormat ; private var txt:TextField = new TextField ; private var xGap:int = 10; private var yGap:int = 6; public var id:int = 0; public function ViniBtn(str:String="") { createBtn(str); } private function createBtn(str:String) { myTxtFormat.font = "黑体"; myTxtFormat.bold = false; myTxtFormat.size = 13; myTxtFormat.color = 0x000000; txt.text = str; txt.x = xGap * 0.5 - 2; txt.y = yGap * 0.5 - 1; txt.mouseEnabled = false; txt.autoSize=TextFieldAutoSize.LEFT; txt.setTextFormat(myTxtFormat); sp.graphics.lineStyle(2,0x00ffff,0.8); sp.graphics.beginFill(0xffffff,1); sp.graphics.drawRoundRect(0,0,txt.textWidth + xGap,txt.textHeight + yGap,8,8); sp.graphics.endFill(); addChild(sp); sp.addChild(txt); } public function overHandler() { myTxtFormat.color = 0x00ffff; txt.setTextFormat(myTxtFormat); } public function outHandler() { myTxtFormat.color = 0x000000; txt.setTextFormat(myTxtFormat); } } }