我将如何更改 actionscript 代码以使测验能够从更长的列表中提供一组随机答案?例如,我是否可以让测验只显示一个 XML 文件中的 4 个随机答案,该文件总共包含 12 个答案作为标志?
我可以把我所有 12 个答案的所有标志都出来,在问题文本字段中只有一个问题女巫是其中一个标志的名称,但不知道如何随机获取四个标志并将它们放入文本字段中的舞台和名称之一
xml 示例:
<flags>
<flag path="../assets/flags/flag1.png">
<country>
Aeland
</country>
</flag>
<flag path="../assets/flags/AmSamoa.png">
<country>
AmSamoa
</country>
</flag>
<flag path="../assets/flags/Bahamas.png">
<country>
Bahamas
</country>
</flag>
<flag path="../assets/flags/CostaRica.png">
<country>
Costa Rica
</country>
</flag>
<flag path="../assets/flags/CotedIvorie.png">
<country>
Cotedivoire
</country>
</flag>
<flag path="../assets/flags/Croatia.png">
<country>
Croatia
</country>
</flag>
我的 AS3 代码是:
private function onXMLLoad(event:Event):void
{
_xml = new XML(event.target.data); //create XML document_xmlLoader.removeEventListener(Event.COMPLETE, onXMLLoad);
_myFlagList = new XMLList(_xml.flag);
_xmlLoader.removeEventListener(IOErrorEvent.IO_ERROR, onIOError);
// _xml.ignoreWhitespace = false;
// trace("MyFlag: _answers: ",_answers);
//trace(_loader);
//trace(targetBtn);
//trace(_myFlagList);
for each (var flagPath:XML in _xml.flag.@path)
{
trace("MyFlag: flagaths: ",flagPath);
_flagPaths.push(flagPath);
}
for each (var flag:XML in _xml.flag.country)
{ //trace("MyFlag: flagaths: ",flag);
_allFlags.push(flag);
}
createButtons();
}
private function onIOError(event:IOErrorEvent):void
{
_xmlLoader.removeEventListener(Event.COMPLETE, onXMLLoad);
_xmlLoader.removeEventListener(IOErrorEvent.IO_ERROR, onIOError);
trace('IO Error:', event.text);
}
private function createButtons():void
{
var col:int;
var row:int;
flagContainer = new Sprite();
trace("Number of flags",_allFlags.length);
for (var i:int = 0; i < NUMBER_OF_BTNS; i++)
{
_answers.push(_allFlags[i]);
targetBtn = new FlagButton(_allFlags[i],_flagPaths[i]);
targetBtn.x = col * (targetBtn.width + 10)+20;
targetBtn.y = row * (targetBtn.height+ 10)+20;
flagContainer.addChild(targetBtn);
addChild(flagContainer);
col++;
if( col == 3)
{
col = 0;
row++;
}
}
randomOption();
}
private function randomOption():void
{
trace("MyFlag: all flags: ", _allFlags);
flagToFind = _allFlags[Math.floor(Math.random() * _allFlags.length)];
trace("MyFlag: flag to find: ", flagToFind);
_bk.text.text=flagToFind;
}