使用按钮在两个不同的XML文件之间切换?

时间:2013-08-06 11:18:42

标签: actionscript-3

我试图通过按两个按钮在两个不同的XML文件之间切换,当按下第一个我要加载t2.wml时,当我推动另一个我要加载t1.xml时。如果我将“num”设置为静态值,但我想根据我按下的按钮更改num的值,一切都有效。

我想我已经错过了一些明显但我无法找到的东西,而且我被困在这里。

任何人对如何解决这个问题都有任何想法?为什么我的函数不能改变num的值? :)

import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.Event;

stop();

var language:String;
var num:int;
var xmlData:XML = new XML ();   
var array:Array = ["t2.xml", "t1.xml"];
var loader_ul:URLLoader = new URLLoader();
loader_ul.load(new URLRequest(array[num]));



engBtn0.addEventListener(MouseEvent.CLICK, engButton);
sweBtn1.addEventListener(MouseEvent.CLICK, sweButton);
engBtn0.addEventListener(MouseEvent.CLICK, fileLoaded);
sweBtn1.addEventListener(MouseEvent.CLICK, fileLoaded);


function engButton (e:Event) {

    language = "eng";
    trace (language);
    trace (num);
    } // end engButton


 function sweButton (e:Event) {

    language = "swe";
    trace (language);
    trace (num);
    } // end sweButton



 function fileLoaded(e:Event)    {

    if (language == "eng"){
            num = 0;
            trace (num);
            xmlData = XML(loader_ul.data);
            myText_text.htmlText = xmlData.testeng;

    } else if (language == "swe") {

            num = 1;
            trace (num);
            xmlData = XML(loader_ul.data);
            myText_text.htmlText = xmlData.testswe;

    } else {
                    trace ("Error");
            }


  } // end fileLoaded

提前致谢!真的可以在这里使用一些帮助。

1 个答案:

答案 0 :(得分:0)

我会这样做:

import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.Event;

stop();

var language: String;

var loader_ul: URLLoader = new URLLoader();
loader_ul.addEventListener(Event.COMPLETE, fileLoaded)

engBtn0.addEventListener(MouseEvent.CLICK, engButton);
sweBtn1.addEventListener(MouseEvent.CLICK, sweButton);

function engButton(e: Event) {
    language = "eng";
    loader_ul.load(new URLRequest('t2.xml');
} 

function sweButton(e: Event) {
    language = "swe";
    loader_ul.load(new URLRequest('t1.xml')); 
} 

function fileLoaded(e:Event) {
    var xmlData:XML = new XML(e.target.data);

    if(language == "swe"){
        myText_text.htmlText = xmlData.testswe;
    } else if(language == "eng" {
        myText_text.htmlText = xmlData.testeng;
    }
}

基本上从按钮本身加载XML并向eventListener添加loader,因此当它完成时会触发函数fileLoaded,它将加载的xml放入正确的文本字段中。 (我会将XML更改为比testswetesteng更通用的内容。

希望它有所帮助。

如果你想让它更通用,你可以这样做:

engBtn0.addEventListener(MouseEvent.CLICK, onButtonClick);
sweBtn1.addEventListener(MouseEvent.CLICK, onButtonClick);

function onButtonClick(e:MouseEvent):void {
    var xmlfile:String = "";

    switch(e.target.name){
        case 'engBtn0':
            language = 'eng';
            xmlfile = 't2.xml';
            break;
        case 'sweBtn1':
            language = 'swe';
            xmlfile = 't1.xml';
            break;
    }
    loader_ul.load(new URLRequest(xmlfile)); 
}

并向eventlisteners添加更多case和更多switch;