2012-05-28 38 views
0

我遇到問題,每當我嘗試從數組中刪除元素時,都會出現此錯誤#2025。當我點擊它時,該對象在舞臺上不再可見,但總是出現此錯誤。ArgumentError:錯誤#2025:提供的DisplayObject必須是調用者的孩子

//remove items inventory 
    public function RemoveObject(mc:MovieClip) 
    { 
     var checkRemove:Number = curSlot - 1; 
     trace("current slot " + (curSlot-1)); 
     trace("current pos array" + myIndex); 

     for(var i:int = 0; i < itemS.length;i++) { 
      this.removeChild(itemS[checkRemove]); 
     } 


    } 
+0

要刪除一個項目嗎? – mgraph

回答

0

不使用for只是:

this.removeChild(itemS[checkRemove]); 
+0

它似乎從舞臺上刪除對象, –

+0

@TomJacobs是你想要的嗎? – mgraph

+0

是的,它使用此代碼從屏幕中刪除對象,但是當我添加拼接功能時,對象不會再從屏幕上移除 –

0

如果你想刪除一個以上的項目,使用每個循環,因爲當過你刪除一個子容器將重新索引它包含了其他孩子的。

for each (var child:DisplayObject in this.getChildren()) { 
    this.removeChild(child); 
} 

,如果你想刪除只有一個項目則是可以做到

this.removeChild(this.getChildAt(childIndex)); 

以下的Container方法也有助於獲得渴望孩子

getChildByName(name:String):DisplayObject 
getChildIndex(child:DisplayObject):int 

和其更好首先檢查你要刪除的孩子是否在容器中

var child:DispalyObject = this.getChildAt(childIndex); 

if(this.contains(child)) 
{ 
    this.removeChild(child); 
} 

希望有幫助

+0

之後添加它現在但 它似乎現在從舞臺上刪除對象,雖然它不會從陣列中刪除元素。當我使用拼接從陣列中刪除元素時,它將刪除與removeChild相同的項目。 這是我的功能 itemS.splice(checkRemove,1); –

+0

親愛的我不清楚你在說什麼,請參閱http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html,我建議你使用ArrayCollection,它是一個包裝類的包裝類會給你像removeItemAt(索引)方法從Array中刪除項目 – Imran

相關問題