dcsimg
A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: many buttons with same code, how to condense?

  1. #1
    Junior Member
    Join Date
    Oct 2001
    Posts
    14

    many buttons with same code, how to condense?

    this should be fairly simple, but i'm not sure how to tackle- i have 10 buttons which all have similar code involving enabled/disabled and setRGB from white to black. my code looks like this (below), and it's repeated for btn_01 through btn_10, so i've got roughly 200 lines of code that are nearly all the same.

    how would i go about condensing this code so there is not so much repetition?

    btn_01.onRelease = function(){
    btn_01.enabled = false;
    btn_02.enabled = true;
    etc
    etc
    btn_09.enabled = true;
    btn_10.enabled = true;
    text01Color.setRGB(0x000000);
    text02Color.setRGB(0xffffff);
    etc
    etc
    text09Color.setRGB(0xffffff);
    text10Color.setRGB(0xffffff);
    }


    btn_02.onRelease = function(){
    btn_01.enabled = true;
    btn_02.enabled = false;
    etc
    etc
    btn_09.enabled = true;
    btn_10.enabled = true;
    text01Color.setRGB(0xffffff);
    text02Color.setRGB(0x000000);
    etc
    etc
    text09Color.setRGB(0xffffff);
    text10Color.setRGB(0xffffff);
    }

  2. #2
    absent
    Join Date
    Aug 2000
    Location
    NB, Canada
    Posts
    155
    I'm sure there's a better way to write this, but i'd handle it along these lines:

    btn_1.onRelease = setBut(1);
    btn_2.onRelease = setBut(2);
    btn_3.onRelease = setBut(3);
    etc.

    function setBut(x){
    for (i=1; i<=10; i++){
    eval("btn_"+i).enabled = true;
    eval("text"+i+"Colour").setRGB(0xFFFFFF);
    }
    eval("btn_"+x).enabled = false;
    eval("text"+x+"Colour").setRGB(0x000000);
    }

    g'luck!
    - g

  3. #3
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    Thats not going to work at all. You will run the function instead of assigning it as an onRelease.

    It would be easier to automate if you left out the 0 in 01 etc. And so name your buttons and colours like this....

    btn_1
    btn_2
    ......
    btn_10

    text1Color
    text2Color
    .....
    text10Color

    Then use this code....

    Code:
    // set up button onRelease actions
    for(i=1; i<=10; i++)
    {
    	this['btn_'+i].id = i;
    	this['btn_'+i].onRelease = myOnRelease;
    }
    
    // set the appropriate buttons to enabled/disabled etc...
    function myOnRelease()
    {
    	for(i=0; i<=10; i++)
    	{
    		if(i==this.id)
    		{
    			this._parent['btn_'+i].enabled = false;
    			this._parent['text'+i+'Color'].setRGB(0x000000);
    		}
    		else
    		{
    			this._parent['btn_'+i].enabled = true;
    			this._parent['text'+i+'Color'].setRGB(0xFFFFFF);
    		}
    	}
    }
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  4. #4
    absent
    Join Date
    Aug 2000
    Location
    NB, Canada
    Posts
    155
    Lexicon is correct, it won't work when you have the onRelease on the timeline.
    I'm used to adding the onRelease code to the button itself, in which case it works.

    on (release) {
    setBut(#);
    }

    But again, it can no doubt be written better.

    g'luck
    - g

  5. #5
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    the version I posted above has the onRelease function on the timeline. The difference is I assign the function rather than running it.

    Code:
    btn.onRelease = myFunc(); // runs the function there and then so it wont work as you want.
    
    btn.onRelease = myFunc; // assigns the function so it works properly as an onRelease function.
    
    function myFunc()
    {
        trace("CLICKED");
    }
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  6. #6
    Lexi wins.

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