Randomize and loop multiple images

  • genius
  • Newbie
  • Newbie
  • User avatar
  • Posts: 12
  • Loc: San Antonio, TX

Post 3+ Months Ago

Hey all, I'm pretty new to flash (I think that is the standard line for newbies) and I need
some help/advice/cod on how to make a random loop. Here's the specifics:

I want to create a flash file that displays 50 different images - 1 at a time for 5 seconds
each. And I want each image to be selected at random (so the images will not always be in
the same order). I would like this to loop infinitly.

I figure that shouldn't be too hard to do but I am not having any luck with the math.random
function so hopefully some of you can help me out.

Also, it would be nice if an image is removed from the "pool" after it is displayed so the
same image doesn't pop up multiple times during the loop, and then all images should be added
back into the "pool" after they have all been displayed.

Thanks in advance[/flash]
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

This might help,

Code: [ Select ]
Array.prototype.skuffle = function(){
    var toReturn = [];
    for(var i=0; i<this.length; i++){
        Math.round(Math.random()) > 0 ? toReturn.push(this[i]) : toReturn.unshift(this[i]);
    }
    return toReturn;
}
function getNext(){
    side.length > 0 ? null : side = main.skuffle();
    var toReturn = side.shift();
    return toReturn;
}
main = ["one.jpg", "two.jpg", "three.jpg", "four.jpg"];
side = [];
  1. Array.prototype.skuffle = function(){
  2.     var toReturn = [];
  3.     for(var i=0; i<this.length; i++){
  4.         Math.round(Math.random()) > 0 ? toReturn.push(this[i]) : toReturn.unshift(this[i]);
  5.     }
  6.     return toReturn;
  7. }
  8. function getNext(){
  9.     side.length > 0 ? null : side = main.skuffle();
  10.     var toReturn = side.shift();
  11.     return toReturn;
  12. }
  13. main = ["one.jpg", "two.jpg", "three.jpg", "four.jpg"];
  14. side = [];

The Array.prototype.skuffle takes an array & skuffles (shuffles) it.
main = []; will hold a list of images/movieclips however your doing it, it doesn't matter what order they are in because the side array is where the selection will come from.
The side array doesn't need anything other than to be there, the getNext function will fill it with a random order of the main array if it is empty, the function will always shift(remove) the first item from the side array & return it.

Usage could go somthing like this,
Code: [ Select ]
function nextImage(delay){
  clearInterval(nextInterval);
  container.onLoad = function(){
   nextInterval = setInterval(nextImage, delay);
  }
  container.loadMovie(getNext());
}

stopButton.onPress = function(){
  clearInterval(nextInterval);
}

startButton.onPress = function(){
  nextInterval = setInterval(nextImage, 5000);
}
  1. function nextImage(delay){
  2.   clearInterval(nextInterval);
  3.   container.onLoad = function(){
  4.    nextInterval = setInterval(nextImage, delay);
  5.   }
  6.   container.loadMovie(getNext());
  7. }
  8. stopButton.onPress = function(){
  9.   clearInterval(nextInterval);
  10. }
  11. startButton.onPress = function(){
  12.   nextInterval = setInterval(nextImage, 5000);
  13. }
  • genius
  • Newbie
  • Newbie
  • User avatar
  • Posts: 12
  • Loc: San Antonio, TX

Post 3+ Months Ago

Wow, thanks for the code. But I'm afraid you have confused me a bit. How do I make the code you gave me work? Do I just paste the code into the actionscript window of a blank frame or do I need to do something more? Sorry for asking you to hold my hand on this, I'm just trying to figure it out one step at a time. :roll:
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Here's a Basic Example Fla
  • genius
  • Newbie
  • Newbie
  • User avatar
  • Posts: 12
  • Loc: San Antonio, TX

Post 3+ Months Ago

Dude, you are the man! It all makes sense now! If your ever in San Antonio let me know, I'll buy you lunch or something. :D
  • scubabloke
  • Born
  • Born
  • scubabloke
  • Posts: 2

Post 3+ Months Ago

Ahhhhhhh you guys are lifesavers!! I've been puzzling this one out, hacking up examples, and generally making a mess of it for a week now.

I'm far from a coding genius when it comes to this stuff so I'm hoping I can milk you for one more little bit of functionality....

Is it possible to add a fade effect to the example so the images don't just jump out?

Funny how I can code the database side of a website without too many problems but can't create a simple flash movie!!
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

With fade, (this code is the only thing that changed in the example FLA)
Code: [ Select ]
/*define two arrays, fill the "main" array with the instance names of the moieclips on the stage.*/
main = [p1, p2, p3, p4];
side = [];
//define space to keep track of the last visible item.
nowVisible = {};
//make every mc in the array invisible
for(var i=0; i<main.length; i++){
    main[i]._alpha=0;
}
//define a shuffle prototype.
Array.prototype.skuffle = function(){
  var toReturn = [];
  for(var i=0; i<this.length; i++){
   Math.round(Math.random()) > 0 ? toReturn.push(this[i]) : toReturn.unshift(this[i]);
  }
  return toReturn;
}
/*Alpha fade prototype*/
MovieClip.prototype.fadeTo = function(value, speed) {
  this.onEnterFrame = function() {
   this.aV = Math.floor(value-this._alpha);
   this.aV ? this._alpha += this.aV/speed : (this._alpha=value, delete this.onEnterFrame);
  }
}
/*define a function to pick the next item from the array & return it,
filling the side array back up with a different order when it gets emptied.*/
function getNext(){
  side.length > 0 ? null : side = main.skuffle();
  var toReturn = side.shift();
  return toReturn;
}
/*define an interval function*/
function nextImage(delay){
  nowVisible.fadeTo(0,10);
  nowVisible = getNext();
  nowVisible.fadeTo(100,10);
}
//start it up.
nextInterval = setInterval(nextImage, 5000);
  1. /*define two arrays, fill the "main" array with the instance names of the moieclips on the stage.*/
  2. main = [p1, p2, p3, p4];
  3. side = [];
  4. //define space to keep track of the last visible item.
  5. nowVisible = {};
  6. //make every mc in the array invisible
  7. for(var i=0; i<main.length; i++){
  8.     main[i]._alpha=0;
  9. }
  10. //define a shuffle prototype.
  11. Array.prototype.skuffle = function(){
  12.   var toReturn = [];
  13.   for(var i=0; i<this.length; i++){
  14.    Math.round(Math.random()) > 0 ? toReturn.push(this[i]) : toReturn.unshift(this[i]);
  15.   }
  16.   return toReturn;
  17. }
  18. /*Alpha fade prototype*/
  19. MovieClip.prototype.fadeTo = function(value, speed) {
  20.   this.onEnterFrame = function() {
  21.    this.aV = Math.floor(value-this._alpha);
  22.    this.aV ? this._alpha += this.aV/speed : (this._alpha=value, delete this.onEnterFrame);
  23.   }
  24. }
  25. /*define a function to pick the next item from the array & return it,
  26. filling the side array back up with a different order when it gets emptied.*/
  27. function getNext(){
  28.   side.length > 0 ? null : side = main.skuffle();
  29.   var toReturn = side.shift();
  30.   return toReturn;
  31. }
  32. /*define an interval function*/
  33. function nextImage(delay){
  34.   nowVisible.fadeTo(0,10);
  35.   nowVisible = getNext();
  36.   nowVisible.fadeTo(100,10);
  37. }
  38. //start it up.
  39. nextInterval = setInterval(nextImage, 5000);


Addition of fade prototype,
Code: [ Select ]
/*Alpha fade prototype*/
MovieClip.prototype.fadeTo = function(value, speed) {
  this.onEnterFrame = function() {
   this.aV = Math.floor(value-this._alpha);
   this.aV ? this._alpha += this.aV/speed : (this._alpha=value, delete this.onEnterFrame);
  }
}
  1. /*Alpha fade prototype*/
  2. MovieClip.prototype.fadeTo = function(value, speed) {
  3.   this.onEnterFrame = function() {
  4.    this.aV = Math.floor(value-this._alpha);
  5.    this.aV ? this._alpha += this.aV/speed : (this._alpha=value, delete this.onEnterFrame);
  6.   }
  7. }

Where tweaking of the fade would happen,
Code: [ Select ]
function nextImage(delay){
  nowVisible.fadeTo(0,10);
  nowVisible = getNext();
  nowVisible.fadeTo(100,10);
}
  1. function nextImage(delay){
  2.   nowVisible.fadeTo(0,10);
  3.   nowVisible = getNext();
  4.   nowVisible.fadeTo(100,10);
  5. }

The lower the second params number in the fadeTo(#, #) is the faster he transition. The first params are the alpha & need not be changed.
  • scubabloke
  • Born
  • Born
  • scubabloke
  • Posts: 2

Post 3+ Months Ago

Thanks!!!

Pure genius... really appreciate your help on this one!
  • wiggles
  • Novice
  • Novice
  • wiggles
  • Posts: 15

Post 3+ Months Ago

is there anyway someone can post a working version of the code you guys are working with fade in all. ive just been butchering this for longer then i care to admit.
-jason
  • wiggles
  • Novice
  • Novice
  • wiggles
  • Posts: 15

Post 3+ Months Ago

disregard my last post. im a moron and didnt realize you basically did that. i agree that you guys rock however.
-jason

Post Information

  • Total Posts in this topic: 10 posts
  • Users browsing this forum: No registered users and 35 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.