A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: [CS3] (AS2) Create Random Array with one Button

  1. #1
    Junior Member
    Join Date
    Feb 2008
    Posts
    24

    [CS3] (AS2) Create Random Array with one Button

    Hello All I'm trying to create an Array that will hold several Jpegs and ONE button that loads them randomly (here comes the stupid part) I have the
    code for the Array itself (I think) but I have no I idea how to connect a button to it. is the Array a function do I use onRelease ? sorry I know this is lame.

    Code:
    images =  new Array();                                  //create array
        images[0] = "image1.jpg";                               //Fill items
        images[1] = "image2.jpg";           
        images[2] = "image3.jpg";
        images[3] = "image4.jpg";
        images[4] = "image5.jpg";
        images[5] = "image6.jpg";
        images[6] = "image7.jpg";
        images[7] = "image8.jpg";
        images[8] = "image9.jpg";    
        
        for (i=1;i<5;i++) {
            ranarray = Math.randomInt(images.length);                 //get Random #
            output = images[ranarray];                        //name output
            eval("picloader"+i).loadMovie(images[ranarray]);  //load corresponding image
            images.splice(ranarray,1);          
            }
        stop();
    }
    I thought of using an empty MovieClip to load the jpegs should "picloader"
    be that MC ?

    Thank allot for reading any Tips would Great
    Cheers.

  2. #2
    Senior Member
    Join Date
    Dec 2005
    Posts
    426
    Create a button on the stage, with an instance name of "btnRand"

    then put this on the first frames actions
    Code:
    images=["image1.jpg","image2.jpg","image3.jpg","image4.jpg","image5.jpg","image5.jpg","image6.jpg","image7.jpg","image8.jpg"];//Other way works too, but i prefer this way.
    
    //Im guessing you only want one image to display at a time if not just let me know
    this.createEmptyMovieClip("picture",1); //New movieclip named picture with a depth of 1.
    picture._x=20;
    picture._y=20;
    
    btnRand.onPress=function()
    {
            num=Math.randomInt(images.length); //random number from the total length of the images
            picture.loadMovie(images[num]); //Load the movie
            images.splice(num,1); //remove the image from the array        
    }

  3. #3
    Junior Member
    Join Date
    Feb 2008
    Posts
    24

    Error Msg

    First of all thanks allot for the replay- it's great when you guys actually explain each line of code !

    however I'm getting an error msg-
    There is no method with the name 'randomInt'.

  4. #4
    Junior Member
    Join Date
    Feb 2008
    Posts
    24
    Forgot the code :

    images = new Array();
    images=["image1.jpg","image2.jpg","image3.jpg","image4.jpg ","image5.jpg","image5.jpg","image6.jpg","image7.j pg","image8.jpg"];


    this.createEmptyMovieClip("picture",1); picture._x=20;
    picture._y=20;

    btnRand.onPress=function()
    {
    num=Math.randomInt(images.length);
    picture.loadMovie(images[num]);
    images.splice(num,1);
    }

  5. #5
    Senior Member
    Join Date
    Dec 2005
    Posts
    426
    did not check if that was a function,

    use
    Code:
    num=1+random(images.length-1);

  6. #6
    Junior Member
    Join Date
    Feb 2008
    Posts
    24

    Almost !!

    No Errors in the script however after it loads the 8 images I get this -
    Error opening URL 'file:///Mac/Users/Tal/Desktop/IG%20ART%20WEB/undefined'

    Sorry If I'm doing something wrong..

  7. #7
    Senior Member
    Join Date
    Dec 2005
    Posts
    426
    No it seems it is not getting the right image path. Just a minute i will make an example and test it.

  8. #8
    Senior Member
    Join Date
    Dec 2005
    Posts
    426
    Ok, i goofed, i forgot that arrays started with 0 :/

    Code:
    btnRand.onPress=function()
    {
    	num=random(images.length);
    	picture.loadMovie(images[num]);
    	images.splice(num,1);
    }
    just replace the code.

  9. #9
    Junior Member
    Join Date
    Feb 2008
    Posts
    24
    Still that Unidentified thing after a couple images
    Code I have :
    images = new Array();
    images=["image1.jpg","image2.jpg","image3.jpg","image4.jpg ","image5.jpg","image6.jpg","image7.jpg","image8.j pg"];//Other way works too, but i prefer this way.

    //Im guessing you only want one image to display at a time if not just let me know
    this.createEmptyMovieClip("picture",1); //New movieclip named picture with a depth of 1.
    picture._x=-167;
    picture._y=86;

    btnRand.onPress=function()
    {
    num=random(images.length);
    picture.loadMovie(images[num]);
    images.splice(num,1);
    }

  10. #10
    Senior Member
    Join Date
    Dec 2005
    Posts
    426
    Most likely those images dont exist? also there is an error on image8.jpg.

    To help debugging and to make sure the images are still in the array. Add trace(images); after the splice line.

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