Skip to main content
April 1, 2008
Answered

Random Load Frames - Please Help!!!

  • April 1, 2008
  • 8 replies
  • 699 views
I need help with Actionscript 3.0. I want to have my flash file jump to a random frame number in my array. But when i test the movie, I get 12 error messages. Here is the code I have on the first frame:

var frameStarters:Array = new Array(2,22,42)
var fs:Number = Math.round((Math.random() * (frameStarters.length-1)));
gotoAndStop(frameStarters[fs]);
stop();

Please help!!!
This topic has been closed for replies.
Correct answer
Fixed. Thanks kglad!

What was happening was I didn't have a preloader. So everytime it was randomly going to a frame in my array, it would run out of time before it got to the third frame in my array. So the code above works for "Random loading into frames", but you also need a preloader on frame 1.

8 replies

kglad
Community Expert
Community Expert
April 3, 2008
you're welcome.
Correct answer
April 2, 2008
Fixed. Thanks kglad!

What was happening was I didn't have a preloader. So everytime it was randomly going to a frame in my array, it would run out of time before it got to the third frame in my array. So the code above works for "Random loading into frames", but you also need a preloader on frame 1.
kglad
Community Expert
Community Expert
April 2, 2008
oops. remove that -1 multiplying Math.random().
April 2, 2008
The random works now, but it only goes to frames 2 and 22. For some reason it will not load into 42. I have tried to limit the array to 1, 42 and 22, 42 but it only goes to the other number and not 42. There is no code on frame 42, just the start of a tween. I even changed out (frameStarters.length-1) to "-2", "+1" and even took off "-1" , thinking this is causing the array to only show 2 numbers. Any thoughts?
kglad
Community Expert
Community Expert
April 1, 2008
then you have some other code causing that problem.
April 1, 2008
Thanks kglad.

But I am not getting the frames to load randomly. Everytime I hit "reload" in the browser, it always ends on frame 2.

Here is the code:
var frameStarters:Array = new Array(2,22,42)
var fs:Number = Math.floor(Math.random() * (frameStarters.length-1));
gotoAndPlay(frameStarters[fs]);

kglad
Community Expert
Community Expert
April 1, 2008
p.s. there's an extra set of parenthesis in your 2nd line.
kglad
Community Expert
Community Expert
April 1, 2008
use Math.floor() instead of Math.round().