AS3 Tips of the Day – removeChild()

Tip of the Day is DisplayObjectContainer’s removeChild() method:

AS3′s method removeChild() is a method of DisplayObjectContainer and Inheritance by:
-> DisplayObjectContainer -> InteractiveObject -> DisplayObject -> EventDispatcher -> Object.
-> import flash.display

AS3′s removeChild() method is not the same as AS2′s removeMovieClip(). These are the differences of removeChild and removeMovieClip:
1. removeChild will output an error if you use removeChild to a null object. While removeMovieClip will just ignore if you remove a null object.

Well, to avoid this error, here are some tips to use removeChild():
1. If you need to isntantiate a movieclip by removing it to the stage before adding to stage. I always use this method (removing child before adding child, in some instances)

package {
	import flash.display.*;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
 
	public class Main extends MovieClip {
		var arr:Array = new Array()
 
		public function Main():void {
			/// creating Timer instance t
			var t:Timer = new Timer(1000, 0);
 
			/// add event listener to Timer t, and has a callback function onTimer()
			t.addEventListener(TimerEvent.TIMER, onTimer);
 
			/// start the Timer t
			t.start();
		}
 
		/// callback function onTimer for Timer t
		private function onTimer(evt:TimerEvent):void {
			/// if arr.length > 5 all movieclips on stage will be removed
			if (arr.length > 5) {
				/// this is how to removeChild in a stage, by putting a child in a Array
				for (var i:int = 0; i < arr.length; i++) {
					removeChild(arr[i]);
				}
				/// will need to instantiate the Array arr
				arr = new Array();
			}
 
			/// will need to instantiate the Array arr
			var mc:Sprite = new Sprite()
			mc.graphics.beginFill(Math.random() * 0xffffff);
			mc.graphics.drawCircle(Math.random() * 100, Math.random() * 100, Math.random() * 20);
			addChild(mc);
 
			/// push the mc in a Array.
			arr.push(mc);
		}
	}
 
}

2. removeChild by ‘name’

/// addChild mc with name "myMc".
var mc:Sprite = new Sprite();
mc.name = "myMc";
addChild(mc);

If you want to remove the child by name used:

var toBeRemove:MovieClip = getChildByName("myMc");
toBeRemove.parent.removeChild(toBeRemove);

3. Just use try/catch methods if you found any error in removing child:

try {
	removeChild(mc);
} catch (err:Error){}

4 Comments to “AS3 Tips of the Day – removeChild()”

  1. By facebook poker, April 4, 2010 @ 9:28 am

    although I expend most of my time on the internet participating in online games like myspace poker or mafia wars, I nevertheless like to spend some spare time to scan a small amount of websites sometimes and I am very pleased to report this latest piece of writing is frankly reasonably effective and significantly more beneficial than half the other worthless junk I read today , anyways i’m going to play a couple of rounds of facebook poker

  2. By watch twilight eclipse, April 8, 2010 @ 11:42 am

    heya outstanding little web site ya have here :) I am using the same theme on my own website however for whatever weird reason it looks to stream a lot quicker on this site although your own includes a whole lot more content. Are you currently applying any kind of plugins or widgets which quicken it up? If you could give the names so I would be able to use these in my internet sites so twilight breaking dawn followers could watch twilight breaking dawn online trailers and films quicker I’d personally be happy – many thanks in advance :)

  3. By Mr.Luke, November 15, 2011 @ 10:08 am

    Wow. Method 2 worked like a charm for me – I have been having a difficult time trying to remove a specific MovieClip from my app and after a couple of hours of research this article has solved the problem! Thanks!

  4. By danbo, September 13, 2012 @ 6:18 pm

    clever…solution 2.
    Funny, you create a clip, you call its parent to point to it.
    Why not, but efficient. I use now this method.
    Just one Question:
    in “var toBeRemove:MovieClip = getChildByName(“myMc”);”
    which seems to be a stack, does the pointer goes directly to “myMC” or does it looks at every level until it finds “myMC”, only to know if it’s quicker than using a “removeChild(“getChildByName(….)) ?
    Thanks for this…From France

RSS feed for comments on this post. TrackBack URI

Leave a Reply