2014-03-14 23 views
0

,所以我進行聯動的動態滾動文本和xml文件,我希望把在舞臺上雙擊文本滑塊他們每個人將獲取從XML文件,但在這裏得到隨機誤差不同的數據TESTIN各種方法是代碼:AS3錯誤文本鏈接爲xml

//SCROLLING SPEED 
var scrolling_speed:int = 2; 
var scrolling_speed2:int = 4; 
//TEXT TO SCROLL 
var text_to_scroll:String = "www.posta.ba"; 


var xmlLoader: URLLoader = new URLLoader(); 
var myData: XML; 
var myItems: XMLList; 
var position: uint; 
xmlLoader.addEventListener(Event.COMPLETE, onComplete); 
xmlLoader.load(new URLRequest("text0.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(); 
var mySmallTextField:TextField = new TextField(); 
//add the field to stage 
addChild(my_text); 
addChild(mySmallTextField); 
//set the text 
my_text.text = text_to_scroll; 
mySmallTextField.text = text_to_scroll; 
//set the x coord off right side of stage 
my_text.x = stage.stageWidth; 
mySmallTextField.x = stage.stageWidth; 
//set y coord in middle of stage (about) 
my_text.y = (stage.stageHeight/1)-(my_text.height/2.5); 
mySmallTextField.y = (stage.stageHeight/1)-(my_text.height/1.7); 
//not selectable 
my_text.selectable = false; 
mySmallTextField.selectable = false; 
//no border 
my_text.border = false; 
mySmallTextField.border = false; 
//field scales with more text 
my_text.autoSize = TextFieldAutoSize.LEFT; 
mySmallTextField.autoSize = TextFieldAutoSize.LEFT; 

//set a format 
var my_text_format:TextFormat = new TextFormat(); 
var mySmallTextField_format:TextFormat = new TextFormat(); 

//set the color to the hex 
my_text_format.color = 0x000000; 
mySmallTextField_format.color = 0x000000; 
//set the font size 
my_text_format.size = 28; 
mySmallTextField_format.size = 22; 
//set the font face 
my_text_format.font = "Futura Md BT"; 
mySmallTextField_format.font = "Futura Md BT"; 
//apply formatting 
my_text.defaultTextFormat = my_text_format; 
my_text.setTextFormat(my_text_format); 
mySmallTextField.defaultTextFormat = mySmallTextField_format; 
mySmallTextField.setTextFormat(mySmallTextField_format); 




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

//scroll function 
function onMoveTexts(e:Event):void { 
    my_text.x-=scrolling_speed; 
    mySmallTextField.x-=scrolling_speed2; 
    if(my_text.x<-my_text.width){ 
     my_text.x=stage.stageWidth; 
    } 

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


     //Set next text 
     if(++position >= myItems.length()){ 
      position = 0; 
     } 
     my_text.text = myItems[position]; 
     mySmallTextField.text = myItems[position]; 
    } 

和XML文件(text0)至極我搶文本從這個樣子的:

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

回答

1

你在你的代碼的幾個問題:

你不要等到XML,並開始ENTER_FRAME訪問myItems

XML中的附加信息沒有意義,因爲您將相同的文本應用於這兩個字段。

function onMoveTexts(e:Event):void { 
    my_text.x -= scrolling_speed; 
    mySmallTextField.x -= scrolling_speed2; 
    if (my_text.x < -my_text.width) { 
     my_text.x = stage.stageWidth; 

     if (myItems != null) { 
      //Set next text 
      if (++position >= myItems.length()) { 
       position = 0; 
      } 
      my_text.text = myItems[position]; 
     } 
    } 

    if (mySmallTextField.x < -mySmallTextField.width) { 
     mySmallTextField.x = stage.stageWidth; 
     if(myData != null){ 
      mySmallTextField.text = myData.anotherData.info; 
     } 
    } 
} 

或開始循環你XML後:

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

    //add the listener to scroll 
    addEventListener(Event.ENTER_FRAME, onMoveTexts); 
} 
+0

非常感謝尼古拉你是這裏真正的朋友!我wacthed你的網頁,我看你是真有創意,如果你需要一些平面設計,網頁的想法,字符畫什麼從藝術領域,我可以幫助你這裏是我的網頁: – user3391599

+0

http://dizajnservis.site90.com / – user3391599