0

我试图创建一个随机数组,每次启动应用程序时都会改变我的图片的位置(在 tilelist 中)。希望你明白我在找什么,我真的不明白如何在这里正确链接代码:/

我认为它更容易简单地复制到闪存中并从那里查看

谢谢 :)

这是我的代码:

flash.events.MouseEvent;

btn_back.addEventListener(MouseEvent.CLICK, ftilbake);


function ftilbake(evt:MouseEvent)
{
    gotoAndStop(1);
}


var heroArray:Array = new Array();
var randomizeArray:Array = new Array();

createArrays()

function createArrays()
{
heroArray[0] = new Array("Rumble","Garen","Lulu","Corki","Warwick");

heroArray[1] = new Array("Bilder/Champions/Rumble.jpg","Bilder/Champions/Garen.jpg","Bilder/Champions/Lulu.jpg","Bilder/Champions/Corki.jpg","Bilder/Champions/Warwick.jpg");

heroArray[2] = new Array("Bilder/Champions/Rumble1.jpg","Bilder/Champions/Garen1.jpg","Bilder/Champions/Lulu1.jpg","Bilder/Champions/Corki1.jpg","Bilder/Champions/Warwick1.jpg");

heroArray[3] = new Array("the Mechanized Menace","the Might of Demacia","the Fae Sorceress","the Daring Bombardier","the Blood Hunter");
heroArray[4] = new Array(0,0,0,0,0);
heroArray[5] = new Array("Rumble.wav","Garen.wav","Lulu.wav","Corki.wav","Warwick.wav");
randomizeArray[0] = new Array();
randomizeArray[1] = new Array();
randomizeArray[2] = new Array();
randomizeArray[3] = new Array();
randomizeArray[4] = new Array();

//randomizing the positions in the array(?)
var randomPos:int = 0;
for (var i:int = 0; i < heroArray.length; i++)
{
    randomPos = int(Math.random() * heroArray[0].length);
    while (randomizeArray[randomPos][0] != null)
    {
        randomPos = int(Math.random() * heroArray.length);  
    }
}
}


var totalKlikk:int = 0;


for (var teller1:int = 0; teller1 <heroArray[0].length; teller1++)
{
    leagueChamps.addItem({label:heroArray[0][teller1], source:heroArray[1][teller1]});
}

leagueChamps.columnWidth = 80;

leagueChamps.rowHeight = 80;

leagueChamps.columnCount = 5;

leagueChamps.rowCount = 1;

leagueChamps.direction = "horizontal";

leagueChamps.addEventListener(MouseEvent.CLICK, bildeKlikk);


function bildeKlikk(evt:MouseEvent)
{
    var element:Object = leagueChamps.selectedItem;
    var fil:String = element.source;
    txtChHero.visible = false;
    totalKlikk++;
    if (totalKlikk <11)
    {
        for (teller1 = 0; teller1 <heroArray[0].length; teller1++)
        {
            if (heroArray[1][teller1] == fil)
            {
            heroArray[4][teller1]++;
            if (heroArray[4][teller1] == 1)
            {
                txtBox1.visible = true;
                txtBox2.visible = true;
                leagueShow.source = heroArray[2][teller1];
                txtBox1.text = heroArray[0][teller1];
                txtBox2.text = heroArray[3][teller1];
            }
            if (heroArray[4][teller1] == 2)
            {
                txtBox1.visible = true;
                txtBox2.visible = true;
                leagueShow.source = heroArray[2][teller1];
                txtBox1.text = heroArray[0][teller1];
                txtBox2.text = heroArray[3][teller1];
                heroArray[5][teller1].play();
            }
            if (heroArray[4][teller1] == 3)
            {
                bildeKlikk3();
            }
        }
    }
}
else
{
    txtChHero.visible = true;
    txtChHero.text = "Du har klikket følgende mange ganger på de forskjellige bildene:";
    txtH1.text = heroArray[4][0]
    txtH2.text = heroArray[4][1]
    txtH3.text = heroArray[4][2]
    txtH4.text = heroArray[4][3]
    txtH5.text = heroArray[4][4]
    txtBox1.visible = false;
    txtBox2.visible = false;
    leagueShow.visible = false;
}
}


function bildeKlikk3()
{
    txtBox1.visible = true;
    txtBox2.visible = true;
    leagueShow.source = heroArray[2][teller1];
    txtBox2.text = "Ikke mer informasjon";
}


txtBox2.visible = false;

txtBox1.visible = false;
4

3 回答 3

0

试试这个

while (heroArray.length > 0) {
  randomizeArray.push(heroArray.splice(Math.round(Math.random() * (heroArray.length - 1)), 1)[0]);
}

不过,只需实例化 randomizeArray,不要像您的源代码中那样用空数组填充它!

于 2013-03-14T00:39:53.627 回答
0

我建议创建一个对象来保存相关数据,并使用该对象类型的单个数组/向量。

要回答您的问题,使用 array.sort() 和随机选择函数对数组进行随机化相当容易。此方法还可用于随机化任何数组。您也不需要拼接数组或迭代。

function sortOnRandom(a:Object, b:Object):Number{
  if(Math.random() > 0.5){
    return 1;
  }else{
    return -1;
  }
}

myArray.sort(sortOnRandom);
于 2013-03-14T03:21:45.263 回答
0

数组随机化在我的所有项目中都非常频繁地出现,所以我最终在我的 Array 实用程序类中为它创建了一个静态方法。

它使用Fisher-Yates shuffle,它被认为是最无偏(和最有效?)的算法,用于对数组的内容进行混洗。可能有更快的方法来做到这一点(比如使用 array.sortOn() 方法,但我不确定他们得到的结果与这个相比有多公正。)

洗牌方法:

/**
     * shuffle the given array
     * @param   array
     * @return
     */
    public static function shuffle(array:Array):Array 
    {

        var index   :int;
        var item    :*;
        var limit   :int    = array.length as int;

        for (var i:int = limit-1; i >= 0 ; --i) 
        {
            index           = Math.floor(Math.random() * (i + 1));
            item            = array[index];
            array[index]    = array[i];
            array[i]        = item;
        }

        return array;

    }

例子:

var myArray:Array = new Array("Red","Orange","Yellow","Green","Blue");

myArray           = ArrayUtils.shuffle(myArray);

其中 ArrayUtils 是我使用的 Array Utility 类的名称。如果您不想使用实用程序类,您可以直接使用该函数。

于 2013-03-14T09:53:16.320 回答