0

こんにちは、スタック オーバーフローのユーザーです。現在、as3 と xml を介してデータをランダム化するのに苦労しています。

xml を読み込んでランダムな会場を生成することはできますが、作成したランダム ボタンをクリックすると、同じノードが 2 回表示されます。基本的には、前の会場の繰り返しがないデータをランダムに選択したいだけです。

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

私の現在のコード:

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 に答える