·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]as3代码删除数组指定的Object

时间:2016-01-07 08:57酷播
今天很郁闷,就是从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);

  1. package 
  2. { 
  3.     import flash.display.Sprite; 
  4.     import flash.events.MouseEvent; 
  5.     import flash.text.TextField; 
  6.     import flash.text.TextFieldAutoSize; 
  7.      
  8.     public class ArrDemo extends Sprite 
  9.     { 
  10.         private var arrList:Array = []; 
  11.         private var txt:TextField; 
  12.          
  13.         public function ArrDemo() 
  14.         { 
  15.             var but:Button = new Button(); 
  16.             but.label = "点击看看"; 
  17.             but.x =10; 
  18.             but.y =10; 
  19.             addChild(but); 
  20.             but.buttonMode = true; 
  21.             but.addEventListener(MouseEvent.CLICK,clickHandler); 
  22.              
  23.             txt = new TextField(); 
  24.             txt.autoSize = TextFieldAutoSize.LEFT; 
  25.             txt.border = true; 
  26.             txt.multiline = true; 
  27.             txt.wordWrap = true; 
  28.              
  29.             txt.width = stage.stageWidth - 20; 
  30.             txt.height = stage.stageHeight - 20 -but.y - but.height; 
  31.  
  32.             addChild(txt); 
  33.             txt.x =10; 
  34.             txt.y = but.y + but.height +10; 
  35.             txt.textColor = 0x00ffff; 
  36.              
  37.             Init(); 
  38.         } 
  39.          
  40.         private function clickHandler(e:MouseEvent):void 
  41.         { 
  42.             clearObjC(1); 
  43.         } 
  44.          
  45.         private function show(str:String):void 
  46.         { 
  47.             txt.appendText(str + "--"); 
  48.         } 
  49.          
  50.         private function clearObj(num:int):void 
  51.         { 
  52.             var i:int = 0; 
  53.             for(i =0 ; i< arrList.length ; i++) 
  54.             { 
  55.                 if(arrList[i].url == num) 
  56.                 { 
  57.                     arrList.splice(i,1); 
  58.                 } 
  59.             } 
  60.              
  61.             show("\n"); 
  62.             show(String(arrList.length)); 
  63.             show("\n"); 
  64.             for(i =0 ; i< arrList.length ; i++) 
  65.             { 
  66.                 show(arrList[i].url); 
  67.             } 
  68.         } 
  69.          
  70.         private function clearObjC(num:int):void 
  71.         { 
  72.             var i:int =0; 
  73.             var tempArr:Array = []; 
  74.             var needArr:Array = []; 
  75.              
  76.             for(i =0 ; i< arrList.length ; i++) 
  77.             { 
  78.                 if(arrList[i].url == num) 
  79.                 { 
  80.                     tempArr.push(arrList[i]); 
  81.                 } 
  82.                 else 
  83.                 { 
  84.                     needArr.push(arrList[i]); 
  85.                 } 
  86.             } 
  87.              
  88.             arrList.splice(0,arrList.length); 
  89.             arrListarrList = arrList.concat(needArr); 
  90.              
  91.             for(i =0 ;i <tempArr.length; i++) 
  92.             { 
  93.                 var obj:Object = tempArr[i]; 
  94.                 obj.dispose(); 
  95.                 tempArr.splice(i,1);  
  96.             } 
  97.              
  98.             show("\n"); 
  99.             show(String(arrList.length)); 
  100.             show("\n"); 
  101.             for(i =0 ; i< arrList.length ; i++) 
  102.             { 
  103.                 show(arrList[i].url); 
  104.             } 
  105.         } 
  106.          
  107.         private function Init():void 
  108.         { 
  109.             for(var i:int = 0; i<30 ;i ++) 
  110.             { 
  111.                 var obj:Object ={}; 
  112.                 obj.url = Math.floor(Math.random() * 3); 
  113.                 obj.dispose = dispose; 
  114.                 arrList.push(obj); 
  115.                 show(obj.url); 
  116.             } 
  117.         } 
  118.          
  119.         private function dispose():void 
  120.         { 
  121.             show("\n"); 
  122.             show("戒不掉你的温柔,每一次都想牵你的手!") 
  123.         } 
  124.     } 
  125. } 
  126.  
  127. import flash.display.Sprite; 
  128. import flash.text.TextField; 
  129. import flash.text.TextFieldAutoSize; 
  130.  
  131. class Button extends Sprite 
  132. { 
  133.     private var _sp:Sprite; 
  134.     private var _txt:TextField; 
  135.      
  136.     public function Button():void 
  137.     { 
  138.         _sp = new Sprite(); 
  139.         addChild(_sp); 
  140.         draw(); 
  141.     } 
  142.      
  143.     private function draw():void 
  144.     { 
  145.         _sp.graphics.clear(); 
  146.         _sp.graphics.beginFill(0x00ffff,1); 
  147.         _sp.graphics.drawRoundRect(0,0,72,22,5,5); 
  148.         _sp.graphics.endFill(); 
  149.     } 
  150.      
  151.     public function set label(str:String):void 
  152.     { 
  153.         if(!_txt) 
  154.         { 
  155.             _txt = new TextField(); 
  156.             _txt.autoSize = TextFieldAutoSize.LEFT; 
  157.             _txt.textColor =0x0000ff; 
  158.             _txt.mouseEnabled = false; 
  159.         } 
  160.         _txt.text = str; 
  161.         _sp.removeChildren(); 
  162.         _txt.x = _sp.width * 0.5 - _txt.textWidth * 0.5 ; 
  163.         _txt.y = _sp.height * 0.5 - _txt.textHeight * 0.5 -2; 
  164.         _sp.addChild(_txt); 
  165.     } 
  166. } 

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器