我試圖將每個「問題」標籤中的「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