我正在使用 AS3 在 flash builder 上制作一个测验应用程序。
我想加载一个 XML 文件来获取我的问题。但我想我无法加载我的 XML 文件。我google了很多。我搜索了论坛。但没有任何效果。我只是在这里看不到我的错误。
当我单击我想在舞台上查看的按钮时,我的 XML 文件是否已加载。但在这种情况下,我的舞台没有任何价值。
测验.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);
}
}
}
问题.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;
}
}
多项选择.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>