/* Open a new FLash file (ActionScript 3.0). Select TimeLine --> Top Layer --> Frame 1 Select Window --> Actions (F9) Copy and paste this snippet */ import flash.display.*; import DisplayObjectUtilities; addChild ( new Sprite() ); addChild ( new Sprite() ); trace (numChildren); DisplayObjectUtilities.removeAllChildren (this); trace (numChildren); /* ******************************************************** */ /* Open a new .as file (ActionScript 3.0). Copy and paste this snippet. Create a new folder on your desktop. Save both the .fla and .as in the folder. Save the .as file as DisplayObjectUtilities.as Observe the output */ // Package below /* Let's create another class. Lets call this one Example. Here we create our own methods to be used as needed. Notice we still have to follow all the "rules" about case, etc Note: in this example we use a "getter" and a "setter" to manipulate the private variable. This is the prefered way because we can enforce rules that we may have. */ package { import flash.display.*; public class DisplayObjectUtilities { // Remove all of the children in a container public static function removeAllChildren( container:DisplayObjectContainer ):void { // Because the numChildren value changes after every time we remove // a child, save the original value so we can count correctly var count:int = container.numChildren; // Loop over the children in the container and remove them for ( var i:int = 0; i < count; i++ ) { container.removeChildAt( 0 ); } } } } // Broken?? Make sure both .fla and .as live in the same folder. // ActionScript 3.0 Cookbook, by Joey Lott, et al; O'Reilly