2011-12-09 95 views
0

簡單的問題,但我不能得到它的工作。 所以我只是有一個顯示對象,一個球的類。我從Main.as 創建它的實例並運行一個for循環來檢查我是否擊球,如果我這樣做,我想刪除該對象。 但我不能。Actionscript 3.0 - 試圖刪除一個孩子

我的代碼有什麼問題?


package 
{ 
    import flash.display.Sprite; 
    import flash.events.Event; 
    import flash.events.MouseEvent; 
    import flash.ui.Mouse; 
    import flash.events.TimerEvent; 
    import flash.utils.Timer; 


    public class Main extends Sprite 
    { 
     private var clay:Clay; 
     private var clayCollection:Vector.<Clay> = new Vector.<Clay>; 
     private var crash:Crash; 
     private var crashCollection:Vector.<Crash> = new Vector.<Crash>; 
     private var timer:Timer = new Timer(0); 
     private var newClayTimer:Timer = new Timer(1000); 

     public function Main() 
     { 
      newClayTimer.start(); 
      newClayTimer.addEventListener(TimerEvent.TIMER, addNewClay); 

      stage.addEventListener(MouseEvent.CLICK, checkForHit); 
     } 

     private function checkForHit(e:MouseEvent):void 
     { 
      if (clayCollection.length > 0) 
      { 
       for (var i:int = 0; i < clayCollection.length; i++) 
       { 
       if (e.target.hitTestObject(clayCollection[i])) 
       { 
        clayCollection.splice(i, 1); 
        removeChild(clayCollection[i]); 
       } 
       } 
      } 
     } 

     private function addNewClay(e:TimerEvent):void 
     { 
      clay = new Clay(); 
      addChild(clay); 
      clayCollection.push(clay); 
     } 
    } 
} 
+0

如果您在命中測試代碼塊中放置了一個斷點,它會觸發嗎?如果是這樣,如果您在removeChild代碼行之後移動代碼拼接線(以便您要刪除的對象仍然位於集合中的索引處),會發生什麼情況?另外,在每個「粘土」對象上添加點擊監聽器會不會更簡單?然後你會知道哪一個被點擊了,因爲它會成爲事件的當前目標。 –

+0

嗨,我試圖改變位置與拼接和removechild,但它給了我相同的對象引用錯誤。我不知道是否在每個對象上放置一個偵聽器只會增加代碼和CPU的能力。 – MindGem

+0

如果你只是在你關心的對象而不是任何對象的情況下觸發一個監聽器,那麼CPU使用率會更低。它不會添加任何代碼......只需將addEventListener代碼移動到addNewClay中,但它只會是一行(指的是新粘土而不是舞臺)。 –

回答

1

嘗試是這樣的

for (var i:int = 0; i < clayCollection.length; i++) { 
    var clay:Clay = clayCollection[i]; 
    if (e.target.hitTextObject) { 
     //seems to me all clays will hit test as true with the stage? 
     removeChild(clay); 
     clayCollection.splice(i, 1); 
    } 
} 

你知道你正在嘗試刪除的對象肯定是擊中測試真一的方式。

0

我同意艾米,聽點擊粘土的實例,而不是舞臺。你仍然可以從陣列中刪除它,像這樣(未測試的代碼):

package 
{ 
import flash.display.Sprite; 
import flash.events.Event; 
import flash.events.MouseEvent; 
import flash.ui.Mouse; 
import flash.events.TimerEvent; 
import flash.utils.Timer; 


public class Main extends Sprite 
{ 
    private var clay:Clay; 
    private var clayCollection:Vector.<Clay> = new Vector.<Clay>; 
    private var crash:Crash; 
    private var crashCollection:Vector.<Crash> = new Vector.<Crash>; 
    private var timer:Timer = new Timer(0); 
    private var newClayTimer:Timer = new Timer(1000); 

    public function Main() 
    { 
     newClayTimer.start(); 
     newClayTimer.addEventListener(TimerEvent.TIMER, addNewClay); 


    } 

    private function checkForHit(e:MouseEvent):void 
    { 
     // identify the target as Clay 
     var clay:Clay = e.target as Clay; 
     if(contains(clay)) removeChild(clay); 

     // remove it from your array 
     for each (var c:Clay in clayCollection) { 
      if (c == clay) clayCollection.splice(clayCollection.indexOf(c), 1); 
     } 


    } 

    private function addNewClay(e:TimerEvent):void 
    { 
     clay = new Clay(); 
     clay.addEventListener(MouseEvent.CLICK, checkForHit); 
     addChild(clay); 
     clayCollection.push(clay); 
    } 
    } 
} 
0

當程序創建的對象的實例,你的鼠標點擊它,調試器拋出此錯誤。

主線(暫停:的RangeError:錯誤#1125:索引0超出範圍0)
主/ checkForHit

解決方法是,你需要限定爲你切循環的索引它。此外,應用拼接和分隔的順序也很重要。

package 
{ 
import flash.display.Sprite; 
import flash.events.Event; 
import flash.events.MouseEvent; 
import flash.ui.Mouse; 
import flash.events.TimerEvent; 
import flash.utils.Timer; 


public class Main extends Sprite { 
    private var clay:Clay; 
    private var clayCollection:Vector.<Clay> = new Vector.<Clay>; 
    private var timer:Timer = new Timer(0); 
    private var newClayTimer:Timer = new Timer(1000); 

    public function Main(){ 
     newClayTimer.start(); 
     newClayTimer.addEventListener(TimerEvent.TIMER, addNewClay); 
     stage.addEventListener(MouseEvent.CLICK, checkForHit); 

    }//END MAIN() 

    private function checkForHit(e:MouseEvent):void{ 

     if (clayCollection.length > 0) 
     { 
      for (var i:int = 0; i != clayCollection.length; i++) 
      { 
      trace(i,clayCollection.length); 
      if (e.target.hitTestObject(clayCollection[i])) 
      { 
       removeChild(clayCollection[i]); 
       clayCollection.splice(i, 1); 
       i--; 
      } 
      } 
     } 
    }//END checkForHit() 

    private function addNewClay(e:TimerEvent):void{ 
      clay = new Clay(); 
      addChild(clay); 
      clayCollection.push(clay); 
    }//END addNewClay() 

}//END MAINCLASS 

}//END PACKAGE