PDA

View Full Version : removeChild



RebuiltJorge
May 26th, 2008, 08:33 AM
Wow, this question is so basic, I almost don't want to ask it. I have an object I place in a container, when the container hits my ship, I want to remove the object, however I get the message
display object must be the child of the caller, please see code:




//DISPLAY BLACK HOLE ICON
ActionScript Code:

public function displayBlackHoleIcon(evt:TimerEvent){
myBlackHoleIconContainer = new Sprite()
myBlackHoleIconContainer.x = randomX();

for(var i:int = 0; i < 200; i++){
myBlackHoleIcon = new BlackHoleIcon();
myBlackHoleIcon.x = Math.random()* 30 + 20;
myBlackHoleIcon.y = Math.random()* 30 + 20;

myBlackHoleIconContainer.addChild(myBlackHoleIcon) ;
}

blackHoleIconArray.push(myBlackHoleIconContainer);
addChild(myBlackHoleIconContainer);


}


//BLACK HOLE ICON VS FALCON
for (var blackHoleNum:int=blackHoleIconArray.length - 1; blackHoleNum >= 0; blackHoleNum--) {
if (blackHoleIconArray.hitTestObject(theFalcon)) {

/*playSound(shipHit);*/
removeChild(myBlackHoleIconContainer);
addDisplayBlackHoleIcon(displayBlackHoleIconCount, 1);
isBlackHolePowerActive = [B]true;



}
}

inquizard
May 26th, 2008, 09:28 AM
am i reading this right - you want to remove the object, not the container? if so is your removeChild right? it's referencing the container and not the object?

RebuiltJorge
May 26th, 2008, 09:34 AM
I want to remove the container which will in return remove what ever is inside it

amarghosh
May 27th, 2008, 03:49 AM
removeChild(myBlackHoleIconContainer);
u are calling it from a loop. on the first iteration of loop, it will remove it. on the second iteration it will try to remove and find that it is not a valid child (since it was removed earlier) and flash tells u that by throwing this error.
check before removing it:

if(contains(myBlackHoleIconContainer))
removeChild(myBlackHoleIconContainer);

RebuiltJorge
May 27th, 2008, 10:48 AM
amazing, thanks!