AS3无法加载XML文件

时间:2013-01-02 14:41:00

标签: xml actionscript-3

我正在使用AS3在Flash Builder上制作一个测验应用程序。

我想加载一个XML文件来解答我的问题。但我想我无法加载我的XML文件。我用Google搜索了很多。我搜索了论坛。但没有任何效果。我这里没有看到我的错误。

如果我的XML文件已加载,当我单击我想在我的舞台上看到的按钮时。但在这种情况下,我的舞台上没有任何价值。

Quiz.as

package
{   
    [SWF('width'= 800, 'height' = 600, 'frameRate'=40)]
    public class Quiz extends Sprite
    {
                // gloabal container 
        public static var container:Sprite = new Sprite();
        // button object 
        public var btnMulti:MyButton = new MyButton();

        public function Quiz()
        {
            init();
        }

        private function init():void {
            addChild(container);
            makeMenu();
        }


        private function makeMenu():void {
            btnMulti.buttonText = "multipleChoice";
            btnMulti.x = 50;
            btnMulti.y = 20;
            btnMulti.addEventListener(MouseEvent.CLICK, multiHandler);
                        // add button to the stage
            container.addChild(btnMulti);
        }

        private function multiHandler(e:MouseEvent):void {
            // if button clicked make a Question object
            var q:Question = new Question();

            // get the value
            var questionText:String = q.getter;

            // print the value in a textfield on the stage
            var tField:TextField = new TextField();
            tField.text = questionText;
            container.addChild(tField);
        }

    }
}

Question.as

public class Question extends Sprite
    {
        private var test:String;    

        public function Question()
        {
            // xml
            var urlReq:URLRequest = new URLRequest("./files/multiplechoice.xml"); 
            var urlLoad:URLLoader = new URLLoader();
            urlLoad.addEventListener(Event.COMPLETE, textLoaded);
            urlLoad.load(urlReq); 
        }

        private function textLoaded(e:Event):void {
            XML.ignoreWhitespace = true;
            var xml:XML = new XML(e.target.data);

            // check if loaded
            test = "xml loaded";
        }

        // getter
        public function get getter():String {
            return test.questionText;
        }
    }

multiplechoice.xml

<?xml version="1.0" encoding="UTF-8"?>
<multipleChoice>
    <questionSet>
        <question>Question 1</question>
        <answer correct="yes">Answer 1</answer>
        <answer>Answer 2</answer>
        <answer>Answer 3</answer>
        <answer>Answer 4</answer>
    </questionSet>

    <questionSet>
        <question>Question 2</question>
        <answer correct="yes">Answer 1</answer>
        <answer>Answer 2</answer>
        <answer>Answer 3</answer>
        <answer>Answer 4</answer>
    </questionSet>

    <questionSet>
        <question>Question 3</question>
        <answer correct="yes">Answer 1</answer>
        <answer>Answer 2</answer>
        <answer>Answer 3</answer>
        <answer>Answer 4</answer>
    </questionSet>
</multipleChoice>

1 个答案:

答案 0 :(得分:2)

问题是你有一个textLoaded的回调函数,但在调用getter()之前你并没有等待它加载。先试试这个:

public function get getter():String {
    if(test) {
        return test.questionText;
    } else {
        return "Data not available";
    }

但您也可以将一个函数引用传递给Question类,以便在加载时将值传回。您可能想要考虑在任何情况下预加载问题。

public class Question extends Sprite
{
    private var test:String;
    private var callback:Function; 

    public function Question(_callback:Function)
    {
        this.callback = _callback;
        // xml
        var urlReq:URLRequest = new URLRequest("./files/multiplechoice.xml"); 
        var urlLoad:URLLoader = new URLLoader();
        urlLoad.addEventListener(Event.COMPLETE, textLoaded);
        urlLoad.load(urlReq); 
    }

    private function textLoaded(e:Event):void {
        urlLoad.removeEventListener(Event.COMPLETE, textLoaded);

        XML.ignoreWhitespace = true;
        var xml:XML = new XML(e.target.data);

        // check if loaded
        this.callback(xml);
    }
}


// in the main class
    private function multiHandler(e:MouseEvent):void {
        // if button clicked make a Question object
        var q:Question = new Question(this.questionCallback);

    }

    public function questionCallback(xml:XML):void
        // get the value from the xml

        var tField:TextField = new TextField();
        tField.text = <<parsed xml....>>;
        container.addChild(tField);
    }

您可能想要创建textfield和question类属性,而不是将范围保持为函数本地,以便您也可以访问它们。