AS3 Procedural. Randomise order of Items in Combobox From XMl

ぃ、小莉子 提交于 2019-12-25 04:27:18

问题


I am attempting to read an XML file and add all the elements to a ComboBox in a random order (I have no idea how to do this). I have produced a ComboBox on stage called Primary_CB. I have my XML all set in the following format...

<data>

<elements>

    <element1>
        <primary>Male Character</primary>
    </element1>
    <element1>
        <primary>Female Character</primary>
    </element1>

</elements>

</data>

My AS3 basically reads the XML file and populates the contents into the ComboBox like so...

Primary_CB.prompt = "Items";

var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
var xmlAry:Array = new Array();
var xmlURL:Array = new Array();

xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest("IdeaElements.xml"));

function LoadXML(e:Event):void
{
    xmlData = new XML(e.target.data);
    trace(xmlData..primary.length());
    for (var i:uint=0; i<xmlData..primary.length(); i++)
    {
        xmlAry.push(xmlData..primary[i]);
        xmlURL.push(xmlData..url[i]);
        Primary_CB.addItem( { label: xmlAry[i], data:i } );
        Primary_CB.addEventListener(Event.CHANGE, action);
    }
}

function action(e:Event):void
{
    var no:Number = Number(Primary_CB.selectedItem.data);
    trace(xmlURL[no]);
}

How would I make it randomize the order of the elements and also add the first element in the list to the prompt of the ComboBox?


回答1:


Primary_CB.prompt = "Items";

var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
var xmlAry:Array = new Array();
var xmlURL:Array = new Array();

xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest("IdeaElements.xml"));

function LoadXML(e:Event):void
{
    xmlData = new XML(e.target.data);
    trace(xmlData..primary.length());
    var tempXMLAry:Array = new Array();
    var tempUrlAry:Array = new Array();
    var i:uint;
    for (i = 0; i<xmlData..primary.length(); i++)
    {
        tempXMLAry.push(xmlData..primary[i]);
        tempUrlAry.push(xmlData..url[i]);
    }
    var tLen:int = tempXMLAry.length; // Store that value as it is here, because it will change during the loop
    for (i = 0; i < tLen; i++) {
        var rnd:int = Math.round(Math.random() * (tempXMLAry.length - 1));
        xmlAry.push(tempXMLAry[rnd]);
        xmlURL.push(tempUrlAry[rnd]);
        tempXMLAry.splice(rnd, 1);
        tempUrlAry.splice(rnd, 1);
    }
    for (i = 0; i < xmlAry.length; i++) {
        Primary_CB.addItem( { label: xmlAry[i], data:i } );
    }
    Primary_CB.selectedIndex = 0;
    Primary_CB.addEventListener(Event.CHANGE, action);
}

function action(e:Event):void
{
    var no:Number = Number(Primary_CB.selectedItem.data);
    trace(xmlURL[no]);
}

This is what I have come up with. It is very similar to the one you already have. What happens is:

  1. An extra pair of temporary arrays are created
  2. These arrays are populated from the XML list
  3. These arrays are shuffled into the global arrays
  4. The values are read from the global arrays into the combobox
  5. The comboboxes selectedIndex property is set to 1 (so it highlights the first object in the list)

Here is a page with multiple ways to sort an array. In the sample above, I used the splicing approach. http://dev.tutsplus.com/tutorials/quick-tip-how-to-randomly-shuffle-an-array-in-as3--active-8776




回答2:


I would get all the elements of the array pushed first, then shuffle them, then add them to the CB. To shuffle an array is fairly straight forward. Just use the top answer here (but use AS3 syntax):

How to randomize (shuffle) a JavaScript array?

(You can lose the Event.CHANGE eventListener & function, unless you have other plans to use it.)



来源:https://stackoverflow.com/questions/21266989/as3-procedural-randomise-order-of-items-in-combobox-from-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!