# How to use removeChild

**URL:** https://forum.kirupa.com/t/how-to-use-removechild/260106
**Category:** flash
**Created:** [May 12, 2008, 6:19pm UTC](https://forum.kirupa.com/t/how-to-use-removechild/260106 "2008-05-12T18:19:09Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![blackpool](https://avatars.discourse-cdn.com/v4/letter/b/46a35a/32.png) [@blackpool](https://forum.kirupa.com/u/blackpool)
#### Post date: [May 12, 2008, 6:19pm UTC](https://forum.kirupa.com/t/how-to-use-removechild/260106/1 "2008-05-12T18:19:09Z")

</div>

I’m loading in pictures in my timeline (1 picture per frame). I want to put in the removeChild command in the function, but the problem is, it creates an error on the first frame because there is no child to remove, what kind of condition would be best to handle this?

Also, when I click on the link twice for the same picture, it disappears, any reason for this?

Here is the code I’m using to load the picture:

```auto

		function loadPic(fileName):void
		{
		var mainRequest:URLRequest = new URLRequest (fileName);
		
		mainLoader.load(mainRequest);
		mainLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, mainLoaded);
		}
		
		function mainLoaded(event:Event):void 
		{
			mainPicArea.x = mainPicPlacementX;
			mainPicArea.y = mainPicPlacementY;
			mainPicArea.addChild(mainLoader);
			fadeIn = new Tween(mainPicArea, "alpha", Strong.easeOut, 0,1,.5, true);
			
		}

```

---

<div class="post-metadata">

### Author: ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)
#### Post date: [May 12, 2008, 6:57pm UTC](https://forum.kirupa.com/t/how-to-use-removechild/260106/2 "2008-05-12T18:57:33Z")

</div>

Check to see if it’s null or not before trying to remove it:

```auto

if (childClip != null && parentClip !=null)
{
      parentClip.removeChild(childClip);
}

```

---

<div class="post-metadata">

### Author: ![blackpool](https://avatars.discourse-cdn.com/v4/letter/b/46a35a/32.png) [@blackpool](https://forum.kirupa.com/u/blackpool)
#### Post date: [May 12, 2008, 7:38pm UTC](https://forum.kirupa.com/t/how-to-use-removechild/260106/3 "2008-05-12T19:38:53Z")

</div>

Thanks - I tried that, but now I’m getting the following errors:

> ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.  
> at flash.display::DisplayObjectContainer/removeChild()  
> at MercuryRedo\_fla::MainTimeline/loadPic()  
> at MercuryRedo\_fla::MainTimeline/frame1()

This is my code:

```auto
var mainPicArea:MovieClip = new MovieClip();
addChild(mainPicArea);

		function loadPic(fileName):void
		{
			
			if (mainLoader != null && mainPicArea !=null)
				{
    				mainPicArea.removeChild(mainLoader);
				}

		var mainRequest:URLRequest = new URLRequest (fileName);
		
		mainLoader.load(mainRequest);
		mainLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, mainLoaded);
		}
		
		function mainLoaded(event:Event):void 
		{
			mainPicArea.x = mainPicPlacementX;
			mainPicArea.y = mainPicPlacementY;
			mainPicArea.addChild(mainLoader);
			fadeIn = new Tween(mainPicArea, "alpha", Strong.easeOut, 0,1,.5, true);
			
		}

```

---

<div class="post-metadata">

### Author: ![zworp](https://avatars.discourse-cdn.com/v4/letter/z/f9ae1b/32.png) [@zworp](https://forum.kirupa.com/u/zworp)
#### Post date: [May 13, 2008, 12:35pm UTC](https://forum.kirupa.com/t/how-to-use-removechild/260106/4 "2008-05-13T12:35:01Z")

</div>

try this

if(mainLoader != null)  
if(mainLoader.parent != null)  
mainLoader.parent.removeChild(mainLoader);

---

<div class="post-metadata">

### Author: ![blackpool](https://avatars.discourse-cdn.com/v4/letter/b/46a35a/32.png) [@blackpool](https://forum.kirupa.com/u/blackpool)
#### Post date: [May 13, 2008, 10:22pm UTC](https://forum.kirupa.com/t/how-to-use-removechild/260106/5 "2008-05-13T22:22:27Z")

</div>

That still didn’t work - so dropped the movieclip, and I’m tinkering with just adding the pictures straight to the stage - your code then worked. I’m just tinkering around here - and i made a button to clear the loaded picture using removeChild. When I triggered the function which loads the images, I then get this:

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.  
at flash.display::DisplayObjectContainer/removeChild()  
at Untitled\_fla::MainTimeline/loadPicture()  
at Untitled\_fla::MainTimeline/frame2()

I don’t understand why removeChild works when it’s in the function which loads the image, but if you just use the command by itself, will prevent me from continuing to load images?

My code for loading:

function loadPicture(fileName):void  
{  
if (theLoad != null)  
{  
removeChild(theLoad);  
}

theLoad = new Loader();  
theLoad.load(new URLRequest(fileName));  
theLoad.x = 25;  
theLoad.y = 25;  
theLoad.contentLoaderInfo.addEventListener(Event.COMPLETE, mainLoaded);  
}

```
	function mainLoaded(event:Event):void 
	{
		theLoad.x = 25;
		theLoad.y = 25;
		addChild(theLoad);
		fadeIn = new Tween(theLoad, "alpha", Regular.easeOut, 0,1,.5, true);
		
		trace(theLoad);
	}
```
