在库项目中添加子元素

时间:2013-08-06 19:33:10

标签: actionscript-3 textfield addchild

我在as3上遇到问题,我想在movieclip中添加textfield,

你可以看到第一张图片,它看起来有一行。但我需要不止一排。我试图开发一个组件。这将从xml文件中获取日期并列出这些数据(xml数据)。

当我添加一行时它确定,但是当我使用循环(它需要多行)时它不起作用,textfield正在添加但背景movieclip(它位于我的库中的符号(WhiteLine))是不!

这段代码是成功的,

            var myTextField:TextField = new TextField();      
            myTextField.text = "some text here!";  
            myTextField.width = 250;  
            myTextField.x = 0;  
            myTextField.y = 0;
            myTextField.selectable = true;  
            myTextField.autoSize = TextFieldAutoSize.LEFT;  
            var myFormat:TextFormat = new TextFormat();  
            myFormat.color = 0xAA0000;
            myFormat.size = 12; 
            myFormat.italic = true;
            myTextField.setTextFormat(myFormat);  
            item =  new WhiteLine;
            item.x = 1;
            item.y = (i*30)+1+3*i;
            item.addChild(myTextField);
            avatar.addChild(item);              
            item.addEventListener(MouseEvent.MOUSE_OVER, listItemOver);

这是结果的截图, //resim

但是当我使用循环时,我得到了这个结果 resim2

这是带循环的代码

        for(var i:int=0;i<25;i++)
        {               
            var myTextField:TextField = new TextField();      
            myTextField.text = "some text here!";  
            myTextField.width = 250;  
            myTextField.x = 0;  
            myTextField.y = 0;
            myTextField.selectable = true;  
            myTextField.autoSize = TextFieldAutoSize.LEFT;  
            var myFormat:TextFormat = new TextFormat();  
            myFormat.color = 0xAA0000;
            myFormat.size = 12; 
            myFormat.italic = true;
            myTextField.setTextFormat(myFormat);  
            item =  new WhiteLine;
            item.x = 1;
            item.y = (i*30)+1+3*i;
            item.addChild(myTextField);
            avatar.addChild(item);              
            item.addEventListener(MouseEvent.MOUSE_OVER, listItemOver);             
        }

1 个答案:

答案 0 :(得分:0)

如果您使用循环来使用XML文件中的数据,则需要从外部XML文件中加载xml数据(我已经非常快速地模拟了这一点 - 我已经使用文档类创建了一个示例 - 如果您在Flash时间轴中编写动作脚本,则需要修改此代码)

package {
import flash.events.Event;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.text.TextFormat;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.Font;

public var loader:URLLoader;
public var data:XML;
public var items:XMLList;

public class DocumentClass extends MovieClip {
public function DocumentClass() {
            dataLoad();    
}
private function dataLoad():void {
    loader = new URLLoader();
    loader.addEventListener(Event.COMPLETE, dataLoaded); 
    loader.load(new URLRequest("data.xml")); // change data.xml to your xml file's path
} // end of dataLoad()

private function dataLoaded(event:Event):void {
// this property holds the loaded xml data
    data = new XML(event.target.data); // event.target.data relates to the dataLoad() function that results in dataLoaded() being called  

    // the items property holds all the repeating item elements
    items = data.item;

    // beginning of loop - items.length is the number of items in the xml file
    for (var i = 0; i < items.length(); i++) {
    var myTextField:TextField = new TextField();      
    addChild(myTextField);
    myTextField.text = items[i].toString();
    myTextField.width = 250;  
    myTextField.x = 0;  
    myTextField.y = 0;
    myTextField.selectable = true;  
    myTextField.autoSize = TextFieldAutoSize.LEFT;  
    var myFormat:TextFormat = new TextFormat();  
    myFormat.color = 0xAA0000;
    myFormat.size = 12; 
    myFormat.italic = true;
    myTextField.setTextFormat(myFormat); 
    item =  new WhiteLine;
    item.x = 1;
    item.y = (i*30)+1+3*i;
    item.addChild(myTextField);
    avatar.addChild(item);
    } // end of loop
    } // end of dataLoaded()
  } // end of class  
} // end of package