Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flash 如何从更大的答案数组中添加到第四阶段的随机答案?_Flash_Actionscript 3 - Fatal编程技术网

Flash 如何从更大的答案数组中添加到第四阶段的随机答案?

Flash 如何从更大的答案数组中添加到第四阶段的随机答案?,flash,actionscript-3,Flash,Actionscript 3,如何更改actionscript代码以使测验能够从较长的列表中呈现一组随机答案?例如,我是否可以让测验从一个包含12个答案作为标记的XML文件中只显示4个随机答案 我可以把我的12个答案全部放出来,在问题文本字段中只有一个问题是其中一个标志的名称,但是我不知道如何随机得到四个标志,然后把它们放在舞台上,把其中一个名称放在文本字段中 Xml示例: <flags> <flag path="../assets/flags/flag1.png">

如何更改actionscript代码以使测验能够从较长的列表中呈现一组随机答案?例如,我是否可以让测验从一个包含12个答案作为标记的XML文件中只显示4个随机答案

我可以把我的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;

}
私有函数onXMLLoad(事件:event):void { _xml=new xml(event.target.data);//创建xml文档_xmloader.removeEventListener(event.COMPLETE,onXMLLoad); _myFlagList=新的XMLList(_xml.flag); _removeEventListener(IOErrorEvent.IO_ERROR,onIOError); //_xml.ignoreWhitespace=false; //跟踪(“MyFlag:_answers:,_answers”); //跟踪(_加载器); //跟踪(targetBtn); //跟踪(_myFlagList); 对于每个(var flagPath:XML in_XML.flag.@path) { 跟踪(“MyFlag:flagaths:,flagPath”); _flagPath.push(flagPath); } 对于每个(var flag:XML in_XML.flag.country) {//trace(“MyFlag:flagaths:,flag”); _allFlags.push(flag); } createButtons(); } 私有函数错误(事件:IOErrorEvent):无效 { _removeEventListener(Event.COMPLETE,onXMLLoad); _removeEventListener(IOErrorEvent.IO_ERROR,onIOError); 跟踪('IO错误:',event.text); } 私有函数createButtons():void { var-col:int; var行:int; flagContainer=新精灵(); 跟踪(“标志数”,_allFlags.length); 对于(变量i:int=0;i您基本上需要一个对象数组。如果您将标记以XML形式排列,可能会更好:

<flag> 
     <path>../assets/flags/flag1.png</path> 
     <country>Aeland</country>
</flag> 

非常感谢你的输入,谢谢你的回答,这很有帮助,还有一个问题,它只给我两个对象,但如果我想一直有四个对象?如何操作,同时我想在invosoble按钮中显示它们,提前感谢您的时间。我已经显示了如何访问四个选定对象中的单个对象,使用此技术访问所有对象。关于按钮,您需要在其中放置四个位置,以便每个位置都可以接受此类对象,然后分配或
addChild()
或chenge text或任何您认为合适的内容。
var selected:Array=[];
while (selected.length<4) {
    var si:int=Math.floor(Math.random()*flags.length);
    if (selected.indexOf(si)==-1) selected.push(si);
}
trace(flags[selected[0]]); // first selected flag
trace(flags[selected[3]]); // fourth selected flag