Skip to main content
Known Participant
May 3, 2007
Question

how do i modify this existing shuffle array / randomiser ?

  • May 3, 2007
  • 17 replies
  • 1418 views
hello !

some one on this forum posted the below code which has been perfect for my requirements but i would
like to stop the randomiser from repeating previously played .swf files...

in laymens terms, ONCE this random file is played, do not play until all other files have been played once....

WHERE DO I START ? (an explanation of what code does what would be appreciated)

var randomMax:Number = 10;
var randomMin:Number = 1;
var randomFileNum:Number = Math.floor(Math.random() * randomMax + randomMin);

trace(randomFileNum); //will display from 1 to 10;

loadMovieNum(randomFileNum + ".swf", 1);


cheers
This topic has been closed for replies.

17 replies

wAySteadAuthor
Known Participant
May 9, 2007
thanks ! i'll give that go

=)
Inspiring
May 9, 2007
Ah, there's your problem.

This is not something that goes on a clip or a button.

Open a brand new document.

Select the first frame.

Press F9 or alt/opt-F9 depending on your version of flash.

Paste the code (Just the part I gave you) in the AS window.

The Array.shuffle() method is now available to shuffle any array you desire.

Just some advice, stop using on(clip Event) style of coding! That is something that came into being for a short while between 2001 and 2002. Since Flash MX it has been surpassed by using timeline code. So let's continue.

Take an instance of your button (which I hope is really a movieclip) and put it on the stage.

Make sure to give it an instance name in the property panel. Something like myButton1 or such.

Find all that code that you put on it and get rid of it.

Now make sure you don't have the button selected, but select the frame and bring up the AS window. If you don't see the array code from before then you have the wrong thing selected.

Add the following below that code.

var count=0;
myButton1.onRollOver=function(){
this.gotoAndPlay(2);
}
myButton1.onRollOut=myButton1.onReleaseOutside=function(){
this.gotoAndPlay(11);
}
myButton1.onRelease=function(){
trace("Array element "+count+" is "+myArray[count++]);
}

I don't really understand what you are trying to do with the on(Release) in your code. So I just have it tracing each element from the array. But you could use that to load or do something else.

The advantage to this style of coding is that all your code is in one place. You don't have to go hunting for it.

The array was already shuffled when you did myArray.shuffle(); So unless you want to shuffle it again when you get to the onRelease you don't need to do that again. It is kind of like a deck of cards. You shuffle it once and then just deal one from the top everytime you need it.

wAySteadAuthor
Known Participant
May 9, 2007
I don't know much about actionscript (That much is obvious)
but i am exporting my movie in ACTIONSCRIPT 2 to support
other code...

this format seems to support my old method (posted above)
but this new array doesn't wont to work for me.

i thought the numeric dynamics for the animated button may have
been confusing the code so i designed a simple button for the
same procedure but still no joy.

u can see the context i am trying to use it in on

BulletProofTV.Com

for example, visit MUSIC, hit BREAKBEAT randomiser..

at present it is the old randomiser coz like i said i cant get it to work
and need some sleep.........

zzzz
wAySteadAuthor
Known Participant
May 9, 2007
sorry people, i cant get this to work...

my entire script assigned is as follows...

the button at present doens't even animate properly with this new code...

my code reads as follows..

on (rollOver) {
gotoAndPlay(2);
}
on (releaseOutside, rollOut) {
gotoAndPlay(11);
}
on (release) {
Array.prototype.shuffle = function() {
for (var ivar = this.length-1; ivar>=0; ivar--) {
var p = random(ivar+1);
var t = this[ivar];
this[ivar] = this

;
this

= t;
}
};
//Hide the shuffle prototype from the tyranny of the for-in loop
ASSetPropFlags(Array.prototype,["shuffle"],1,1);

var myArray:Array=new Array(396,397,398,399,400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416);
myArray.shuffle();
trace(myArray);

wAySteadAuthor
Known Participant
May 8, 2007

I dont concur

:(

it doesn't work...

i've tried the code and it just gets a THIS SCRIPT IS SLOWING DOWN FLASH
message and then crashes.....

would someone mind supplying / editing the script for my requirements ??

i'm trying to get the shuffle to go through the following files...

301.swf, 302.swf, 303.swf, 304.swf, 305.swf, 306.swf and 307.swf


pretty please with sugar on top !

Inspiring
May 8, 2007
How, exactly, are you using it? I've used this for years and never had any trouble at all.
wAySteadAuthor
Known Participant
May 5, 2007
thank you doctors for your concuring assements for
my current dilema,

i'll be back to concur or maybe i will beg to differ

May 3, 2007
easiest just to make an array of the numbers you need and shuffle it.

//And ths code to add the shuffle method to the array class

Array.prototype.shuffle = function() {
var i:Number;
this.sort(function(a,b) { i = Math.round((Math.random() * 100) - 50); return i;});
return this;
};


var fileNum_array = [];//declare empty array
for( var i=randomMin;i<=randomMax;i++)
{
fileNum_array.push(i);
}
fileNum_array.shuffle();

then pull the values out of the array incrementally


loadMovieNum(fileNum_array[0] + ".swf", 1);
loadMovieNum(fileNum_array[1] + ".swf", 1);
loadMovieNum(fileNum_array[2] + ".swf", 1);
etc...

or use an incrementing variable everytime you need to return a value

var myIncVar = 0;

loadMovieNum(fileNum_array[myIncVar++] + ".swf", 1);
loadMovieNum(fileNum_array[myIncVar++] + ".swf", 1);
etc...

Hope that helped, Ade
Inspiring
May 3, 2007
I concur with dr_ross' approach, but offer a slightly different version of the shuffle method. This is something that we have discussed in some rather long threads in the past. In general the one that provides the best stability and speed is below.

The random() is gone from AS3, but then again a lot of things have changed there and none of this will work. Except the algorithm – step backward through the array, at each spot choose a random element from the spot or below, swap them, step back to the previous element in the array…

I've also added the ASSetPropFlag to hide the new method. If you don't know what it does, create a small array and then try the following code both with and without that line.

for(var a in myArray){
trace(a+" is "+myArray[ a ]);
}

Replace myArray with the name of your Array.