Skip to main content
Inspiring
October 12, 2009
Answered

Random image loader help, Please

  • October 12, 2009
  • 2 replies
  • 1185 views

Hi all,
I am looking into using a random image loader, to load external image files using the following code:

pic_arr = ["dhi/1", "dhi/2", "dhi/3", "dhi/4", "dhi/5"];

onLoad = function() {

        ranNum = Math.floor(Math.random()*pic_arr.length);

        randomLoaderMc.loadMovie(pic_arr[ranNum]+".jpg");

};

Does anyone know how I can do this so It will never loads the same image twice in a row?

Many thanks in advance
Rich

This topic has been closed for replies.
Correct answer Ned Murphy

I am not an experienced with actionScript, can't get this to work... not sure how to add the code you suggested!

this is what i tried:

function shuffle(a){

var len = a.length-1;

for (i=len; i>=0; i--) {

  var p = Math.floor(Math.random()*(i+1));

  var t = a;

  a = a

;

  a

= t;

}

return a;

}

pic_arr = shuiffle("dhi/1", "dhi/2", "dhi/3", "dhi/4", "dhi/5", "dhi/6");

myClip = this.createEmptyMovieClip("randomLoaderMc", 100);

myClip._x = 0;

myClip._y = 0;

myClip.loadMovie(pic_arr[shuffle]+".jpg")


// first create the array

var pic_arr = new Array("dhi/1", "dhi/2", "dhi/3", "dhi/4", "dhi/5", "dhi/6");

function shuffle(a){

var len = a.length-1;

for (i=len; i>=0; i--) {

  var p = Math.floor(Math.random()*(i+1));

  var t = a;

  a = a

;

  a

= t;

}

return a;

}

// then randomize what's in it

picArray = shuffle(picArray);

2 replies

Inspiring
October 12, 2009

The image loads on first frame (randomly) then the timeline plays and loops with the movieClip to then action the same code.

Please see FLA attached, movieClip in question is "dynamicImageLoader"

thanks

Rich

Inspiring
October 12, 2009

The code use original wasn't work, so now using this:

pic_arr = ["dhi/1", "dhi/2", "dhi/3", "dhi/4", "dhi/5", "dhi/6"];

ranNum = Math.floor(Math.random()*pic_arr.length);

myClip = this.createEmptyMovieClip("randomLoaderMc", 100);

myClip._x = 0;

myClip._y = 0;

myClip.loadMovie(pic_arr[ranNum]+".jpg")

But still cant figure out how to make to not load the same image twice in a row!

Thanks in advance for any help.

rich

Ned Murphy
Legend
October 12, 2009

You should just shuffle the array elements, then you can loop thru them with a counter.  If you search this forum for the term "shuffle" you should be able to find the shuffle function.

Edit: My own search for the function turned up nothing as code segments are apparently being removed from older forum postings, so here it is...

function shuffle(a){
var len = a.length-1;
for (i=len; i>=0; i--) {
  var p = Math.floor(Math.random()*(i+1));
  var t = a;
  a = a

;
  a

= t;
}
return a;
}

You would just need to pass your array into the function to have it returned in a random order...

pic_arr = shuiffle(pic_arr);

Ned Murphy
Legend
October 12, 2009

In what way do you mean to not load the same image twice in a row?  That code appears to only load one image, so when would the next occurence of a loading take place?