AS3错误文本链接到xml

时间:2014-03-14 09:37:08

标签: xml actionscript-3 flash text

所以我在各种方式测试链接动态滚动文本和xml文件我想放在舞台双文本滑块上每个都会从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 个答案:

答案 0 :(得分: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);
}
相关问题