Cyril_Hyori
Cyril_Hyori

Reputation: 19

Randomize Saved movieclips stored on children

I'm having a hard time with some code in actionscript 3.0. I don'y know how to randomize the movieclips stored on child and pick only 8 movieclips wherein there are 10 movieclips stored. I hope you'll be able to help me with this problem. thanks

Here is the code:

    //start stage function
    this.mainmc.addEventListener (Event.ENTER_FRAME, setupStage1);
    this.waitingCounter=0;

//set up current stage
function setupStage1 (e:Event) {
    //wait for timeline
    if (this.waitingCounter<2) {
        this.waitingCounter++;
        //not ready yet, do nothing
        return;
    }
    //Start the timer
    timer.start();
    //hide hint
    this.mainmc.hintmc.visible=false;
    //hide star animation
    this.mainmc.starAnimation.visible=false;
    //listener for hint button
    this.mainmc.hintbut.addEventListener (MouseEvent.CLICK, showHint1);
    //create objects array
    this.obArr=[];
    //count the objects on stage
    for (var n=0; n<this.mainmc.numChildren; n++) {
        //get the children
        var ob=this.mainmc.getChildAt(n);
        //only take movie clips
        if (ob is MovieClip) {
            //only count the movie clips that have name declared
            if (ob.myname!=null) {
                //push to array
                this.obArr.push (MovieClip(ob));
            }
        }
    }

on the code above, the code will store all the movieclips that are present in the stage. it stores them in a child. each 10 movieclips has a variable name "myname".

Upvotes: 1

Views: 160

Answers (1)

user562566
user562566

Reputation:

If you simply want to randomly sort items within an array, use the array.sort method and within your sort function, simply create a random number between 1 and 2. If it's 1, return true, if it's 2, return false. Here is an actionscript 2 snippet along with a link to a couple of tutorials:

var a:Array = new Array(“a”, “b”, “c”, “d”, “e”);
function shuffle(a,b):Number {
var num : Number = Math.round(Math.random()*2)-1;
return num;
}
var b:Array = a.sort(shuffle);
trace(b);

http://mrsteel.wordpress.com/2007/05/26/random-array-in-as2-as3-example-using-sort/

http://sierakowski.eu/list-of-tips/75-random-sort-function-comes-handy-when-building-applications-with-playlists.html

This is a much longer and more in-depth tutorial:
http://active.tutsplus.com/tutorials/actionscript/quick-tip-how-to-randomly-shuffle-an-array-in-as3/

Upvotes: 1

Related Questions