今天很郁闷,就是从Vector里边删除具有指定url的ImageLoader。用for循环直接删除竟然删除不干净。
这里有一个imageList. var imageList:Vector.<ImageLoader> = new Vector.<ImageLoader>();
imageList里边放了几个ImageLoader,其中,有相同url的ImageLoader有好几个。这个时候,我们指定要删除具有相同url的ImageLoader,并卸载ImageLoader。
怎么做呢。看起来很简单,自己却走入了误区,没有将ImageLoader删除干净。这里调用 clearLoader(_url);
public function clearLoader(_url:String):void { var i:int = 0; var imageLoader:ImageLoader; for (i = 0; i < imageList.length; i++) { if (imageList[i].url == _url) { imageLoader = imageList[i] as ImageLoader; imageList.splice(i,1); imageLoader.dispose(true); imageLoader = null; } } }
可是,这样清理不干净。那怎么办呢。这里有两个方法。第一个方法是倒着遍历,第二个方法是建立两个临时数组:tempArr,和needArr.用tempArr记录下需要删除的具有相同url的ImageLoader,needArr记录不需要删除的。
然后,清空imageList,将needArr给imageList。然后清除整个tempArr,卸载里边的ImageLoader就可以了。其实,有了第一个方法,第二个方法就没有什么意义了。
public function clearLoader(_url:String):void { var i:int = 0; var imageLoader:ImageLoader; var tempArr:Vector.<ImageLoader > = new Vector.<ImageLoader > ; var needArr:Vector.<ImageLoader > = new Vector.<ImageLoader > ; for (i = 0; i < imageList.length; i++) { if (imageList[i].url == _url) { tempArr.push(imageList[i]); } else { needArr.push(imageList[i]); } } imageList.splice(0,imageList.length); imageList = imageList.concat(needArr); for(i =0 ; i <tempArr.length ;i++) { imageLoader = tempArr[i] as ImageLoader; tempArr.shift(); imageLoader.dispose(true); imageLoader = null; } }
下边,是完整的代码,不以ImageLoader为对象,就以简单的Object,给Object的属性url和方法dispose。
package { import flash.display.Sprite; import flash.events.MouseEvent; import flash.text.TextField; import flash.text.TextFieldAutoSize; public class ArrDemo extends Sprite { private var arrList:Array = []; private var txt:TextField; public function ArrDemo() { var but:Button = new Button(); but.label = "点击看看"; but.x =10; but.y =10; addChild(but); but.buttonMode = true; but.addEventListener(MouseEvent.CLICK,clickHandler); txt = new TextField(); txt.autoSize = TextFieldAutoSize.LEFT; txt.border = true; txt.multiline = true; txt.wordWrap = true; txt.width = stage.stageWidth - 20; txt.height = stage.stageHeight - 20 -but.y - but.height; addChild(txt); txt.x =10; txt.y = but.y + but.height +10; txt.textColor = 0x00ffff; Init(); } private function clickHandler(e:MouseEvent):void { clearObjC(1); } private function show(str:String):void { txt.appendText(str + "--"); } private function clearObj(num:int):void { var i:int = 0; for(i =0 ; i< arrList.length ; i++) { if(arrList[i].url == num) { arrList.splice(i,1); } } show("\n"); show(String(arrList.length)); show("\n"); for(i =0 ; i< arrList.length ; i++) { show(arrList[i].url); } } private function clearObjC(num:int):void { var i:int =0; var tempArr:Array = []; var needArr:Array = []; for(i =0 ; i< arrList.length ; i++) { if(arrList[i].url == num) { tempArr.push(arrList[i]); } else { needArr.push(arrList[i]); } } arrList.splice(0,arrList.length); arrList = arrList.concat(needArr); for(i =0 ;i <tempArr.length; i++) { var obj:Object = tempArr[i]; obj.dispose(); tempArr.splice(i,1); } show("\n"); show(String(arrList.length)); show("\n"); for(i =0 ; i< arrList.length ; i++) { show(arrList[i].url); } } private function Init():void { for(var i:int = 0; i<30 ;i ++) { var obj:Object ={}; obj.url = Math.floor(Math.random() * 3); obj.dispose = dispose; arrList.push(obj); show(obj.url); } } private function dispose():void { show("\n"); show("戒不掉你的温柔,每一次都想牵你的手!") } } } import flash.display.Sprite; import flash.text.TextField; import flash.text.TextFieldAutoSize; class Button extends Sprite { private var _sp:Sprite; private var _txt:TextField; public function Button():void { _sp = new Sprite(); addChild(_sp); draw(); } private function draw():void { _sp.graphics.clear(); _sp.graphics.beginFill(0x00ffff,1); _sp.graphics.drawRoundRect(0,0,72,22,5,5); _sp.graphics.endFill(); } public function set label(str:String):void { if(!_txt) { _txt = new TextField(); _txt.autoSize = TextFieldAutoSize.LEFT; _txt.textColor =0x0000ff; _txt.mouseEnabled = false; } _txt.text = str; _sp.removeChildren(); _txt.x = _sp.width * 0.5 - _txt.textWidth * 0.5 ; _txt.y = _sp.height * 0.5 - _txt.textHeight * 0.5 -2; _sp.addChild(_txt); } }