【问题标题】:TextField appears on stage when it's not a child of DisplayContainer?当 TextField 不是 DisplayContainer 的子级时出现在舞台上?
【发布时间】:2013-02-05 03:19:09
【问题描述】:

真的需要一些帮助来了解之后发生的事情

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();

我很确定有一条规则我遗漏或不理解....

谢谢。

【问题讨论】:

    标签: actionscript-3 textfield children stage addchild


    【解决方案1】:

    您在问题中指出它实际上 正在 被添加。您在该循环中创建的每个 tempText 对象都将添加到 textContainer 中,而 textContainer 已添加到构造函数中的阶段。

    textContainer.addChild(tempText);
    

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

    【讨论】:

    • 感谢您的快速回复,但恐怕我仍然没有得到它:/。如果舞台上出现的是在 textContainer 中添加的“tempText”对象......为什么更改“textFields”的索引会更改 tempText 的值?
    • 没有代码可以“重新分配” textFields[i].text = thisWord.charAt(i);到“临时文本”
    • 我有一个巨大的“哦”时刻。我在想 textFields.push(tempText);将“值”而不是实际对象推入数组,并且 textFields[i].text = thisWord.charAt(i);更改了值,但没有将其添加到舞台。当它真的改变已经在舞台上的对象时。我想我只是需要有人以另一种方式重复给我听。以为我快疯了。谢谢。
    猜你喜欢
    • 2012-11-22
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-09
    • 1970-01-01
    相关资源
    最近更新 更多