A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Using a for loop to streamline code?

  1. #1
    Junior Member
    Join Date
    Mar 2005
    Posts
    16

    Using a for loop to streamline code?

    I have some AS2 code that has a lot of duplication to control various buttons and MovieClips and was wondering if it's possible to streamline this code using a 'for' loop, like I would when attaching MovieClips dynamically.

    Here's the code as it's currently written:

    Code:
    //----------- INIT VARS -------------------------
    
    var color01:String = "0xE24C9B";
    var color02:String = "0x009BDB";
    var color03:String = "0x50B848";
    var color04:String = "0xF58220";
    
    var base01:MovieClip = timer01_mc.timerBase_mc;
    var base02:MovieClip = timer02_mc.timerBase_mc;
    var base03:MovieClip = timer03_mc.timerBase_mc;
    var base04:MovieClip = timer04_mc.timerBase_mc;
    
    var btn01:Button = timer01_mc.rollBtn_btn;
    var btn02:Button = timer02_mc.rollBtn_btn;
    var btn03:Button = timer03_mc.rollBtn_btn;
    var btn04:Button = timer04_mc.rollBtn_btn;
    
    var bar01:MovieClip = timer01_mc.timerBar_mc;
    var bar02:MovieClip = timer02_mc.timerBar_mc;
    var bar03:MovieClip = timer03_mc.timerBar_mc;
    var bar04:MovieClip = timer04_mc.timerBar_mc;
    
    var url01:String = "http://www.url01.com";
    var url02:String = "http://www.url02.com";
    var url03:String = "http://www.url03.com";
    var url04:String = "http://www.url04.com";
    
    //-------------- BUTTON CONTROL -------------------------
    
    btn01.onRollOver = function(){ baseCol01 = new Color(base01); baseCol01.setRGB(color01);
    };
    btn01.onRollOut = function(){ baseCol01 = new Color(base01); baseCol01.setRGB(0xCCCCCC);
    };
    btn01.onRelease = function(){ getURL(url01, "_blank");
    };
    
    btn02.onRollOver = function(){ baseCol02 = new Color(base02); baseCol02.setRGB(color02);
    };
    btn02.onRollOut = function(){ baseCol02 = new Color(base02); baseCol02.setRGB(0xCCCCCC);
    };
    btn02.onRelease = function(){ getURL(url02, "_blank");
    };
    
    btn03.onRollOver = function(){ baseCol03 = new Color(base03); baseCol03.setRGB(color03);
    };
    btn03.onRollOut = function(){ baseCol03 = new Color(base03); baseCol03.setRGB(0xCCCCCC);
    };
    btn03.onRelease = function(){ getURL(url03, "_blank");
    };
    
    btn04.onRollOver = function(){ baseCol04 = new Color(base04); baseCol04.setRGB(color04);
    };
    btn04.onRollOut = function(){ baseCol04 = new Color(base04); baseCol04.setRGB(0xCCCCCC);
    };
    btn04.onRelease = function(){ getURL(url04, "_blank");
    };
    Can anyone show me how to construct the correct type of 'for' loop to streamline this code?

  2. #2
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,145
    You can reference objects as shown below. You can have the buttons share functions:

    for(var i=1;i<=4;i++){
    holderClip["btn0" + i].buttNo = i;
    holderClip["btn0" + i].onRollOver = overButton;
    holderClip["btn0" + i].onRollOver = offButton;
    holderClip["btn0" + i].onRollOver = pressButton;
    }

    Whatever button calls the function can be referenced by "this":

    function pressButton(){
    trace("button " + this.buttNo + " was pressed");
    }

    When btn04 is pressed it will trace "button 4 was pressed".

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