Creating and clearing rectangles in Flash CS5
I have an XML document that I am reading in and drawing a bunch of rectangles from the data that I get. This bit of code is inserted in one frame. Buttons are used to navigate through the frames. A button on the main screen takes you to the frame that draws the rectangles. I have a Back button on the frame that draws the rectangles that takes you back to the main screen and that works fine but when I click Back, the rectangles that I drew still remain on the scene. Is there anyway that I can erase all of the re开发者_开发问答ctangles at once?
Thanks
Define an array of type DisplayObject and add each object (Sprite, Movieclip) to it.
When you want to remove them, iterate through the array and remove the child:
// Adding object to the array
var objArray:Array; // this could be a field member
objArray = [];
// ... navigate to your XML file and get each entry
// ... after you create an object, add it
if( objArray.indexOf(obj1) < 0 ) // Make sure it is not there so we do not add it twice
objArray.push(obj1);
// Clearing...
for each (var obj1:DisplayObject in objArray)
{
obj1.parent.removeChild(obj1);
}
// when done, clear the array
objArray = [];
if you want to remove everything in the current frame:
function disposeFrameContent() : void
{
while(numChildren)
removeChildAt(0);
}
// run this code before you change frame
disposeFrameContent();
That would depend on how you draw the rectangles, if you use the graphics API, you could simply do:
this.graphics.clear();
if you add DisplayObjects, do:
while( this.numChildren > 0 )
this.removeChildAt( 0 );
You can of course call these methods on any container MovieClip :
//for instance...
mc.graphics.clear();
//or
while( mc.numChildren > 0 )
mc.removeChildAt( 0 );
精彩评论