0

我在 as3 上有问题,我想在movieclip 中添加文本字段,

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

当我添加一行时它可以,但是当我使用循环时(多行是必需的)它不起作用,文本字段正在添加但背景影片剪辑(它位于我的库中的符号(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);             
        }
4

1 回答 1

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
于 2013-08-06T21:02:42.693 回答