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

 

热门文章推荐

请稍候...

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

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