AS3代码xml数据获取

时间:2014-03-12 13:04:02

标签: xml actionscript-3 flash scroll

所以基本上我有这个代码用于简单的文本新闻滚动,但是我如何为xml或txt文件中的wiritten的外部源添加xml数据读取(获取)文本的代码? 我还想知道这段代码的xml文件是什么样的。

//SCROLLING SPEED
var scrolling_speed:int = 2;
//TEXT TO SCROLL
var text_to_scroll:String = "This is my text";


//establish the field
var my_text:TextField = new TextField();
//add the field to stage
addChild(my_text);
//set the text
my_text.text = text_to_scroll;
//set the x coord off right side of stage
my_text.x = stage.stageWidth;
//set y coord in middle of stage (about)
my_text.y = (stage.stageHeight/1)-(my_text.height/2.5);
//not selectable
my_text.selectable = false;
//no border
my_text.border = false;
//field scales with more text
my_text.autoSize = TextFieldAutoSize.LEFT;

//set a format
var my_text_format:TextFormat = new TextFormat();
//set the color to the hex
my_text_format.color = 0x000000;
//set the font size
my_text_format.size = 28;
//set the font face
my_text_format.font = "Futura Md BT";
//apply formatting
my_text.defaultTextFormat = my_text_format;
my_text.setTextFormat(my_text_format);

//add the listener to scroll
my_text.addEventListener(Event.ENTER_FRAME,move_text);

//scroll function
function move_text(myevent:Event):void {
    my_text.x-=scrolling_speed;
    if(my_text.x<(0-my_text.width)){
        my_text.x=stage.stageWidth;
    }
}

1 个答案:

答案 0 :(得分:0)

您可以根据需要设计数据源(XML),例如:

<?xml version="1.0"?>
<data>

    <news>
        <item><![CDATA[Text for news 1]]></item>
        <item><![CDATA[Text for news 2]]></item>
        <item><![CDATA[Text for news 3]]></item>
        <item><![CDATA[Text for news 4]]></item>
        <item><![CDATA[Text for news 5]]></item>
        <item><![CDATA[Text for news 6]]></item>
    </news>
</data>

在处理数据之前,不要忘记预加载它:

var xmlLoader: URLLoader = new URLLoader();
var myData: XML;
var myItems: XMLList;
var position: uint;
xmlLoader.addEventListener(Event.COMPLETE, onComplete);
xmlLoader.load(new URLRequest("path/to/xml"));

function onComplete(e : Event): void{
    myData = new XML(URLLoader(e.currentTarget).data);
    myItems = myData..item;
}

从数据源更改文本字段中的文本:

function move_text(myevent:Event):void {
    my_text.x-=scrolling_speed;
    if(my_text.x<-my_text.width){
        my_text.x=stage.stageWidth;
        //Set next text
        if(++position >= myItems.length()){
            position = 0;
        }
        my_text.text = myItems[position];
    }
}

并且不要忘记使用XML中的第一个元素启动文本字段