all groups > flash actionscript > april 2004 >
You're in the

flash actionscript

group:

Playing SCENES in random oder.


Playing SCENES in random oder. wian?
4/30/2004 8:41:18 PM
flash actionscript:
Hello,

I have just started using Flash MX.

I have created 11 scenes with text animation on each.

What i would like to do next is have those scenes play in a random order
every time the file is opened.

Thank you in advance!


Re: Playing SCENES in random oder. rlc5611
5/2/2004 12:33:28 PM
there are two issues:

First you need to generate the random numbers with no repetition. The
following script builds an array with the numbers 1 through 11 in random order.
Run this script once each time you load the movie.

var myscene_array = new Array ();// make a new array
mycurrentscene = 1;
maxscenes = 11;//maximum number of scenes in your case.
//this nex for loop is not really required in MX or below. Not really required
in MX 2004 but I don't like "undefineds"
for (i = 0; i < maxscenes; i++) {//try without this for loop. Should be okay.
myscene_array.push(0);//initialize the array so it is not undefined (MX 2004
only)
}
//
for (i = 0; i < maxscenes; i++) {
unique = false;//set the uniqueness test to false
while(not unique) {//test for uniqueness of new random number against
previous values
number2test = random(maxscenes)+1;//plus 1 is used because "random" is
zero indexed otherwise returns 0-10
for( k = 0; k < maxscenes; k++) {
if( number2test == myscene_array[k]) {
k = maxscenes;//number is not unique so exit this for loop
} else if (k == maxscenes-1) {//made it all the way thru and is
unique so save it into the array
myscene_array[i]=number2test;//store unique random number into
array
unique = true;//exit the while loop
}
}
}
}
for (i = 0; i < maxscenes; i++) {
trace(myscene_array[i]);//see the resulting array of random numbers
}
gotoAndStop("myscene" +myscene_array[0]);//goes to the keyframe identified by
the naming convention below.
//or gotoAndPlay depending on what you want to do.

Now in the second frame of the first scene place a keyframe label called
"myscene1";
Now in the first keyframe of all the remaining scenes place a frame label that
has symmetry (i.e.
"myscene2"
"myscene3"
"myscene4"
etc.

In each scene place the script:

++mycurrentscene;//increment the scene counter

when you are ready to go to the next scene use the script:

gotoAndStop("myscene" +myscene_array[mycurrentscene]);//go to next one stored
in the array

Re: Playing SCENES in random oder. rlc5611
5/2/2004 12:36:03 PM
Re: Playing SCENES in random oder. rlc5611
5/2/2004 12:55:34 PM
sorry I made a typo. I corrected it below. I had written
myscene_array=number2test; and it should be myscene_array[i]=number2test;


var myscene_array = new Array ();// make a new array
mycurrentscene = 1;
maxscenes = 11;//maximum number of scenes in your case.
//this nex for loop is not really required in MX or below. Not really required
in MX 2004 but I don't like "undefineds"
for (i = 0; i < maxscenes; i++) {//try without this for loop. Should be okay.
myscene_array.push(0);//initialize the array so it is not undefined (MX 2004
only)
}
//
for (i = 0; i < maxscenes; i++) {
unique = false;//set the uniqueness test to false
while(not unique) {//test for uniqueness of new random number against previous
values
number2test = random(maxscenes)+1;//plus 1 is used because "random" is zero
indexed otherwise returns 0-10
for( k = 0; k < maxscenes; k++) {
if( number2test == myscene_array[k]) {
k = maxscenes;//number is not unique so exit this for loop
} else if (k == maxscenes-1) {//made it all the way thru and is unique so save
it into the array
myscene_array[i]=number2test;//store unique random number into array
unique = true;//exit the while loop
}
}
}
}
for (i = 0; i < maxscenes; i++) {
trace(myscene_array);//see the resulting array of random numbers
}

Re: Playing SCENES in random oder. kglad
5/2/2004 2:41:08 PM
rlc, creating random numbers with no repeat (or shuffling numbers 1 to 11) can
be done more succinctly. the following is a shuffle method (from peer
brouwers) that can be added to arrays:

Array.prototype.shuffle = function() {
var len = this.length;
var i = len;
while (i--) {
var p = random(len);
var t = this[i];
this[i] = this[p];
this[p] = t;
}
};
myA = new Array();
for (ivar=0; ivar<=10; ivar++) {
myA[ivar] = ivar+1;
}
myA.shuffle();



Re: Playing SCENES in random oder. rlc5611
5/2/2004 3:17:30 PM
Re: Playing SCENES in random oder. kglad
5/2/2004 4:00:52 PM
i like the idea behind peer's coding but i don't really like the way it's
coded. i prefer:

Array.prototype.shuffle = function() {
for (ivar=this.length-1; ivar>=0; ivar--) {
var p = random(ivar);
var t = this[ivar];
this[ivar] = this[p];
this[p] = t;
}
};
AddThis Social Bookmark Button