# addChild and removeChild issue

**URL:** https://forum.kirupa.com/t/addchild-and-removechild-issue/287299
**Category:** flash
**Created:** [April 29, 2009, 9:29am UTC](https://forum.kirupa.com/t/addchild-and-removechild-issue/287299 "2009-04-29T09:29:34Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![100hertz](https://avatars.discourse-cdn.com/v4/letter/1/db5fbb/32.png) [@100hertz](https://forum.kirupa.com/u/100hertz)
#### Post date: [April 29, 2009, 9:29am UTC](https://forum.kirupa.com/t/addchild-and-removechild-issue/287299/1 "2009-04-29T09:29:34Z")

</div>

Ok, here’s the deal. I want to generate a maximum of 5 childs in a clip called ‘placeholder’ using a button (placeCloud\_1). the childs will ‘float’ to the top of the stage. when the y=0 the child must be removed (using the function checkPos() ) so i can add another child without exceding the number of 5 childs. Everything works, exept for the removeChild! i’ve tried removeChild(this) , this.removeChild(this), parent.this.removeChild(this), parent.this.removeChild(mc), placeholder.removeChild(this) etc… and nothing works.  
When using (this) it throws  
TypeError: Error #1034: Type Coercion failed: cannot convert global@2f6710d1 to flash.display.DisplayObject.  
at MethodInfo-251()

when using (mc) it throws  
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.  
at flash.display: DisplayObjectContainer/removeChild()  
at MethodInfo-251()

It’s driving me nuts!  
Can someone help me?

this is my code:

* * *

import fl.transitions.Tween;  
import fl.transitions.easing.\*;  
import fl.transitions.TweenEvent;  
var i:int = 0

placeCloud\_1.addEventListener(MouseEvent.CLICK, makeCloud\_1);

function makeCloud\_1(event:MouseEvent):void {  
if (i\<5) {  
var mc:cloud\_1 = new cloud\_1();

mc.alpha = Math.random() \* 1; //random alpha -\> 1 = 100%  
mc.scaleX = randRange(30, 130) / 100; //1 = 100%  
mc.scaleY = mc.scaleX;  
mc.x = randRange(1, stage.stageWidth);  
mc.y = Math.random()\* stage.stageHeight;

placeholder.addChild(mc);

var myTweenSpeed = randRange(500, 1000) ;  
var myTween:Tween=new Tween(mc,“y”,Regular.easeOut,300,-300,myTweenSpeed,false);  
myTween.start();  
i++;  
//trace (i);

mc.addEventListener(Event.ENTER\_FRAME, checkPos);  
function checkPos (e:Event):void{  
if (mc.y\<0) {  
//trace (“done”);  
removeChild(this);  
i–;  
}  
}

}  
}

function randRange(min:Number, max:Number):Number {  
var randomNum:Number = Math.floor(Math.random() \* (max - min + 1)) + min;  
return randomNum;  
}
