AS3程序.来自XMl的组合框中项目的随机顺序 [英] AS3 Procedural. Randomise order of Items in Combobox From XMl

查看:58
本文介绍了AS3程序.来自XMl的组合框中项目的随机顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取XML文件并将所有元素以随机顺序添加到ComboBox(我不知道如何执行此操作).我在舞台上制作了一个名为Primary_CB的ComboBox.我将XML全部设置为以下格式...

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>

我的AS3基本上读取XML文件并将内容填充到ComboBox中,就像这样...

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]);
}

我如何使其随机化元素的顺序,并将列表中的第一个元素添加到ComboBox的提示符?

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?

推荐答案

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. 创建了一对额外的临时数组
  2. 这些数组是从XML列表填充的
  3. 这些数组改组为全局数组
  4. 从全局数组中读取值到组合框
  5. 组合框selectedIndex属性设置为1(因此突出显示了列表中的第一个对象)
  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)

这是具有多种方式对数组进行排序的页面.在上面的示例中,我使用了拼接方法. http://dev.tutsplus.com/tutorials/quick-tip-how-to-randomly-shuffle-an-array-in-as3--active-8776

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

这篇关于AS3程序.来自XMl的组合框中项目的随机顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆