A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: [RESOLVED] Random Image/Movieclip on refresh?

  1. #1
    Lissnoms
    Join Date
    Aug 2010
    Posts
    3

    resolved [RESOLVED] Random Image/Movieclip on refresh?

    Hey there.

    So, I'm a student, still learning Flash.. I would call myself a Flash newbie still.

    I'm currently using Flash CS4, and Actionscript 2.0 to make myself a website for my artwork. My entire website is being created in Flash (not using Dreamweaver, so keep that in mind.)

    My site is constructed so that Frame 1 is my Home page, Frame 2 is another page on the site, etc.

    On my Home page, I want a random image (of my artwork) to show up in the body of the site (there is an interface surrounding it), every time a viewer visits/refreshes the page. It sounds simple, but I've been struggling with this a lot because I keep coming across really complicated Actionscript.

    I know there are two possible ways to do this. 1) The images can be drawn from the same folder as my .fla file is saved in...OR...I can attach Movieclips (of my artwork) from my Library, to a blank target Movieclip where they will load on the page. I'm willing to do whatever works, basically..haha.

    Here's an outline of the functions I want:
    -Random selection of image/movieclip
    -target destination (where imagery can load)
    -(I'll have a total of about 3 images)

    If you think you can help me with this, I would appreciate it SOOO much. And, like I said, I'm a newb at this.. Please be as step-by-step as you can be, while explaining this. I just want to make sure I'm not missing a simple step..

    Thank you for any help you can offer.

  2. #2
    var x:Number = 1; x /= 0;
    Join Date
    Dec 2004
    Posts
    549
    just have an array of all the sources of the images

    imgArray=["img1.jpg","img2.jpg","img3.jpg"];

    then tell it to load one of those

    curImg=imgArray[random(imgArr.length)];
    image_mc.loadMovie(curImg);
    Z¡µµ¥ D££

    Soup In A Box

  3. #3
    Developing For Dunkets mneil's Avatar
    Join Date
    Mar 2007
    Location
    Lincoln City
    Posts
    2,156
    what you need to do is pick a bunch of images you want to randomly select and then store their paths in an array like so:

    PHP Code:
    var imgs:Array = ["path/to/image.jpg""path/to/image2.jpg""path/to/image3.jpg"]; 
    So that's your array of images. If your swf is in a folder called www. Then your images would be placed in www/path/to/

    That's your folder structure just in case that didn't make sense.

    Then, you'll want to randomly select one of those images:

    PHP Code:
    var imgs:Array = ["path/to/image.jpg""path/to/image2.jpg""path/to/image3.jpg"];
    var 
    img:String imgs[Math.floor(Math.random()*imgs.length)]; 
    now the variable img is storing a randomly selected path from the imgs array. What we're doing is grabbing a random number between 0 and the length of the array and then rounding it down to the nearest whole number.

    Next, you'll want to actually load that image into a movieclip.

    PHP Code:
    var mcLoad:MovieClipLoader = new MovieClipLoader();
    var 
    mcList:Object = {};
    mcLoad.addListener(mcList);
    mcList.onLoadInit ImageLoaded
    This is your loader. It's an instance of a moviecliploader object with a listener assigned to it and an onLoadInit function that will fire when the image is fully loaded. Now we need a movieclip to load the image into and to load it. We'll also want to create the onLoadInit function ImageLoaded and set the depth of the image once it's loaded to be behind everything.

    All Together:

    PHP Code:
    var imgs:Array = ["path/to/image.jpg""path/to/image2.jpg""path/to/image3.jpg"];
    var 
    img:String imgs[Math.floor(Math.random()*imgs.length)];

    var 
    mcLoad:MovieClipLoader = new MovieClipLoader();
    var 
    mcList:Object = {};
    mcLoad.addListener(mcList);
    mcList.onLoadInit ImageLoaded;

    var 
    mc:MovieClip = new MovieClip();
    attachMovie(m"theName", -this.getNextHighestDepth());

    mcLoad.loadClip(imgmc);

    function 
    ImageLoaded(){
       
    trace("your image is loaded");

    so the last part that we just added was the creation of a movieclip, placing it on stage, giving it an arbitrary name, and setting it at the opposite of the highest depth. We also called the loadClip function through the moviecliploader and loaded the string path stored in the variable img and we loaded into our movieclip stored in the variable mc.

    Complicated if you don't understand it; super simple if you do.

    I didn't test this so there may be an error; let me know.

    a couple things can go wrong with this:
    1) make sure the path is correct to the image from the swf's location
    2) in order for this to be behind everything we set it at a negative depth, make sure nothing is covering the loaded image!

    Lastly, I haven't written as2 in a while so forgive me if you plug it in and get errors. Try to solve them based on the error and report back if can't. Good luck.
    http://code.mneilsworld.com/
    Text Effects | Bubbles | Dynamic Resize
    4 weeks , 20 papers ... thats 2 dollars .....Caassshhh!

  4. #4
    Developing For Dunkets mneil's Avatar
    Join Date
    Mar 2007
    Location
    Lincoln City
    Posts
    2,156
    forgot about the regular old random function - you could use that random over the Math.random(). Probably easier to understand.
    http://code.mneilsworld.com/
    Text Effects | Bubbles | Dynamic Resize
    4 weeks , 20 papers ... thats 2 dollars .....Caassshhh!

  5. #5
    Lissnoms
    Join Date
    Aug 2010
    Posts
    3
    Ok, I "think" it's working properly.

    I used your code, mneil.

    When I test my Movie, it gives me an error for loading one of my three images. But, I think that's simply because my files aren't uploaded to a server yet? Is that correct?

    I tested my Movie several times, and the random function definitely works. It chooses a different image each time I test it. It's simply not loading the image, and I think that's because of what I said above--my files aren't uploaded to a serve yet.

    Thanks so much for your help! Your explanations were extremely helpful.

  6. #6
    Developing For Dunkets mneil's Avatar
    Join Date
    Mar 2007
    Location
    Lincoln City
    Posts
    2,156
    No problem. What is the error? It could have to do with the filename, or incorrect path. Likely the filename though. The server shouldn't have anything to do with it unless you're loading it from a server other than your own. And, since you're saying it's not on a server then that's likely not the case.

    Anyway, I'm rambling. Good Luck.
    http://code.mneilsworld.com/
    Text Effects | Bubbles | Dynamic Resize
    4 weeks , 20 papers ... thats 2 dollars .....Caassshhh!

  7. #7
    Lissnoms
    Join Date
    Aug 2010
    Posts
    3
    This is the error I get:
    Error opening URL 'file:///C|/Users/Lisa/Desktop/WEBSITE/path/to/img2.jpg'

    Not sure how to fix it.

    Edit: Nevermind, I fixed it! Thanks again for your help mneil!!!!
    Last edited by Lissnoms; 08-23-2010 at 08:56 PM.

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center