Skip navigation
Currently Being Moderated

Arrays and For Loops To Generate Multiple Btns For Loading Images

Sep 22, 2009 11:18 AM

I have this array that contains the names of various images I want to load into Flash

 

var imageArray:Array=["image1","image2","image3","image4","image5","image 6","image7",
"image8","image9","image10"];

 

Now I want to create 10 different buttons with each button loading in an image. I know there's a way to do this with a for loop without having to make 10 different buttons and writing 10 different functions. Not sure how to correlate the members imageArray with the generated btns.

 

i.e. btn1 loads in image1, btn2 loads in image2, etc.

 
Replies
  • kglad
    62,058 posts
    Jul 21, 2002
    Currently Being Moderated
    Sep 22, 2009 11:54 AM   in reply to anthonyv1121

     

    var imageArray:Array=["image1","image2","image3","image4","image5","image 6","image7 ",
    "image8","image9","image10"];

     

    for(var i:uint=0;i<imageArray.length;i++){
    var btn:YourButton=new YourButton();
    btn.ivar=i;
    addChild(btn);
    btn.x=..
    btn.y=..
    btn.addEventListener(MouseEvent.CLICK,clickF);
    }

    var ldr:Loader=new Loader();
    addChild(ldr);

    function clickF(e:MouseEvent){
    ldr.load(new URLRequest(imageArray[e.currentTarget.ivar]));
    }
     
    |
    Mark as:
  • kglad
    62,058 posts
    Jul 21, 2002
    Currently Being Moderated
    Sep 22, 2009 3:39 PM   in reply to anthonyv1121

    each button needs to know it has some relation to the number i.  ivar stores that value and it's retrieved when the button is clicked.

     
    |
    Mark as:
  • kglad
    62,058 posts
    Jul 21, 2002
    Currently Being Moderated
    Sep 22, 2009 3:45 PM   in reply to anthonyv1121

    you're welcome.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 18, 2012 2:21 AM   in reply to kglad

    Hi kglad.

     

    I am wondering if you can help me on the same subject:

    i used your above code as such, (below,) and it loads the first image in the array, while what i'd like is to add code that loads the elements of the array - the sequentially numbered images, one right after the other to create a moving image effect, that when u click the btn you get the pics loading 1,2,3,4,5 etc.. with a tiny delay in between so it looks like silent movie. Plz consider that i am not an event intermediate and need examples of code, if you do decide to help out. thnks for your time.

     

     

    import flash.net.URLRequest;

    import flash.display.Loader;

     

     

    var imageArray:Array=["1.jpg","2.jpg","3.jpg","4.jpg","5.jpg","6.jpg","7. jpg",

    "8.jpg","9.jpg","9.jpg"];

     

     

    for(var i:uint=0;i<imageArray.length;i++){

    var btn:YourButton=new YourButton();

    btn.ivar=i;

    addChild(btn);

    btn.x=0;

    btn.y=0;

    btn.addEventListener(MouseEvent.CLICK,clickF);

    }

     

    var ldr:Loader=new Loader();

    addChild(ldr);

     

    function clickF(e:MouseEvent){

    ldr.load(new URLRequest("D:/flash cs5.5/image_sequence/twirlSeq/"+imageArray[e.currentTarget.ivar]));

    }

     
    |
    Mark as:
  • kglad
    62,058 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 18, 2012 6:27 AM   in reply to minimalcomfort

    you should start another thread.

     

    your solution will be more complicated becaues you need to preload your image sequence and then you need to use a loop to load the sequence.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points