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

flash actionscript

group:

Random function


Random function Nupz
1/31/2004 6:13:14 PM
flash actionscript:
Hello all,

Wondering if you can help me,

I need the playhead to jump randomly to 1 of 3 frames after the preloader has finished.

Can anyone help?

Re: Random function eonyron
2/1/2004 8:25:06 AM
I have wrestled with the Math.random() fuction before about true randomness and most people I have talked to swear by it in flash that it is truely random. try something like this:

if(getBytesLoaded() == getBytesTotal()){
x = Math.ceil((Math.random()*3)+1);
gotoAndStop(x);
}

Re: Random function Nupz
2/1/2004 9:39:23 AM
Definately on the right track.

The only thing is, that i need to define the 3 random frames as they are on frames 5, 15, 20.

Any ideas?

Re: Random function Karim N
2/1/2004 6:22:49 PM
well, not elegant, but a switch/case clause would do it:

after the previous code:

switch (x) {
case 1:
gotoAndStop(5);
break;
case 2:
gotoAndStop(15);
break;
case 3:
gotoAndStop(20);
break;
default:
gotoAndStop(1);
}


Re: Random function Skim Milk
2/1/2004 8:22:14 PM
create an array with your 3 frame numbers in it, and then use the math.random function to find a number between 0 and 2 (since array indexing starts at 0). Eonyron's code will work nicely here:
frameArray = new Array(5,15,25);

if(getBytesLoaded() == getBytesTotal()){
x = Math.ceil(Math.random()*2);
gotoAndStop(frameArray[x]);
}

*Drink More Milk*
Skim Milk
Homepage
*Feedback Welcome*

Re: Random function Karim N
2/2/2004 2:43:28 PM
Duh. Yeah, that's the better way to do it.

Re: Random function Nupz
2/2/2004 8:42:05 PM
Loverly, cheers guys, i knew i could count on ya!

AddThis Social Bookmark Button