Copy link to clipboard
Copied
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?
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 r
Copy link to clipboard
Copied
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();
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
if you use an flvplayback component, you can use a "complete" listener:
var lo:Object={};
lo.complete=gotoNextLabel;
flv_pb.addEventListener("complete",lo);
p.s. that shuffle function is an implementation of the Fisher-Yates algorithm from 1938.
Copy link to clipboard
Copied
Ok. Thank you very much.
While I was waiting for a response, I did some more googling and tried some things out myself. I got the player working, but it still is repeating some videos. I have 12 videos that I need it to play, and before it plays all 12 videos in random order, it still plays some 3 or 4 times. Do you know why this might be? Should I start another thread about this specific issue?
Copy link to clipboard
Copied
you can simplify things (and eliminate errors) by using one flvplayback (eg, flv_pb) on 1 keyframe to play all 12 videos:
var lo:Object={};
lo.complete=playNextF;
flv_pb.addEventListener("complete",lo);
var videos:Array = ["vid1.flv", "vid2.flv", etc];
shuffle(videos);
var index:Number = 0;
function playNextF():Void {
if (index < videos.length) {
flv_pb.contentPath = videos[index];
index++;
} else {
index = 0;
shuffle(videos);
playNextF();
}
}
playNextF();
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;
}
}
Copy link to clipboard
Copied
I would do that, but I need to use buttons in this player, too. The 'invisible' buttons are above a thumbnail image, and when a person clicks on that image, it takes them to that "video", although it's really just taking them to the set of frames that I assigned the video to.
Copy link to clipboard
Copied
have the button handlers assign the appropriate index and call playNextF().
Copy link to clipboard
Copied
Wow. That might be a little too advanced for me. I really appreciate your help, though! If only the code I used from earlier with your code didn't play some of the same videos several times, it'd be perfect! I don't know if the code won't work, won't do random frame labels without going back to some a few times, b/c I'm using flvs or not.
Copy link to clipboard
Copied
there's not much to that.
if button0 plays the first video in that array and button1 plays the next etc, use:
for(var i:Number=0;i<videos.length;i++){
this["button"+i].onRelease=function(){
index=Number(this._name.substring(6));
playNextF();
}
}
Copy link to clipboard
Copied
Does it matter that my buttons are nested in a movie clip? I had to do that b/c they're in a scrollpane. And for the number '6' in substring, do I replace the 6 ith how many buttons I have (12)?
Copy link to clipboard
Copied
how do you reference your buttons?
Copy link to clipboard
Copied
Okay, I know this is really bad practice, and I usually try to avoid it, but I put the code right on the buttons. That way, I don't have to reference the button in my main code. I googled for hours trying to find out how I could 'call to' the buttons through the mc in my main script, but nothing worked for me. I tried multiple _root codes and other codes, nothing worked.
Copy link to clipboard
Copied
Oh, and I'm trying to figure out how to do a .flv array. I thought it'd be just like a swf array, but I was very wrong. I've done plenty of swf arrays before, but have never done an flv array. I can load an external flv into a video object just fine, but when it comes to setting up an array, I can't quite figure it out.
Copy link to clipboard
Copied
you should remove your buttons from the scrollpane unless there's no other way to display all your buttons. that's going to make your coding more difficult.
click on a button to select it. in the properties panel assing an instance name (eg, button0). do the same for each button, using different names for each.
if you choose names wisely they'll be related to the position in the videos array (shown in message 5) corresponding to the video that should play when that button is clicked.
message 5 shows how to assign your flvs to an array.
how you reference your buttons depends on whether you can get them out of that scrollpane.
Copy link to clipboard
Copied
Thank you so much for all of your help. I'm going to save this discussion and reference back to it when I have more time to try this project from scratch and use your ideas. If I get everything to work, I'll let you know! It's always good to learn about this type of stuff, and I like becoming a better programmer. I just usually have to come back to old discussions and work on the new codes and practices when I have time. For now, I'll mark your original answer as correct. Thank you so much again!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now