Search code examples
xmlactionscript-3flashtext

AS3 text linkage to xml


I was testing various ways for linkage dynamic scrolling text and xml file I wanted to put on stage double text sliders each of them will fetch different data from xml file but getting random error here is the code:

//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];
    }

And xml file(text0) which I grab text from looks like this:

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

Solution

  • You have several problems in your code:

    You don't wait for XML, and start ENTER_FRAME with access to myItems

    There is no sense in additional information in the XML, because you apply same texts to the both fields.

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

    Or start your loop after you get 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);
    }