2013-02-04 54 views
0

真的需要一些幫助理解什麼後當舞臺不是DisplayContainer的子項時,TextField會出現在舞臺上嗎?

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

你就在你的問題,它實際上是添加指出。您在循環創建的每個tempText對象添加到已加入其在構造函​​數中上演textContainer

textContainer.addChild(tempText); 

textFields[i].text = thisWord.charAt(i);剛剛更新的TextField的陣列內的特定索引處的文本。沒有理由在舞臺上文本不應在這裏更新。

+0

感謝您的快速回復,我怕林仍然沒有得到它,但:/。如果什麼顯示在舞臺上了是在textContainer添加「tempText」對象......爲什麼不改變「文本框」的指數變化tempText的價值? – aph107

+1

沒有代碼, 「重新分配」 文本字段[I]的.text = thisWord.charAt(ⅰ);到「tempText」 – aph107

+0

我有一個巨大的「OH」時刻。我在想textFields.push(tempText);正在推動「值」而不是實際的對象到數組中,並且textFields [i] .text = thisWord.charAt(i);改變了價值,但沒有把它添加到舞臺上。當它真的在改變已經在舞臺上的物體時。 我想我只是需要有人以另一種方式向我重複。以爲我瘋了。謝謝。 – aph107