0

Hi users of stack overflow I am currently struggling with randomizing my data via as3 and xml.

I can load in the xml fine and generate a random venue however when I click on the random button I have created, the same node is shown twice! Basically I just want to randomly select data with no repeat of the previous venue if this makes sense.

My xml:

<gallery>
<venue>
        <name>1</name>
        <description>1</description>
        <picture>images/1.jpg</picture>
        <thumb>thumbs/1.jpg</thumb>
        <address>1</address>
        <website>http://1.co.uk</website>
</venue>

<venue>
        <name>2</name>
        <description>2</description>
        <picture>images/2.jpg</picture>
        <thumb>thumbs/2.jpg</thumb>
        <address>2</address>
        <website>http://2.co.uk</website>
</venue>

<venue>
        <name>3</name>
        <description>3</description>
        <picture>images/3.jpg</picture>
        <thumb>thumbs/3.jpg</thumb>
        <address>3</address>
        <website>http://3.co.uk</website>
</venue>
</gallery>

My current code:

var xml:XML = <venues>
<venue name="" description="" address="" website="" picture=""/>
<venue name="" description="" address="" website="" picture=""/> 
<venue name="" description="" address="" website="" picture=""/>
<venue name="" description="" address="" website="" picture=""/>
</venues>;

var Gallerylist:XMLList = new XMLList(xml.venue);

function RandomGallery(e:Event)
{
    var rand:int = Gallerylist.length() * Math.random();
    myTextBoxTitle.text = myXML.venue.name[rand]
    myTextBoxDes.text = myXML.venue.description[rand]
    myTextBoxAddress.text = myXML.venue.address[rand]
    myTextBoxWeb.text = myXML.venue.website[rand]
    myVenueImage.source = myXML.venue.picture[rand]
}
randomBTN.addEventListener(MouseEvent.MOUSE_DOWN, RandomGallery);
4

2 回答 2

1

创建一个包含所有场地名称的数组。如果数据集太大,或者刚开始为您的示例使用硬编码值以使其工作,您可以通过编程方式执行此操作。当您单击随机按钮时,弹出名称并使用它来选择下一个。这将避免必须检查哪些已被使用,而您只需从数组中剩余的尚未查看的中进行选择。当用户选择最后一个并且数组为空时,重新初始化并继续。

于 2012-11-23T14:59:26.877 回答
0

如果您不介意直接打乱您的 xml 以避免制作名称的新副本,您可以使用像FicherYates这样的打乱功能来打乱您的数据。

您可以制作一个多合一功能,每次单击新场地时都会拾取它,并在到达终点时重新启动。

这是一个函数示例,每次响应点击事件时都会选择一个随机元素:

var xml:XML = <venues>
<venue name="" description="" address="" website="" picture=""/>
<venue name="" description="" address="" website="" picture=""/> 
<venue name="" description="" address="" website="" picture=""/>
<venue name="" description="" address="" website="" picture=""/>
</venues>;


var fnShuffle:Function = function(xl:XMLList):Function {
 var len:int=xl.length();
 var lastUsedIndex:int;

 return function(e:MouseEvent):void{
   var i:int;

   if (len<=0) {
     // restart over since you have reached the end of the list
     len = xl.length();
     i = int(Math.random() * (len--));

     // in case of a new round you don't want to redisplay the last one again
     if (i == lastUsedIndex) i = len; 
   } else {
     i = int(Math.random() * (len--));
   }

   var myRandomVenue:XML = xl[i];
   var tmp:XML = xl[len];
   xl[i] = tmp;
   xl[len] = myRandomVenue;
   lastUsedIndex = len;

   // here do what you want with your randow venue
   trace(myRandomVenue);
 }
}

this.addEventListener(MouseEvent.CLICK, fnShuffle(xml.venue));

每次单击 TextArea 时,您都可以在 wonderfl 现场看到它: http ://wonderfl.net/c/aQ1D

于 2012-11-23T17:11:45.983 回答