Skip to main content
Known Participant
May 8, 2007
Question

help "mr can't program for toffee" with his array

  • May 8, 2007
  • 3 replies
  • 402 views
hey people,

i posted this topic a few days back and had a great response but can't seem to make the code
supplied work...

my current code is

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);


THIS WORKS GREAT EXCEPT FOR THE RANDOM REPEATS which i need to
eliminate

if someone wouldn't mind scripting the whole code for me (Coz i'm thick)
with the assumption that the files i want to shuffle are called

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

help me obi won kenobi, your my only hope....


THIS WAS THE POSTED SOLUTION :
---------------------------------------------------------------------------------------------------------------

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

This topic has been closed for replies.

3 replies

Inspiring
May 9, 2007
PS: Go back to the original shuffle prototype that I gave you in the other thread. It works just fine as it is!

Also the slice() will make the shufle take longer. Not really a problem for a small number of items in the array…
wAySteadAuthor
Known Participant
May 8, 2007

i try this code and it just crashes when i run flash despite no script errors....

any other suggestions ? or can some one supply the entire script as maybe i'm
editing it wrong with my existing script...

the opening of my script beings :

on (rollOver) {
gotoAndPlay(2);
}
on (releaseOutside, rollOut) {
gotoAndPlay(11);
}
on (release) {

THIS IS THE NEW CODE REPLACING MY PREVIOUS RANDOMISER AS SUGGESTED

Array.prototype.shuffle = function() {
var tmpArray=this;
var responseArray=[];
while (tmpArray.length) {
var newElement=tmpArray.splice( Math.floor( Math.random()*tmpArray.length ), 1 );
responseArray.push( newElement );
}
return responseArray;
};


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

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

WHAT AM I DOING WRONG ?

:(
May 8, 2007
well the code you've got there will generate an array, then randomise it fine. and you can prove that by simply tracing out the value of fileNum_array i.e. trace(fileNum_array);

The problem must be in how you are reading the values out and utilising them in your loading of movies. Look for the error you get traced out at runtime when you try and load in external movies , something like "error file 1.swf is not found" and that'l indicate what values are coming through to your "loadMovieN function.

Once you've done that and theres nothing obvious, post the section of code where you are calling
"loadMovieNum(fileNum_array[0] + ".swf", 1);" - that'l probably be the problem

Inspiring
May 8, 2007
i would change the shuffle function. The one provided needs a random number of loops.

The number of loops for this next one is equal to the array length: