在Flash AS3中删除child

在Flash AS3中删除child,flash,actionscript-3,Flash,Actionscript 3,我想在单击tuebutton时清除加载在stage上的xml对象 monbutton.addEventListener(MouseEvent.CLICK, monHandler); function monHandler(evt:MouseEvent):void { trace(evt.type); // Set the "y" location on stage where the first box will live var yPlacement:int = 11

我想在单击tuebutton时清除加载在stage上的xml对象

monbutton.addEventListener(MouseEvent.CLICK, monHandler);

function monHandler(evt:MouseEvent):void
{
    trace(evt.type);
    // Set the "y" location on stage where the first box will live
    var yPlacement:int = 110;
    // Set the "x" location on stage where all boxes will line up vertically
    var xPlacement:int = 30;
    // Set the distance each box should be apart here
    var distance:int = 25;

    // Initialize the XML, place the xml file name, initialize the URLRequest
    // put URLRequest into a new URLLoader, and add event listener on 
    // myLoader listening for when the XML loading is complete
    var myXML:XML = new XML();
    var XML_URL:String = "menu.xml";
    var myXMLURL:URLRequest = new URLRequest(XML_URL);
    var myLoader:URLLoader = new URLLoader(myXMLURL);
    myLoader.addEventListener("complete", xmlLoaded);


    // Create the xmlLoaded function
    function xmlLoaded(event:Event):void {

        // Place the xml data into the myXML object
        myXML = XML(myLoader.data);
        // Initialize and give var name to the new external XMLDocument
        var xmlDoc:XMLDocument = new XMLDocument();
        // Ignore spacing around nodes
        xmlDoc.ignoreWhite = true;
        // Define a new name for the loaded XML that is the data in myLoader
        var menuXML:XML = XML(myLoader.data);
        // Parse the XML data into a readable format
        xmlDoc.parseXML(menuXML.toXMLString());

        // Run the "for each" loop to iterate through all of the menu items listed in the external XML file
        for each (var maindish:XML in myXML.monday.maindish) {

            // Access the value of the "itemColor" node in our external XML file
            var dishName:String = maindish.dname.toString();
            // Access the value of the "itemLabel" node in our external XML file
            var dishSmall:String = maindish.small.toString();
            // Access the value of the "itemPhone" node in our external XML file
            var dishLarge:String = maindish.large.toString();

            // This all pertains to the style of the Box that holds each person
            var rect:Shape = new Shape;
            var color:Number = Number(0xFFFFFF);
            rect.graphics.beginFill(color);
            //rect.graphics.lineStyle(1, 0x999999);
            // Draw the Box 
            rect.graphics.drawRect(0, 0, 150, 50);

            // This all pertains to the text fields that give our boxes their labels, alter values to your liking
            var myText1:TextField = new TextField();
            myText1.text = dishName;
            myText1.autoSize = TextFieldAutoSize.LEFT;
            myText1.x = 2;
            myText1.y = 0;

            var myText2:TextField = new TextField();
            myText2.text = dishSmall;
            myText2.autoSize = TextFieldAutoSize.LEFT;
            myText2.x = 250;
            myText2.y = 0;

            var myText3:TextField = new TextField();
            myText3.text = dishLarge;
            myText3.autoSize = TextFieldAutoSize.LEFT;
            myText3.x = 300;
            myText3.y = 0;

            // Create MovieClip holder for each box graphic and text labels to organize everything into container
            var clip_mc = new MovieClip();
            // Add the rectangle graphic
            clip_mc.addChild(rect);
            // Add the text fields
            clip_mc.addChild(myText1);
            clip_mc.addChild(myText2);
            clip_mc.addChild(myText3);
            // Put the new movieClip on stage now
            addChild(clip_mc);       

            // Now apply it in its offset Y position to the stage
            clip_mc.y = yPlacement; 
            // X position that it will be placed on stage
            clip_mc.x = xPlacement;       
            // Offset each one in the loop to make sure they don't just get put right on top of each other
            yPlacement = yPlacement + distance;

        }

    }
    this.days.gotoAndStop("Monday");
}

tuebutton.addEventListener(MouseEvent.CLICK, tueHandler);

function tueHandler(evt:MouseEvent):void
{
    trace(evt.type);
    removeChild(clip_mc);
    this.days.gotoAndStop("Tuesday");
}

在星期一按钮处理程序中,在声明剪辑后立即为其命名,如下所示:

var clip_mc = new MovieClip();
clip_mc.name = "monday_mc"
然后在星期二按钮处理程序中,在gotoAndPlay之前执行以下操作:

removeChild(this.getChildByName('monday_mc'));
另一方面,只使用一个函数通过传递给该函数的日期变量解析XML数据要比在按钮处理程序代码中进行所有XML解析容易得多