Flash 如何在另一个函数中删除动态创建的TextField

Flash 如何在另一个函数中删除动态创建的TextField,flash,actionscript-3,Flash,Actionscript 3,我目前正在for循环中创建textfields-尽管在本例中仅创建一个TextField 我的问题是,如何在另一个函数中删除TextField子项? 我基本上做的是,创建一个文本字段,将child添加到一个容器->然后将容器放到另一个位置->然后删除容器中的child和另一个文本。我试过这样的方法: removeChild(getChildByName(myTextField2)) 编辑:也许对我来说,一个解决方案就是将文本字段的文本更改为其他内容,而不是删除它并添加新的文本 public fu

我目前正在for循环中创建textfields-尽管在本例中仅创建一个TextField

我的问题是,如何在另一个函数中删除TextField子项? 我基本上做的是,创建一个文本字段,将child添加到一个容器->然后将容器放到另一个位置->然后删除容器中的child和另一个文本。我试过这样的方法:

removeChild(getChildByName(myTextField2))

编辑:也许对我来说,一个解决方案就是将文本字段的文本更改为其他内容,而不是删除它并添加新的文本

public function handleTextFrames(numberOfFrames:Number, textFrame1:String, textFrame2:String, textFrame3:String, textFrame4:String):void

    {

        var textArray:Array = new Array(textFrame1,textFrame2,textFrame3,textFrame4);

        // Creating font instance
        var garageInstance:Font = new garage();

        //Creating the textfield object and naming it "myTextField"
        var myTextField:TextField = new TextField();

        //Here we add the new textfield instance to the stage with addchild()
        textContainer.alignContainer1.addChild(myTextField);

        //myTextField.width = 930;
        myTextField.embedFonts = true;
        myTextField.multiline = true;
        myTextField.wordWrap = false;
        myTextField.selectable = false;

        var myText:String = textArray[0];

        myTextField.htmlText = myText;

        //This last property for our textfield is to make it autosize with the text, aligning to the left.
        myTextField.autoSize = TextFieldAutoSize.LEFT;

        //This is the section for our text styling, first we create a TextFormat instance naming it myFormat
        var myFormat:TextFormat = new TextFormat();

        //Embedding font
        myFormat.font = garageInstance.fontName;

        myFormat.color = 0xffffff;
        myFormat.size = 60;
        myFormat.align = TextFormatAlign.CENTER;


        //Now the most important thing for the textformat, we need to add it to the myTextField with setTextFormat.
        myTextField.setTextFormat(myFormat);

        myTextField.y = textContainer.alignContainer1.height * 0.5 - myTextField.textHeight * 0.5;
        myTextField.x = payoffContainer_mc.width / 2 - myTextField.textWidth / 2;
    }

首先为textfield指定一个名称:

var myTextField:TextField = new TextField();
myTextField.name = "mytextfield";
然后通过执行以下操作来访问它:

var myTextField:TextField = textContainer.alignContainer1.getChildByName("mytextfield");
删除它:

textContainer.alignContainer1.removeChild(myTextField);
myTextField = null;

你到底想做什么?:)因为如果你把文本字段保存在一个“全局”数组中,这就解决了它

假设您将其保存在一个名为
textfields
的数组中

var textfields:Array = [];
你把一切都推到这里来

textfields.push(textfield);
然后,您可以随时通过调用此函数来删除此项:

textfields[0].parent.removeChild(textfields[0]);
关于第一个要素。在flash中,这始终有效:

INSTANCE.parent.removeChild(INSTANCE);

<>这有帮助吗?< /p> 对于一个,而不是GeHealByByNAME--只考虑使用这个方法:

container["text_field_name"];
另外,我建议您对removeChild()调用更严格一些,以避免出现空对象引用错误:

var tf:TextField = TextField(container["text_field_name"]);

if(tf.parent) tf.parent.removeChild(tf);
要从函数中删除它,您需要访问的只是“容器”,它是TextField的父项,也是TextField本身


希望这能有所帮助。

您只需访问文本字段并更改其值,而无需反复删除和添加它。访问文本字段并更改其值对我来说实际上是一种更简单的方法-因此感谢您的建议!最后一个代码示例“始终有效”,如果实例有父对象,否则将导致运行时错误(由于父对象为空)。问题是如何“删除”文本字段。从这个问题可以明显看出,实例被添加到了stage中,因此它有一个parent.anemgyenge!全局数组帮了我的忙-我很抱歉响应太慢,但是我没有从这里收到任何通知,不知道为什么非常感谢!