2013-03-14 67 views
3

我正在通過基於XML文件的循環添加一系列文本字段。字段的寬度始終爲200 px,因此根據XML節點中包含多少文本,文本字段的高度會有所不同。我需要一種方法根據它們的高度將這些字段堆疊在一起,並在每個字符之間加上一個10 px的空格。以下是我如何創建文本字段。堆疊動態文本字段Flash/ActionScript 3

for(var i:int; i < xml.item.length(); i++) 
{ 
    var theText:TextField = new TextField(); 
    addChild(theText); 
    theText.wordWrap = true; 
    theText.width = 200; 
    theText.antiAliasType = AntiAliasType.ADVANCED; 
    theText.autoSize = TextFieldAutoSize.LEFT; 
    theText.selectable = false; 
    theText.htmlText = xml.item[i][email protected]; 
}; 

回答

3

您可以通過使用文本字段的高度來跟蹤高度。

var startHeight:int = 0; 
for(var i:int; i < xml.item.length(); i++) 
{ 
    var theText:TextField = new TextField(); 
    addChild(theText); 

    theText.y = startHeight; 

    theText.wordWrap = true; 
    theText.width = 200; 
    theText.antiAliasType = AntiAliasType.ADVANCED; 
    theText.autoSize = TextFieldAutoSize.LEFT; 
    theText.selectable = false; 
    theText.htmlText = xml.item[i][email protected]; 

    startHeight += theText.height + 10; 
} 
+0

謝謝!我知道這很簡單。第一行應該是:var startHeight:int = 0;「 – user2170376 2013-03-14 15:27:42

+0

哈哈是啊編輯太多的C++/AS3混合有時候會這樣:) – 2013-03-14 15:28:57