AS3复制动态文本

时间:2014-03-13 11:51:04

标签: actionscript-3 flash text duplicates

试着找出我怎么能复制这个功能!所以我有一个代码从名为text1.xml的xml文件中获取简单的文本数据。在我的舞台上有滚动文本,它告诉网站信息,但我需要另一个scrooling文本,首先放置在上面的小字体,告诉不同的东西,所以我需要另一个文本在其中的文本像text2.xml吗?如何升级,这是代码:

//SCROLLING SPEED
var scrolling_speed:int = 7;
//TEXT TO SCROLL
var text_to_scroll:String = "Main site info like:www.mysite.com";

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

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


//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<-my_text.width){
        my_text.x=stage.stageWidth;
        //Set next text
        if(++position >= myItems.length()){
            position = 0;
        }
        my_text.text = myItems[position];
    }
}

文本的xml(text1)文件如下所示:

<?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>

1 个答案:

答案 0 :(得分:0)

为什么还需要另一个xml文件?您已经拥有数据源,因此将附加信息添加到您拥有的xml中并不是一个问题。

使用必要信息扩展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>

    <anotherData>
            <info><![CDATA[Text for small sized text]]></info>
    </anotherData>
</data>

放置文字字段并将文字应用于其中:

//initialise text field with format, position, etc
mySmallTextField.text = myData.anotherData.info;

滚动

function move_text(myevent:Event):void {
    //your code for my_text

    mySmallTextField.x = -someSpeed;
    if(mySmallTextField.x < -mySmallTextField.width){
        mySmallTextField.x = stage.stageWidth;
    }
}