[AS3]as3代码删除数组指定的Object
今天很郁闷,就是从Vector里边删除具有指定url的ImageLoader。用for循环直接删除竟然删除不干净
今天很郁闷,就是从Vector里边删除具有指定url的ImageLoader。用for循环直接删除竟然删除不干净。
这里有一个imageList. var imageList:Vector.<ImageLoader> = new Vector.<ImageLoader>();
imageList里边放了几个ImageLoader,其中,有相同url的ImageLoader有好几个。这个时候,我们指定要删除具有相同url的ImageLoader,并卸载ImageLoader。
怎么做呢。看起来很简单,自己却走入了误区,没有将ImageLoader删除干净。这里调用 clearLoader(_url);
- 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);
- arrListarrList = 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);
- }
- }
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例
请稍候...