Skip to main content
Inspiring
September 18, 2011
Question

MUST be a better way... !

  • September 18, 2011
  • 1 reply
  • 398 views

Hi all,

I've recently inherited a project which needs to load random images from a dir on the server.

the previous developer has this code on every swf (there are over 40):

- - - - - - - -

pic_arr = ["images/randoms/1", "images/randoms/2","images/randoms/3", "images/randoms/4", "images/randoms/5", "images/randoms/6", "images/randoms/7",

           "images/randoms/8", "images/randoms/9","images/randoms/10","images/randoms/11", "images/randoms/12","images/randoms/13","images/randoms/14",

           "images/randoms/15","images/randoms/16","images/randoms/17","images/randoms/18","images/randoms/19","images/randoms/20","images/randoms/21",

           "images/randoms/22","images/randoms/23","images/randoms/24","images/randoms/25","images/randoms/26","images/randoms/27","images/randoms/28",

           "images/randoms/29", "images/randoms/30","images/randoms/31","images/randoms/32"];

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

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

    unloadMovieNum(1);

- - - - - - - - -

I need to update this as the number of random images is set to increase to about 1000 and I don't fancy hand coding the above to include "images/randoms/999" ,  "images/randoms/1000"  !!!

Can anyone suggest a solution where I can keep a single .as file on the server which handles this and then a line of code in each swf which imports it ?

All and any ideas of finding a better solution MUCH appreciated.

FYI - the site is built in php with a Flash fornt end so easy to use php to count the contents of the "images/randoms" dir and pass that number as a flashVar if that helps.

Best wishes

Tony

;

This topic has been closed for replies.

1 reply

Participant
September 19, 2011

Well, I don't know about importing an .as file or anything,


but experience in other languages can let me help solve your array numbering problem:

This isn't tested code, but should be close to right.

var array_size = 1000;     //or however large it is

var pic_arr; //just in case

pic_arr = new Array(); 

for(var i = 1; i <= array_size; i++)

{

     pic_arr = "images/randoms/" + String(i);  //String(integer) should convert the integer to a string

}

// From here we would use the rest of your original code

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

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

unloadMovieNum(1);

OR we could do even better:

This saves the time and space of creating such a large array!

var array_length = 1000;

ranNum = Math.floor(Math.random()*array_length) + 1;

pic_hold.loadMovie("images/random/"+String(ranNum)+".jpg");

unloadMovieNum(1);

I don't know about the rest of your problems thouhg =( sorry

-Stefan