2013-01-07 52 views
0

我有問題的孩子,這裏是我使用AS3 removeChild之提供的DisplayObject必須是調用錯誤

public var monsterArray:Array; 
    public var score:Number; 

    public function MonsterManager() 
    { 
     score = 0; 
     monsterArray = new Array(); 
    } 

public function spawnMonster(waveNumber:Number):void 
    { 
     //This code needs to choose the ID not the monster itself 
     var id:Number; 
     var meleeRanged = Math.floor(Math.random()*2) 
     trace("meleeRanged :"+meleeRanged); 
     id = Math.floor(Math.random()*3)+1; 
     switch(id) 
     { 
      case 1: var newMonster1:Monster1 = new Monster1(); 
         newMonster1.monsterSetup(waveNumber, id); 
         monsterArray.push(newMonster1); 
         addChild(newMonster1); 
       break; 
     } 

    } 

public function update():void 
    { 
     //trace("Go To Update"); 
     var i:number; 
     i = 0; 
     for each (var monster:Monster in monsterArray) 
     //monster needs a bool variable to say if it is active, if it is dont reuse, 
     //if it is not activate, we can use setup and re use it, 
     { 
      monster.update(); 
      if(monster.hp <= 0) 
      { 
       score += 10; 
       removeChild(monster); 
       array.splice(i,1); 
       //monster.x = -1000; 
       //monster.hp = 1; 
       i++; 
      } 
     } 
    } 

我已經刪除添加文本函數的代碼,因爲那並不是」牛逼導致問題:),但是當我打電話APON的removeText功能,我在閃光得到這個錯誤

The supplied DisplayObject must be a child of the caller. 

數組是公開的,所以我不知道爲什麼它不能被刪除,會有人請點這個。

乾杯。

新的代碼下面

public function update(heroGra:HeroDisplay):void 
    { 
     //trace("Go To Update"); 
     var count:Number = monsterArray.length; 
     var monster:Monster; 

     for(var i:int = 0; i<count; i++) 
     { 
      monster = monsterArray[i]; 
      monster.update(heroGra); 


      if(monster.hp <= 0) 
      { 
       score += 10; 
       removeChild(monster); 
       monsterArray.splice(i,1); 
       i--; 
      } 
     } 
    } 

畫布。

如果monsterArray中有超過2個怪物發生錯誤,有關我如何才能阻止它的任何想法?

+0

'removeChild()'用於從舞臺上刪除某些東西(即:可以在屏幕上看到的東西)。你不能刪除以前從未添加過的東西(使用'addChild()'添加)。你打算從陣列或舞臺上移除一些東西嗎?你可能想要顯示更多的代碼:) –

回答

0

不知道你的每個功能是否做得很好。嘗試這樣做:

public function update():void 
{ 
    var count:Number = monsterArray.length; 
    var monster:Monster; 

    for(var i:int = 0; i<count; i++) 
    { 
     monster = monsterArray[i]; 
     monster.update(); 

     if(monster.hp <= 0) 
     { 
      score += 10; 
      removeChild(monster); 
      array.splice(i,1); 
      count--; 
      i--; 
     } 
    } 
} 
+0

是的,有一種方法稱爲addText,這增加了一些文本到數組中,我更新了我的問題 – Canvas

+0

我很抱歉,我修改了上面的內容:D – Canvas

+0

你不能addChild字符串到DisplayList,因爲它不是DisplayObject。創建TextField,而不是... – 3vilguy

相關問題