我试图将每个“问题”标签中的“txt”属性的内容推入AS3 Flash中名为“questions”的数组中。这是我的xml文件的摘录。XML - 针对节点属性,推入Flash AS3阵列
<question id='Q1' uId='99036' no_ans='2' txt='In a flat structure employees are not expected to provide their bosses with their opinions.' feedback='' type='MC' passingWeight='1' url='media/'>
<answer id='Q1A1' uId='311288' txt='True' weight='0'/>
<answer id='Q1A2' uId='311289' txt='False' weight='1'/>
</question>
<question id='Q2' uId='99037' no_ans='2' txt='In a hierarchy, information typically flows downward.' feedback='' type='MC' passingWeight='1' url='media/'>
<answer id='Q2A1' uId='311290' txt='True' weight='1'/>
<answer id='Q2A2' uId='311291' txt='False' weight='0'/>
</question>
<question id='Q3' uId='99038' no_ans='2' txt='Someone who keeps many projects going at one time is an example of someone who is flexible-time oriented.' feedback='' type='MC' passingWeight='1' url='media/'>
<answer id='Q3A1' uId='311292' txt='True' weight='1'/>
<answer id='Q3A2' uId='311293' txt='False' weight='0'/>
</question>
这是我在一个循环的尝试:
// get number of questions
trace(myXML.question.length());
numberOfQuestions = myXML.question.length();
//loop and push questions into questions array at top
for (var i:int = 0; i < numberOfQuestions; i++) {
trace("Hello.");
questions.push([email protected]);
trace(questions);
}
这只是推的问题,所有9日一旦进入阵列的每个位置。我想每个阵列位置1个问题。我不确定如何在问题标记中使用id属性来区分每个问题。
编辑:我尝试这样做,我可以从的processXML函数中(2)访问使用getQuestionAt问题文本,但不应超出它。
var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("html/VUBZ7318CROSSCULTUREQUIZ/manifest.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void {
myXML = new XML(e.target.data);
//trace(myXML.question)
// get number of questions
trace(myXML.question.length());
numberOfQuestions = myXML.question.length();
//Question list
var questions:Object = {};
//Extracting question from xml
for each (var item:XML in myXML.question) {
questions[item. @ id] = item. @ txt;
}
//Some method for fetching question from question list
function getQuestionAt(index:Number):String {
if (questions["Q" + index] == undefined) {
throw new Error("Wrong index for question!!!");
}
return questions["Q"+index];
}
//Getting question from list
trace("Here is question No 2:\t" + getQuestionAt(2));
}
我如何进入的问题之一来自不同帧的FLA? 我试图在第6帧使用** text_txt.htmlText = getQuestionAt(2); **来填充文本字段,但它不起作用。 – Livi17 2012-03-15 21:40:10
创建只有一个帧的新图层,并且只要您的总帧数(例如6个)就可以创建该帧的长度。然后在该框架中添加AS代码。在这种情况下,您可以在任何帧中调用该函数,因为它在所有帧上都是“可见的”。 – Engineer 2012-03-15 21:53:15
我试过你的建议,但它仍然无法工作......是因为我把你的代码放在** processXML()**函数中?看到我上面的编辑。它在函数内部工作,但不在外部,或从fla中的任何其他地方工作。 – Livi17 2012-03-15 22:06:42