How to use removeChild

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:


		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);
			
		}



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


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

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:

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);
			
		}

try this

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

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);
	}