2014-03-13 108 views
0

試圖找出我如何複製此功能!所以我有一個代碼從XML文件中獲取簡單的文本數據,名爲text1.xml。在我的舞臺上有滾動文字告訴網站信息,但我需要另一個小小的字體放在剛剛超過第一個上面的scrooling文本,告訴不同的東西,所以我需要另一個XML文件,如text2.xml中的文本?如何升級這一點,這裏是代碼:AS3複製動態文本

//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(文本)文件是這樣的:

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

回答

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; 
    } 
} 
+0

有一些錯誤,我寫的是我用代碼完成 – user3391599

+0

無法訪問空對象引用的屬性或方法。 – user3391599

+0

但是你只是複製了我的代碼,甚至沒有分析它......你是否閱讀過代碼中的註釋? –