简体   繁体   中英

Actionscript 3: Trying to create a randomized array

im trying to create a randomized array that will change the position of my pictures(in the tilelist) each time the application is launched. Hope you understand what im looking for, and i dont really understand how to link code correctly here :/

I think its easier simply copying into flash and view from there

thanks :)

Here's my code:

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;

Try this

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

Just instatiate randomizeArray though, and don't fill it with empty Arrays like in ur source!

I recommend creating a single object to hold related data, and using a single Array/Vector of that object type.

To answer your question, randomizing an array is fairly easy to do with array.sort() and a random selection function. This method can also be used to randomize any array. you don't need to splice arrays or iterate either.

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

myArray.sort(sortOnRandom);

Array randomization is something which comes up very frequently in all my projects so I ended up creating a static method in my Array utility class for it.

It uses the Fisher–Yates shuffle which is supposed to be the most unbiased (and efficient?) algorithm for shuffling the content of an array. There could be faster ways of doing it (like using the array.sortOn() method, but I am not sure how unbiased a result they get compared to this one.)

The shuffle method:

/**
     * 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;

    }

Example:

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

myArray           = ArrayUtils.shuffle(myArray);

where ArrayUtils is the name of the Array Utility class I use. You can simply use the function directly if you don't want to use a utility class of course.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM