Page 2 of 2 FirstFirst 12
Results 11 to 17 of 17

Thread: Flash Random Image Loader help needed...

  1. #11
    Join Date
    Aug 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi
    This code is exactly what I've been looking for, although I couldn't make it work.
    I just changed the path and img names but the only thing that I get is a blank screen.
    I tested every step and it seems like it's working fine but jpg files do not display.
    I'm new to this and I use AS 2.0. I really appreciate any help
    Thanks

  2. #12
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    Check the registration points of your loader movieclip (they should be in the upper left) and/or post your source.
    Last edited by Medyman; 08-17-2007 at 12:49 PM.

  3. #13
    Join Date
    Aug 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ok this is the code on the first frame.
    "createEmptyMovieClip" creates check_mc on the stage right?
    So it's automatically on the upper left corner.


    Code:
    stop();
    
    var thisMovie:MovieClip = this;  // A reference to the current movie
    
    // The movieClipLoader and listener
    var my_mcl:MovieClipLoader = new MovieClipLoader();
    var clipListener1:Object = new Object();
    
    // An array to hold valid path names
    var images:Array = new Array();
    
    // Variables to facilitate the check
    var maxImages:Number = 3;
    var currentImage:Number = 1;
    var imagePath:String = "images/"
    var currentImagePath:String = imagePath + currentImage + ".jpg";
    
    // A movieClip to hold the images temporarily
    var check_mc:MovieClip = this.createEmptyMovieClip("check_mc",0);
    check_mc._alpha = 0;
    
    // Start the load check
    my_mcl.loadClip(currentImagePath, check_mc);
    
    
    // Loads the next image in the sequence
    function checkNextImage(){
    	currentImage++;
    	currentImagePath = imagePath + currentImage + ".jpg";
    	my_mcl.loadClip(currentImagePath, check_mc);
    trace("checked "  + currentImagePath);
    	}
    
    
    // Loading events
    clipListener1.onLoadStart = function(){
    	trace("Added: " + currentImagePath);
    	images.push(currentImagePath);
    	checkNextImage();
    }
    clipListener1.onLoadError = function(){
    	if(currentImage < maxImages){
    		checkNextImage();
    	} else {
    		thisMovie.gotoAndPlay("display");
    	}
    }
    my_mcl.addListener(clipListener1);

  4. #14
    Join Date
    Aug 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    And this is what's on display frame.
    I can't see any problem but there is absolutely one


    Code:
    stop();
    trace(images);
    
    // Display your images randomly here
    
    var imageArray:Array = images;			// Just grabs the array from frame 1 so you remember it's there
    var numImages:Number = imageArray.length;	// The number of valid images
    var thisImage:String;					// Property for the current image
    var delayTime:Number = 3000;			// The delay, in milliseconds that you want between image switches (1 sec = 1000 ms)
    
    my_mcl.removeListener(clipListener1);	// Gets rid of the checking actions
    
    // Get an image to display
    getNewImage();
    
    
    // Gets a new image path from the array and call the function to display that image
    function getNewImage(){
    	var newImgNum:Number = randRange(0, (numImages - 1));  // Get a random item number in the array
    	var newImage:String = imageArray[newImgNum];  // Get the image path from the array
    		
    	if (newImage != thisImage){ // If it's not the same as what's displayed
    		displayImage(newImage); // Calls the function below 
    		setTimeout(getNewImage, delayTime); // Calls this function again in a set amount of time
    		thisImage = newImage;  // Sets the current image property
    	} else {
    		getNewImage(); // if the image called is the same as the one that's displayed, get a new one
    	}
    }
    
    
    // Displays the image
    function displayImage(path:String){
    	
    	check_mc._alpha = 100
    	trace("displayed " + thisImage);
    	// Your code here
    }
    
    // Gets a random number within a range
    function randRange(min:Number, max:Number):Number {
        var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
        return randomNum;
    }

  5. #15
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    I kinda skimmed over the code but didn't really pay much attention to it.
    I build these all the time and I think your random picture function is way too needlessly complex with too much code especially since I didn't notice any XML loading, picture transition functions or preloader code.

    Here's how I would do it in 12 lines of code:

    Code:
    var pics:Array = new Array();
    pics[0] = "image1.jpg";
    pics[1] = "image1.jpg";
    pics[2] = "image1.jpg";
    pics[3] = "image1.jpg";
    function showRandomPic() {
       var n:Number = Math.ceil(Math.random()*pics.length-1); //create random number
       pic_mc.loadMovie(pics[n]); // load associated image from random number
    }
    var timer = setInterval(function () {//set up timer
    showRandomPic();//run function
    },1000);//load random pic every 1 second
    It's just the most basic concept of a random picture without any bells and whistles.

  6. #16
    Join Date
    Aug 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks.
    Is it complicated to avoid same pictures repeating right after?

  7. #17
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    Something like this:

    Code:
    var a:Array = new Array();
    
    for(i=0;i<15;i++){
       
       a[i] = i;
       
       }
    
    var stored:Number = Math.ceil(Math.random()*a.length-1);// generate first random number to store in memory
    
    function randomNumber() {
    
       var n:Number = Math.ceil(Math.random()*a.length-1);//generate random number for display
    
       if (n == stored) {
    
          ran();//run function again
    
       } else {
    
          stored = n; //store new random number into memory if not matching
          trace(a[n]);
    
       }
    }
    var timer = setInterval(function () {
    randomNumber();
    }, 100);

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
  •