如何添加到舞台,从答案的一个更大的数组4 mathrandom的答案?答案、更大、数组、舞台

2023-09-08 14:27:28 作者:清晨自渎

我将如何改变动作code,使测验以present一组随机从一个更长的名单的答案?例如,我可以有测验present仅有4从包含共12个答案为标志的XML文件的随机答案?

我可以把我所有的12个回答所有的标志出来,并在问题文本字段只有一个问题,女巫的标志之一的名字,但不知道如何随机得到短短四年的标志和把它们放在舞台上,并在文本字段的名称之一

XML例子:

 <标志>
    <旗路径=../资产/标记/ flag1.png>
         <国家>
            Aeland
         < /国家>
    < /标志>

    <旗路径=../资产/标记/ AmSamoa.png>
         <国家>
            AmSamoa
         < /国家>
    < /标志>

    <旗路径=../资产/标记/ Bahamas.png>
         <国家>
            巴哈马
         < /国家>
    < /标志>
    <旗路径=../资产/标记/ CostaRica.png>
         <国家>
            哥斯达黎加
         < /国家>
    < /标志>
    <旗路径=../资产/标记/ CotedIvorie.png>
         <国家>
            Cotedivoire
         < /国家>
    < /标志>
    <旗路径=../资产/标记/ Croatia.png>
         <国家>
            克罗地亚
         < /国家>
    < /标志>
 

和我的AS3 code是:

 私有函数onXMLLoad(事件:事件):无效
{
    _xml =新的XML(event.target.data); //创建XML document_xmlLoader.removeEventListener(引发Event.COMPLETE,onXMLLoad);
    _myFlagList =新的XMLList(_xml.flag);

    _xmlLoader.removeEventListener(IOErrorEvent.IO_ERROR,onIOError);
    // _xml.ignoreWhitespace = FALSE;
    //跟踪(MyFlag:_answers:_answers);
    //跟踪(_loader);
    //跟踪(targetBtn);
    //跟踪(_myFlagList);


    每个(VAR flagPath:XML在_xml.flag @路径)
    {
        跟踪(MyFlag:flagaths:flagPath);
        _flagPaths.push(flagPath);
    }


    每个(VAR标志:XML在_xml.flag.country)
    {//跟踪(MyFlag:flagaths:标志);
        _allFlags.push(标志);
    }

    createButtons();

}


私有函数onIOError(事件:IOErrorEvent):无效
{
    _xmlLoader.removeEventListener(引发Event.COMPLETE,onXMLLoad);
    _xmlLoader.removeEventListener(IOErrorEvent.IO_ERROR,onIOError);
    跟踪(IO错误:',event.text);

}

私有函数createButtons():无效
{


    VAR西:INT;
    VAR行:INT;
    flagContainer =新的Sprite();


    跟踪(旗号,_ allFlags.length);


    对于(VAR我:= 0; I< NUMBER_OF_BTNS;我++)
    {
        _answers.push(_allFlags [I]);

        targetBtn =新FlagButton(_allFlags [I],_ flagPaths [I]);
        targetBtn.x = COL *(targetBtn.width + 10)+20;
        targetBtn.y =行*(targetBtn.height + 10)+20;
        flagContainer.addChild(targetBtn);
        的addChild(flagContainer);



        COL ++;

        如果(COL == 3)
        {
            COL = 0;
            排++;
        }
    }



    randomOption();
}
私有函数randomOption():无效
{


    跟踪(MyFlag:所有的标志:_allFlags);

    flagToFind = _allFlags [Math.floor(的Math.random()* _allFlags.length)];
    跟踪(MyFlag:标志地发现:flagToFind);

    _bk.text.text = flagToFind;

}
 

解决方案 Java中math.random 100是什么

您基本上要的对象数组。或许,如果你安排你的旗帜在XML中,因为这样它会更好:

 <旗>
     <路径> ../资产/标志/ flag1.png< /路径>
     <国家> Aeland< /国家>
< /标志>
 

有了这个,你将最终标志在你的XML对象的数组,它可以迭代。

关于从数组随机选择 - 这是一个容易的任务,只要确保你没有选择,你已经选择了previously指数

  VAR选择:数组= [];
而(selected.length 4;){
    变种SI:INT = Math.floor(的Math.random()* flags.length);
    如果(selected.indexOf(SI)==  -  1)selected.push(SI);
}
跟踪(标志[选择[0]); //首先选择标志
跟踪(标志[选择了[3]); //第四个选择的标志
 

How would I change the actionscript code to enable the quiz to present a random set of answers from a longer list? For example, could I have the quiz present just 4 random answers from an XML file containing 12 answers as flags in total?

I can get to put all of my 12 answers all the flags come out and in the question text field just one question witch is the name of one of those flags, but dont know how to randomly get just four of the flags and put them in the stage and one of the names in the text field

Xml example:

 <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>   

And my AS3 code is:

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;

}

解决方案

You basically want an Array of Objects. Perhaps it will be better if you arrange your flags in XML as such:

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

With this, you will end up with an array of flag in your XML object, which can be iterated.

About selecting from an array at random - it's an easier task, just make sure you didn't select an index you've already selected previously.

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