A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Need help loading 4 external .jpgs randomly and resize them?

  1. #1
    Member
    Join Date
    Aug 2006
    Posts
    57

    Need help loading 4 external .jpgs randomly and resize them?

    Hello there,

    I have quite the task in front of me. I have designed a homepage that has 4 boxes on the right hand side that are of different size. I would like to have a movie loader that loads .jpgs randomly from a specified folder into each box. At the same time I don't want any repeats in any of the 4 boxes. Also is it possible to resize the images according to the box size without having to resize them outside of flash? I can have all the images in the external folder the same size, but then if I can just shrink them with whichever image falls into the box the random script assigns.

    Does that make sense?

    Look at the attached .swf and .fla. That should help clarify my question.

    Thanks so much,
    Lorne
    Attached Files Attached Files

  2. #2
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,145
    Create a new array to store the jpg numbers you have loaded. Use the xml data to tell you how many jpgs there are. Then generate a random number between zero and the total jpgs. Check the number against the array of loaded jpgs. If not found, push the random number into the array and load the jpg.

    You should use movieClipLoader because it will tell you when the jpg is visible so you can get its width and height. There's a time gap between the time when a jpg has loaded and when it's actually showing. You can't get the width and height until it's showing. Below is example of loading jpgs one after another. You can get the height and width inside the onLoadInit function so resize the jpgs there.

    Code:
    var mclListener:Object = new Object();
    var image_mcl:MovieClipLoader = new MovieClipLoader();
    image_mcl.addListener(mclListener);
    
    var imageLoading:Number = 0;
    	
    function loadImages(){
    	if(imageLoading < 3){
    		imageLoading++;
    		image_mcl.loadClip("images/pic_"+imageLoading+".jpg", _root["picHolder_" + imageLoading]);
    	}else{
    		trace("loadImages():  all images have loaded");
    	}
    }
    
    
    mclListener.onLoadInit = function(target_mc:MovieClip) {
    	trace("onLoadInit: clip=" + target_mc);
    	target_mc._parent._parent.loadingAnimation._visible = false;
    	loadImages();
    }
    
    
    loadImages();
    Also, don't use eval like that. Use regular object references.

    Instead of:
    eval("galleryMenu_mc.button"+ii+"_mc")._y

    use:
    galleryMenu_mc["button" + ii + "_mc"]._y
    Last edited by moot; 08-01-2009 at 12:02 PM.

  3. #3
    Member
    Join Date
    Aug 2006
    Posts
    57
    Hello,

    I have been tinkering with it and got it to finally work.

    Here's the code, it's a bit different than yours but I thought I'd share with others.

    Code:
    stop();
    //Random Image Loader:
    //Variables
    var currBox:Number = new Number();
    var contentWidth:Number = box_0.content._width;
    var borderSize:Number = 10;
    //Image loader
    this.createEmptyMovieClip("image_mc", this.getNextHighestDepth());
    var mclListener:Object = new Object();
    mclListener.onLoadInit = function(target_mc:MovieClip) {
        with (target_mc) {
            if (_width>_height) {
                target_mc._xscale = target_mc._yscale=(contentWidth/target_mc._width)*100;
            } else {
                target_mc._xscale = target_mc._yscale=(contentWidth/target_mc._height)*100;
            }
            _x = (_root["box_"+currBox]._width-_width)/2;
            _y = (_root["box_"+currBox]._height-_height)/2;
        }
    };
    var image_mcl:MovieClipLoader = new MovieClipLoader();
    image_mcl.addListener(mclListener);
    //Array containing pictures
    var pics:Array = new Array();
    pics[0] = "Images/Blue.jpg";
    pics[1] = "Images/Red.jpg";
    pics[2] = "Images/Green.jpg";
    pics[3] = "Images/Yellow.jpg";
    pics[4] = "Images/Purple.jpg";
    pics[5] = "Images/Orange.jpg";
    //Keep only 4 images
    while (pics.length>4) {
        pics.splice(random(pics.length), 1);
    }
    //Load each image randomly into one of the four boxes
    if (pics.length<5) {
        for (var i = 0; i<4; i++) {
            var r:Number = random(pics.length);
            currBox = i;
            image_mcl.loadClip(pics[r], _root["box_"+i].content);
            pics.splice(r, 1);
        }
    }

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