A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [F8] Help with making AS smaller, please?

  1. #1
    Eternal Newbie
    Join Date
    Apr 2006
    Location
    Bitter cold Hell of the crappy NW
    Posts
    392

    [F8] Help with making AS smaller, please?

    I am making a menu and on it are MCs that act as buttons. They have 2 states (on and off) and I have the AS set up where if you turn one "on" all the others go "off"

    The problem is that I have 16 of these buttons and the AS to turn each off is getting very long. I am not sure if there is a way to make it better or not. I was thinking maybe one of thse arrays or the "if i>=0 {do this" type of thing.

    Usually I jst code on button to do what ever, but I am trying new things (external AS files and functions and such) and I am not sure how to set it up.

    Currently the AS for one button looks like this:

    Code:
    	if (tog1._currentframe == 1) {
    		tog1.gotoAndStop(2);
    		togIsOn = 1;
    		tog2.gotoAndStop(1);
    		tog3.gotoAndStop(1);
    		tog4.gotoAndStop(1);
    		tog5.gotoAndStop(1);
    		tog6.gotoAndStop(1);
    		tog7.gotoAndStop(1);
    		tog8.gotoAndStop(1);
    		tog9.gotoAndStop(1);
    		tog10.gotoAndStop(1);
    		tog11.gotoAndStop(1);
    		tog12.gotoAndStop(1);
    		tog13.gotoAndStop(1);
    		tog14.gotoAndStop(1);
    		tog15.gotoAndStop(1);
    		tog16.gotoAndStop(1);
    	} else if (tog1._currentframe == 2) {
    		tog1.gotoAndPlay(3);
    		togIsOn = 0;
    	}
    };
    Each button is an instance of MC named "tog" and they all have numbers (1 - 16) added to the "tog" for an instance name (tog1, tog12, etc.)

    I am sure there is something I can do to to shorten this code so I don't have to put this code inside each button function. I just don't know how. Also, the "togIsOn = #" is just a check for another button. If the tog is set to "on" togIsOn will == that tog's number, if the tog is then turned off (all off) then togIsOn == 0;.

    Anyone have an idea?

    ~MoN
    If I am wrong, please just correct me and move on.. there is no need for all that pointing and laughing! ~MoN

  2. #2
    Total Universe Mod jAQUAN's Avatar
    Join Date
    Jul 2000
    Location
    Honolulu
    Posts
    2,429
    You can shorten that to a very simple bit of code but it's going to take some explanation so this post is kinda long.
    Basically you will create one function that handles updating all the buttons based on the current state of things.
    Clicking each button will simply alter the state (in code) and run that function. That function will loop ever button in a list and compare it to the current state. After that's explained I'll show you how to skip using the list for even simpler code.

    The first thing to do is choose a logical nameing skeme for your menu clips. Updating the movie with more buttons and reusing the code in other projects becomes much easier if like items are numbered starting with 0. For example menuClip0, menuClip1, menuClip2,... etc.

    Next we'll create a variable to store the current state in. Since our current state will be the active menu, we'll call it activeMenu.

    PHP Code:
    //create a container to hold the current state.
    activeMenu;

    //create an array of all menu buttons to loop over.
    var mainMenuButtons = ["menuClip0""menuClip1""menuClip2""menuClip3""menuClip4"];

    //we'll create the updateMenu() function later
    //for now you need to tell each button to call it

    //create a function to initialize the buttons
    function setupMenu(){
        
    //record the length of the menu list to make the loop run faster
        
    var numBtns mainMenuButtons.length;
        
        
    //create a for loop to step through the list
        
    for(i=0i<numBtnsi++){
            
    //record a temporary variable for this loop only
            
    var tempBtn this[mainMenuButtons[i]];
            
            
    //record a variable on each button so they can pass it when they are clicked
            
    tempBtn.btnID i;
            
            
    //tell each button to call updateMenu() and pass its btnID
            
    tempBtn.onRelease = function(){
                
    //if this button is not already active
                
    if(activeMenu != this.btnID){
                    
    //change the active button to this one
                    
    activeMenu this.btnID;
                    
    //call updateMenu()
                    
    updateMenu(this.btnID);
                }
                
            }
        }
    }


    //now for the updateMenu() function
    function updateMenu(ID){
        
    //record the length of the menu list to make the loop run faster
        
    var numBtns mainMenuButtons.length;
        
        
    //create a for loop so we can check each button
        
    for(i=0numBtnsi++){
            
    //record a temp var for this loop only
            
    var tempBtn this[mainMenuButtons[i]];
            
            
    //assuming frame 1 is off and frame 2 is on.
            
    if(tempBtn.btnID == activeMenu){ //this is the clicked button
                
    tempBtn.gotoAndStop(2);
            }else{
                
    tempBtn.gotoAndStop(1);
            }
        }
    }

    //then to get the ball rolling, call setupMenu()
    setupMenu(); 

    That's it!

    NOTE: You can save yourself from having to maintain a list by evaluating the clip names during the loop.

    Just add this variable below activeMenu;
    Code:
    numBtns = 4; //now all you have to change is this number when you add more buttons
    Delete this line:
    Code:
    var mainMenuButtons = ["menuClip0", "menuClip1", "menuClip2", "menuClip3", "menuClip4"];
    Then delete these lines, be sure to get both
    Code:
    var numBtns = mainMenuButtons.length;
    Then simply replace both instances of this line:
    Code:
    var tempBtn = this[mainMenuButtons[i]];
    with this one:
    Code:
    var tempBtn = this["menuClip"+i];

  3. #3
    Eternal Newbie
    Join Date
    Apr 2006
    Location
    Bitter cold Hell of the crappy NW
    Posts
    392
    Wow! You explained it very well, and I thank you!

    I will try this out right now!

    <EDIT>
    That was what I needed! Thank you so very much! I learned something today!
    </EDIT>
    Last edited by MasterofNothing; 01-08-2008 at 10:16 PM.
    If I am wrong, please just correct me and move on.. there is no need for all that pointing and laughing! ~MoN

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