# removeChild problem

**URL:** <https://forum.kirupa.com/t/removechild-problem/260712>\
**Category:** Uncategorized\
**Created:** [May 19, 2008, 7:58am UTC](https://forum.kirupa.com/t/removechild-problem/260712 "2008-05-19T07:58:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![slumgutt](https://avatars.discourse-cdn.com/v4/letter/s/4da419/32.png) [@slumgutt](https://forum.kirupa.com/u/slumgutt)\
**Post date:** [May 19, 2008, 7:58am UTC](https://forum.kirupa.com/t/removechild-problem/260712/1 "2008-05-19T07:58:48Z")

</div>

Hi!

I’m making a game, and I’m having some trouble with the score function. When one movieclip touches an item I want the score to go up, and the item to disappear. My code seems to work, as the score increases when the item is touched, but I get this error:

2025: The supplied DisplayObject must be a child of the caller.

This error outputs every time my hero touches the sport where the item used to be. Probably, this is because the item is not entirely removed, just invisible. But how do I avoid this error?

My event is enter\_frame and the function is:

[AS]private function fiendeKollisjon(e:Event):void {  
if (mc\_fiende\_1.hitTestObject(mc\_humle)) {  
removeChild(mc\_fiende\_1);  
theScore += 50;  
score.text = String(theScore);  
} else {  
score.text = String(theScore);  
}

}[/AS]

mc\_fiende\_1 = the item my hero tries to eat  
mc\_humle = my hero  
theScore = a variable originally set to ‘0’  
score = dynamic textfield

any suggestions?

---

<div class="post-metadata">

**Author:** ![sasxa](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sasxa](https://forum.kirupa.com/u/sasxa)\
**Post date:** [May 19, 2008, 10:44am UTC](https://forum.kirupa.com/t/removechild-problem/260712/2 "2008-05-19T10:44:50Z")

</div>

Hi,

I had some similar problems, can’t tell you how I’ve solved them exactly (I was mostly changing order of things, so not sure what helped (: ). But you might wanna try to play with your MovieClips to see if you can get what you want. For example:

```auto

mc_fiende_1.hitTestObject(mc_humle) --> mc_humle.hitTestObject(mc_fiende_1)...
removeChild(mc_fiende_1) --> e.target.removeChild(...) 
//or
e.currentTarget.removeChild(...) 
//or 
this.removeChild(...) 
//maybe even 
mc_fiende_1.parent.removeChild(mc_fielde_1)

```

Once I had to do a check to see if parent wasn’t null:

```auto

if (object.parent != null) {
  removeChild(object);
}

```

not sure why (I didn’t have time to check class structure (: )… I believe one of these examples should help…

Sasxa

---

<div class="post-metadata">

**Author:** ![Iamthejuggler](https://avatars.discourse-cdn.com/v4/letter/i/ecc23a/32.png) [@Iamthejuggler](https://forum.kirupa.com/u/Iamthejuggler)\
**Post date:** [May 19, 2008, 11:05am UTC](https://forum.kirupa.com/t/removechild-problem/260712/3 "2008-05-19T11:05:42Z")

</div>

Indeed. The problem is that the object you are trying to remove is not a child of the object/class that your script is placed. e.target will reference the object that sent the event, which may or may not be the parent of the object you are trying to remove depending on your structure. If it isn’t you may need to dispatch an event from the hitTest function and add a listener for it on the parent of the object you want to remove.
