当TextField不是DisplayContainer的子项时,它会出现在舞台上吗?

时间:2013-02-04 19:04:31

标签: actionscript-3 textfield children stage addchild

真的需要一些帮助,了解

之后发生的事情
textFields[i].text = thisWord.charAt(i);

我理解它更新了该特定索引的值,我无法弄清楚它影响的原因:

textContainer.addChild(tempText);
textFields.push(tempText);

改变的价值出现在舞台上,但我不明白为什么,因为它从未作为textContainer的孩子添加。

var loader:URLLoader;
var allWords:Array;
var thisWord:String;

var textContainer:MovieClip;

var textFields:Array;
var textStyle:TextFormat;
var underline:MovieClip;

function init():void
{
    loader = new URLLoader();
    allWords = new Array();

    textContainer = new MovieClip();
    textFields = new Array();

    textStyle = new TextFormat();
    textStyle.font = "Courier New";
    textStyle.size = 48;
    textStyle.bold = true;

    textContainer.y = stage.stageHeight / 2 - 50;
    addChild(textContainer);

    loader.load(new URLRequest("words.txt"));
    loader.addEventListener(Event.COMPLETE, textLoaded);
    guess_btn.addEventListener(MouseEvent.CLICK, guess);

}

function textLoaded(event:Event):void
{
    var tempText:TextField;

    var stringOfWords:String = event.target.data;
    allWords = stringOfWords.split(",");
    thisWord = allWords[Math.floor(Math.random() * allWords.length)];
    trace(thisWord);

    for (var i:uint = 0; i < thisWord.length; i++)
    {
        tempText = new TextField();
        tempText.defaultTextFormat = textStyle;
        tempText.name = "textField" + i;
        tempText.text = "1";//for restart

        tempText.width = 48;
        tempText.x = i * tempText.width;
        tempText.selectable = false;
        textContainer.addChild(tempText);

        textFields.push(tempText);

        if (thisWord.charAt(i) != " ")
        {
            underline = new Underline();
            underline.x = tempText.x + tempText.width / 3;
            underline.y = tempText.y + tempText.height / 2 + 5;
            //textContainer.addChild(underline);
        }
    }

    textContainer.x = stage.stageWidth / 2 - textContainer.width / 2;



}

function guess(event:MouseEvent):void
{
    if (guess_txt.text != "")
    {
        if (thisWord.indexOf(guess_txt.text) != -1)
        {
            for (var i:uint = 0; i < textFields.length; i++)
            {
                if (thisWord.charAt(i) == guess_txt.text)
                {
                    textFields[i].text = thisWord.charAt(i);
                    trace(textFields[i].text);
                }
            }

        }
        else if (guesses_txt.text == "")
        {
            guesses_txt.appendText(guess_txt.text);
        }
        else
        {
            guesses_txt.appendText("," + guess_txt.text);
        }
    }

    guess_txt.text = "";
    findch();
}

init();

我非常确定这是我缺失或不理解的规则....

感谢。

1 个答案:

答案 0 :(得分:1)

您在问题中指出实际 正在添加。您在该循环中创建的每个tempText对象都会添加到已添加到构造函数中的舞台的textContainer

textContainer.addChild(tempText);

textFields[i].text = thisWord.charAt(i);只是在数组中的特定索引处更新TextField的文本。 舞台上的文字没有理由不在这里进行更新。