How to Jump to a Random Frame Label

ActionScript 2.0 Flash

This question comes up from time to time in the forums.  Typically, someone has built a Flash banner ad with a handful of entry points.  The ad is supposed to start at any one of these, but the choice should be random.  For example, a fruit stand ad is supposed to play the bit about the bananas first, then the apples, then the bananas again, then the oranges.  How can this be accomplished?  Well, this one’s pretty easy. 

An answer, short and sweet.

Add the following to frame 1 of your scripts layer.

function getRandomLabel():String {
  var labels:Array = new Array("a", "b", "c");
  var index:Number = Math.floor(Math.random() * labels.length);
  return labels[index];
}
this.gotoAndStop(getRandomLabel());

Make sure to replace the “a”, “b”, and “c” with the names of actual frame labels in your movie.  (It doesn’t matter how many, but it won’t make much sense if you only put one.)

How it works

The first line declares a function that returns a string, which will provide the desired random frame label when needed.

Inside the function, we declare an array variable and assign it the value of an Array instance.  This array holds a list of our frame labels.  Next, we declare a number variable and assign it a random value based on the count of elements in the array.  The Array.length property returns this count.  The count is multiplied by Math.random(), a static method that returns a pseudo-random decimal value between zero and one.  Because the result needs to be an integer, we round the whole thing down with the Math.floor() method.  When this line is executed, index’s value will be an integer between zero and the number of items in the array.  Finally, we use the array access operator, [], to “pull” an element from the array.  In this case, labels[0] would be “a,” labels[1] would be “b,” and so on.  We’re simply randomizing the number that goes in between the square brackets.

Outside the function, a normal invocation of MovieClip.gotoAndStop() does its thing with a frame label as the parameter.  Once the custom getRandomLabel() function is declared, it can be referenced anywhere else in the timeline, so if you want to jump to a random label again after, say, frame 300, just paste that last part into a keyframe at that point.

this.gotoAndStop(getRandomLabel());

Variations

As is, the movie will immediately jump to one of your frame labels and stop — that’ll be the end of it.  If you want the playhead to jump to a frame and play, replace the reference to this.gotoAndStop() with this.gotoAndPlay(), leaving that getRandomLabel() function as a parameter, where it is.

To jump to a random frame (not frame label), the following variation will do.

function getRandomFrame():Number {
  return Math.ceil(Math.random() * (this._totalframes - 1)) + 1;
}
trace(getRandomFrame());

Note the use of MovieClip._totalframes to retrieve the total number of frames in the timeline. Hardcode this number, if you like, but make sure to exclude 1 from the possible outcome. If the playhead goes to frame 1, the getRandomFrame() function is needlessly redefined.

How about visiting a series of random frame labels without repeats? For that, check out the aptly titled “How to Jump Randomly to Frame Labels without Repeats.”

Leave a Reply