A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: [F8] Array of Buttons

  1. #1
    Member
    Join Date
    Nov 2003
    Posts
    88

    [F8] Array of Buttons

    Is there a way to create an array of buttons, evenly spaced on the stage.
    And at the same time give these buttons instance names?

  2. #2
    Junior Member
    Join Date
    Nov 2006
    Posts
    15
    create a button and set the linkage (rightmousebutton on the mc in the library)

    Code:
    var buttons:Array = new Array();
    var amountOfButtons:Number = 10;
    _root.createEmptyMovieClip("mcHolder",_root.getNextHighestDepth());
    buttons.push(_root.mcHolder);
    for (i=1;i<=amountOfButtons;i++) {
    	mc = mcHolder.attachMovie("button","button" + i,mcHolder.getNextHighestDepth());
    	buttons.push(mc);
    	mc._x = 100;
    	mc._y = 30 + 30 * i;
    }
    now you have buttons with even spacing between them (30) and an array containing all those buttons.
    you can change the _x of button number 1 by using:
    buttons[1]._x = 30
    or
    mcHolder.button1._x = 30;
    Last edited by Schiz; 11-21-2006 at 10:27 PM.

  3. #3
    Member
    Join Date
    Nov 2003
    Posts
    88
    Thank you, Thank you, Thank you.
    I don't know if this is the right way to do what I want,
    but it works for me.

    I messed around with it a bit, came up with this.
    The stage is 800x800 and the buttons are 50px square

    PHP Code:
    stop();
    //
    var buttons:Array = new Array();
    var 
    amountOfButtons:Number 16;
    _root.createEmptyMovieClip("mcHolder"_root.getNextHighestDepth());
    buttons.push(_root.mcHolder);


    //---------------- 1
    for (i=1i<=amountOfButtonsi++) {
        
    mc mcHolder.attachMovie("button""button"+imcHolder.getNextHighestDepth());
        
    buttons.push(mc);
        
    mc._x 0;
        
    mc._y = (-50)+50*i;
    }
    //---------------- 2
    for (i=1i<=amountOfButtonsi++) {
        
    mc mcHolder.attachMovie("button""button"+imcHolder.getNextHighestDepth());
        
    buttons.push(mc);
        
    mc._x 50;
        
    mc._y = (-50)+50*i;
    }
    //---------------- 3
    for (i=1i<=amountOfButtonsi++) {
        
    mc mcHolder.attachMovie("button""button"+imcHolder.getNextHighestDepth());
        
    buttons.push(mc);
        
    mc._x 100;
        
    mc._y = (-50)+50*i;
    }
    //---------------- 4
    for (i=1i<=amountOfButtonsi++) {
        
    mc mcHolder.attachMovie("button""button"+imcHolder.getNextHighestDepth());
        
    buttons.push(mc);
        
    mc._x 150;
        
    mc._y = (-50)+50*i;
    }
    //---------------- 5
    for (i=1i<=amountOfButtonsi++) {
        
    mc mcHolder.attachMovie("button""button"+imcHolder.getNextHighestDepth());
        
    buttons.push(mc);
        
    mc._x 200;
        
    mc._y = (-50)+50*i;
    }
    //---------------- 6
    for (i=1i<=amountOfButtonsi++) {
        
    mc mcHolder.attachMovie("button""button"+imcHolder.getNextHighestDepth());
        
    buttons.push(mc);
        
    mc._x 250;
        
    mc._y = (-50)+50*i;
    }
    //---------------- 7
    for (i=1i<=amountOfButtonsi++) {
        
    mc mcHolder.attachMovie("button""button"+imcHolder.getNextHighestDepth());
        
    buttons.push(mc);
        
    mc._x 300;
        
    mc._y = (-50)+50*i;
    }
    //---------------- 8
    for (i=1i<=amountOfButtonsi++) {
        
    mc mcHolder.attachMovie("button""button"+imcHolder.getNextHighestDepth());
        
    buttons.push(mc);
        
    mc._x 350;
        
    mc._y = (-50)+50*i;
    }
    //---------------- 9
    for (i=1i<=amountOfButtonsi++) {
        
    mc mcHolder.attachMovie("button""button"+imcHolder.getNextHighestDepth());
        
    buttons.push(mc);
        
    mc._x 400;
        
    mc._y = (-50)+50*i;
    }
    //---------------- 10
    for (i=1i<=amountOfButtonsi++) {
        
    mc mcHolder.attachMovie("button""button"+imcHolder.getNextHighestDepth());
        
    buttons.push(mc);
        
    mc._x 450;
        
    mc._y = (-50)+50*i;
    }
    //---------------- 11
    for (i=1i<=amountOfButtonsi++) {
        
    mc mcHolder.attachMovie("button""button"+imcHolder.getNextHighestDepth());
        
    buttons.push(mc);
        
    mc._x 500;
        
    mc._y = (-50)+50*i;
    }
    //---------------- 12
    for (i=1i<=amountOfButtonsi++) {
        
    mc mcHolder.attachMovie("button""button"+imcHolder.getNextHighestDepth());
        
    buttons.push(mc);
        
    mc._x 550;
        
    mc._y = (-50)+50*i;
    }
    //---------------- 13
    for (i=1i<=amountOfButtonsi++) {
        
    mc mcHolder.attachMovie("button""button"+imcHolder.getNextHighestDepth());
        
    buttons.push(mc);
        
    mc._x 600;
        
    mc._y = (-50)+50*i;
    }
    //---------------- 14
    for (i=1i<=amountOfButtonsi++) {
        
    mc mcHolder.attachMovie("button""button"+imcHolder.getNextHighestDepth());
        
    buttons.push(mc);
        
    mc._x 650;
        
    mc._y = (-50)+50*i;
    }
    //---------------- 15
    for (i=1i<=amountOfButtonsi++) {
        
    mc mcHolder.attachMovie("button""button"+imcHolder.getNextHighestDepth());
        
    buttons.push(mc);
        
    mc._x 700;
        
    mc._y = (-50)+50*i;
    }
    //---------------- 16
    for (i=1i<=amountOfButtonsi++) {
        
    mc mcHolder.attachMovie("button""button"+imcHolder.getNextHighestDepth());
        
    buttons.push(mc);
        
    mc._x 750;
        
    mc._y = (-50)+50*i;
    }
    /////////////////////////////////////
    ///////////  TEST BTNS ///////////
    /////////////////////////////////////

    buttons[1].onRelease = function() {trace(this);}
    buttons[2].onRelease = function() {trace(this);}
    buttons[3].onRelease = function() {trace(this);}
    buttons[4].onRelease = function() {trace(this);}
    buttons[5].onRelease = function() {trace(this);}
    buttons[61].onRelease = function() {trace(this);}
    buttons[256].onRollOver = function() {trace(this);} 

  4. #4
    Senior Member Genesis F5's Avatar
    Join Date
    Jan 2002
    Location
    Unallocated memory
    Posts
    1,845
    Why did you copy the for loop that many times?

    If you need to call the for loop that many times, I suggest creating a fucntion, but from what I see, they all are doing the same exact thing.

    Take for-loops 2 -16 out of there.

  5. #5
    Junior Member
    Join Date
    Nov 2006
    Posts
    15
    Code:
    stop(); 
    // 
    var buttons:Array = new Array(); 
    var amountOfButtons:Number = 16; 
    _root.createEmptyMovieClip("mcHolder", _root.getNextHighestDepth()); 
    buttons.push(_root.mcHolder); 
    
    for(j=1;j<=16;j++) {
    	for (i=1; i<=amountOfButtons; i++) { 
    		mc = mcHolder.attachMovie("button", "button"+i, mcHolder.getNextHighestDepth()); 
    		buttons.push(mc); 
    		mc._x = j * 50 - 50; 
    		mc._y = (-50)+50*i; 
    	} 
    }
    here's a simplified code of what you made... but nice going glad i could help

  6. #6
    Member
    Join Date
    Nov 2003
    Posts
    88
    Quote Originally Posted by Schiz
    Code:
    stop(); 
    // 
    var buttons:Array = new Array(); 
    var amountOfButtons:Number = 16; 
    _root.createEmptyMovieClip("mcHolder", _root.getNextHighestDepth()); 
    buttons.push(_root.mcHolder); 
    
    for(j=1;j<=16;j++) {
    	for (i=1; i<=amountOfButtons; i++) { 
    		mc = mcHolder.attachMovie("button", "button"+i, mcHolder.getNextHighestDepth()); 
    		buttons.push(mc); 
    		mc._x = j * 50 - 50; 
    		mc._y = (-50)+50*i; 
    	} 
    }
    here's a simplified code of what you made... but nice going glad i could help
    PERFECT!!!
    Thanks again.



    Quote Originally Posted by Genesis F5
    Why did you copy the for loop that many times?

    If you need to call the for loop that many times, I suggest creating a fucntion, but from what I see, they all are doing the same exact thing.

    Take for-loops 2 -16 out of there.
    I didn't know how else to get the array to duplicate accross the stage.
    That was my 'take the long way around' method :-)

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