获得一个随机的场地使用ActionScript没有重复 [英] Getting a random venue using actionscript with no repeat

查看:109
本文介绍了获得一个随机的场地使用ActionScript没有重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

堆栈溢出的用户您好,我目前正在努力通过AS3和XML随机我的数据。

我可以在XML罚款加载并生成一个随机地点但是当我点击我创建了随机按钮,同一个节点显示两次!基本上,我只是想用随机的无重复的previous场地的选择数据,如果这是有道理的。

我的XML:

 <画廊>
<场地>
        <名称> 1 LT; /名称>
        <描述> 1< /描述>
        <图片>图片/ 1.JPG< /图片>
        <拇指>拇指/ 1.JPG< /拇指>
        <地址> 1 LT; /地址>
        &LT;网站&GT; HTTP://1.co.uk< /网站&GT;
&LT; /场地&GT;

&LT;场地&GT;
        &LT;名称&gt; 2'; /名称&gt;
        &所述;描述→2&其中; /描述&GT;
        &LT;图片&GT;图片/ 2.JPG&LT; /图片&GT;
        &LT;拇指&GT;拇指/ 2.JPG&LT; /拇指&GT;
        &其中;地址&gt; 2'; /地址&gt;
        &LT;网站&GT; HTTP://2.co.uk< /网站&GT;
&LT; /场地&GT;

&LT;场地&GT;
        &LT;名称&gt;第3版​​; /名称&gt;
        &LT;描述&GT;第3版; /描述&GT;
        &LT;图片&GT;图片/ 3.JPG&LT; /图片&GT;
        &LT;拇指&GT;拇指/ 3.JPG&LT; /拇指&GT;
        &LT;地址&gt;第3版​​; /地址&gt;
        &LT;网站&GT; HTTP://3.co.uk< /网站&GT;
&LT; /场地&GT;
&LT; /画廊&GT;
 

我目前的code:

  VAR的xml:XML =&LT;场地&GT;
&LT;场馆名称=描述=地址=网站=图片=/&GT;
&LT;场馆名称=描述=地址=网站=图片=/&GT;
&LT;场馆名称=描述=地址=网站=图片=/&GT;
&LT;场馆名称=描述=地址=网站=图片=/&GT;
&所述; /场地取代;

VAR Gallerylist:返回XMLList =新的XMLList(xml.venue);

功能RandomGallery(五:事件)
{
    VAR兰德:INT = Gallerylist.length()*的Math.random();
    myTextBoxTitle.text = myXML.venue.name [兰特]
    myTextBoxDes.text = myXML.venue.description [兰特]
    myTextBoxAddress.text = myXML.venue.address [兰特]
    myTextBoxWeb.text = myXML.venue.website [兰特]
    myVenueImage.source = myXML.venue.picture [兰特]
}
randomBTN.addEventListener(的MouseEvent.MOUSE_DOWN,RandomGallery);
 

解决方案

创建与场馆的所有名称的数组。你可以做到这一点编程如果数据集变得过大或刚开始用硬codeD值的例子来得到它的工作。当您单击随机按钮,弹出名关闭,并用它来挑选下一个。这将避免进行检查,看哪些已经被使用,你只需要挑选剩下的还没有被观看的数组中的人。当用户选择的最后一个,该阵列是空的,重新初始化它,继续

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

解决方案

Create an array with all the names of the venues. You can do this programmatically if the data set gets too large or just start out with hard coded values for your example to get it to work. When you click the random button, pop the name off and use it to pick the next one. This will avoid having to check to see which ones have already been used and you just have to pick from the ones remaining in the array that haven't been viewed. When the user selects the last one and the array is empty, reinitialize it and continue.

这篇关于获得一个随机的场地使用ActionScript没有重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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