Results 1 to 3 of 3

Thread: Loading Random Images From A Folder

  1. #1
    Join Date
    Apr 2006
    Posts
    584
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Loading Random Images From A Folder

    Hi All,

    I have a little Flash header and each time it loads I want it to load one of six images randomly, onto the stage as a background... The images will be sitting in a folder in the same directory as the Flash file so they can easily be updated, anyone ever done this? Some opinions would be much appreciated!

  2. #2
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    Put your images in an array. Use loadMovie() to select one from said array.

    I will try to find some exact coding for you to use.
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  3. #3
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    Okay, after some research, I found that using the array can sometimes slow it down a tad. Let's try to avoid that.

    I will continue to do some research, but instead we could just tell Flash to do a random math function and self pic an image from the result.

    In order to do this, you will have to change (maybe) a few things. First, create an emptyMovieClip (if you don't know how, just ask) put it on the stage and give it an instance name we will call ours "empty". Next, you will have to give your images numeric names (1.jpg, 2.jpg etc but NOT 0.jpg)

    Now all you do is create the function to randomize the number like so:

    Code:
    MovieClip.prototype.setRand = function() {
    	largest= 10;//change the number to the total number of images
    	lowest = 1;//this should stay the same
    	rand = ((Math.floor(Math.random()*100)%(largest-lowest+1))+lowest);
    Then we tell the movie to take that number, and apply it to the file name, along with the folder and ".jpg" to grab the image out of the server with:

    Code:
    loadMovie("images/"+rand+".jpg", "empty");
    Then we just call the function when we need it (right away if you want ~~ probably preferred here) :

    Code:
    setRand();
    So all in all it should look like this:

    Code:
    MovieClip.prototype.setRand = function() {
    	largest= 10;
    	lowest = 1;
    	rand = ((Math.floor(Math.random()*100)%(largest-lowest+1))+lowest);
            loadMovie("images/"+rand+".jpg", "empty");
    }
    
    setRand();
    Give that a try and see what you get
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •