Skip to main content
Known Participant
August 13, 2013
Answered

Loop Random Frame Labels

  • August 13, 2013
  • 1 reply
  • 2495 views

Hello.  I recently got a code from David Stiller's blog that I really like.  It's about how to get a series of frame labels to play in random order without ever repeating.  That's awesome, and I understand most of the code.

However, when all the frame labels have been 'played', I don't want it to just go and rest/stop on the last frame.  I want it to start the process all over again.  Is there a way to do this?  Here is the code I currently have:

function shuffle(arr:Array):Void {
  var len:Number = arr.length - 1;
  for (var i:Number = len; i >= 0; i--) {
    var p:Number = Math.floor(Math.random() * (i + 1));
    var t:Object = arr;
    arr = arr

;
    arr

= t;
  }
}

var labels:Array = ["a", "b", "c", "d", "e"];
shuffle(labels);

var currentLabel:Number = 0;
function gotoNextLabel():Void {
  if (currentLabel < labels.length) {
    gotoAndPlay(labels[currentLabel]);
    currentLabel++;
  } else {
    stop();
  }
}
gotoNextLabel();

Here is the blog entry I got it from:  http://www.quip.net/blog/2007/flash/how-to-jump-randomly-to-frame-labels-without-repeatshttp://

I'm guessing I have to replace 'stop' toward the end with something else, but what?  Repeat?

This topic has been closed for replies.
Correct answer kglad

use:

function shuffle(arr:Array):Void {
  var len:Number = arr.length - 1;
  for (var i:Number = len; i >= 0; i--) {
    var p:Number = Math.floor(Math.random() * (i + 1));
    var t:Object = arr;
    arr = arr

;
    arr

= t;
  }
}

var labels:Array = ["a", "b", "c", "d", "e"];
shuffle(labels);

var currentLabel:Number = 0;
function gotoNextLabel():Void {
  if (currentLabel < labels.length) {
    gotoAndPlay(labels[currentLabel]);
    currentLabel++;
  } else {
currentLabel = 0;

shuffle(labels);  //<-if you want to re-randomize labels. otherwise, omit this line

gotoNextLabel();

  }
}
gotoNextLabel();

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
August 13, 2013

use:

function shuffle(arr:Array):Void {
  var len:Number = arr.length - 1;
  for (var i:Number = len; i >= 0; i--) {
    var p:Number = Math.floor(Math.random() * (i + 1));
    var t:Object = arr;
    arr = arr

;
    arr

= t;
  }
}

var labels:Array = ["a", "b", "c", "d", "e"];
shuffle(labels);

var currentLabel:Number = 0;
function gotoNextLabel():Void {
  if (currentLabel < labels.length) {
    gotoAndPlay(labels[currentLabel]);
    currentLabel++;
  } else {
currentLabel = 0;

shuffle(labels);  //<-if you want to re-randomize labels. otherwise, omit this line

gotoNextLabel();

  }
}
gotoNextLabel();

CuwenAuthor
Known Participant
August 13, 2013

Yep, I definitely want to re-shuffle.  So, I'll keep that line in.

Thank you so much for your help!  I think it's cool that you're helping me with this b/c in David's blog, he said that the shuffle script came from you.  So thank you twice!

CuwenAuthor
Known Participant
August 13, 2013

One more quick question.  Can I put this into another string of code?  I'm basically creating a video player, and I have it set up currently so that a listener object is created for each video I insert into a frame label.  When the video is done playing, I want it to do that shuffle command.  So, could I put all of the previous code into this:

var listenerObject:Object = new Object();

listenerObject.complete = function(eventObject:Object):Void {

And then, after Void { I'd put in all the previous code.  Would that work?