11 Replies Latest reply on Apr 4, 2011 4:03 AM by janineabc

    Script to make button stay on hit state until another is clicked!

    janineabc Level 1

      Hi

       

      Got a bit of a issue I can not solve.

       

      I have a line of 5 buttons which each links to a different page. These buttons are all currently in one movie clip.

       

      However I want to create an effect where when one is clicked on it stays highlighted. i.e. stays on the hit state, until another one of the buttons is clicked on.

       

      I would have thought this would be somethink I could find online, but carnt seem to find it anywhere.

       

      I've found the code to make one item stay on when clicked, but it works on its own to the whole movie clip, so it has no effect when other buttons are clicked on!

       

      Have attempted the script below, but Im sure that that is not going to work as it doesnt link to the other buttons at all.

       

      on (press) {

      if (this._currentframe == 1) {
      this.gotoAndStop(2);
      } else {
      this.gotoAndStop(1);
      }
      }
      Thanks for any help!
        • 1. Re: Script to make button stay on hit state until another is clicked!
          Ned Murphy Adobe Community Professional & MVP

          You need to use movieclips for buttons if you want to retain states.  Otherwise you could use a movieclip behind the buttons that provides some form of highlighting/glow/selection indication.

          • 2. Re: Script to make button stay on hit state until another is clicked!
            Ned Murphy Adobe Community Professional & MVP

            To use something like what you showed, you would need to have some reset function that gets called to either tell all buttons to return to their normal states, or have a variable that identifies the last button clicked and tells it to go back to its normal state.  Although, that code you showed is for more of a toggle functionality than what you probably intend.

            • 3. Re: Script to make button stay on hit state until another is clicked!
              janineabc Level 1

              Hi,

               

              Thanks for that.

               

              So if I turn them into movie clips would I have to put script on each individual movie clip or on one movie clip which controls the whole set??

               

              And do you have any ideas of where I can find the script to be able to do this?

               

              Thanks

              • 4. Re: Script to make button stay on hit state until another is clicked!
                Ned Murphy Adobe Community Professional & MVP

                When thoughts of putting code on objects pop up, dismiss the notion and put the code in the timeline where it is easy to manage and easier to implement.

                 

                For one approach I show below, you would be placing the code in the timeline that contains the buttons (2-frame movieclips) and you would be assigning instance names to the buttons as indicated (btn1, btn2, etc... though you can choose your own names)

                 

                var lastBtnUsed;

                 

                btn1.onRelease = function(){
                    processBtns(btn1);
                }

                 

                btn2.onRelease = function(){
                    processBtns(btn2);
                }

                 

                btn3.onRelease = function(){
                    processBtns(btn3);
                }

                 

                btn4.onRelease = function(){
                    processBtns(btn4);
                }

                 

                function processBtns(newBtn){
                    if(lastBtnUsed != null && lastBtnUsed != newBtn){

                        lastBtnUsed.gotoAndStop(1);  // reset current highlighted button
                    }
                    newBtn.gotoAndStop(2);  // highlight new button

                    lastBtnUsed = newBtn;
                }

                 

                You can also implement this in the timeine that holds the movieclip instead, but you will need to target the buttons thru the movieclip... as in...

                 

                movieName.btn1.onRelease.... etc

                • 5. Re: Script to make button stay on hit state until another is clicked!
                  janineabc Level 1

                  Thank you,

                   

                  I will give it a go, I'll let you know how I get on!

                  • 6. Re: Script to make button stay on hit state until another is clicked!
                    Ned Murphy Adobe Community Professional & MVP

                    You're welcome.  Here's a somewhat condensed version of the same...

                     

                    var lastBtnUsed;

                     

                    btn1.onRelease = btn2.onRelease = btn3.onRelease = btn4.onRelease = function(){
                        processBtns(this);
                    }

                     

                    function processBtns(newBtn){
                        if(lastBtnUsed != null && lastBtnUsed != newBtn){
                            lastBtnUsed.gotoAndStop(1);
                        }
                        lastBtnUsed = newBtn;
                        newBtn.gotoAndStop(2);
                    }

                    • 7. Re: Script to make button stay on hit state until another is clicked!
                      janineabc Level 1

                      Hi,

                       

                      Ive got it working when they are not in another movie clip, However when I put them in main_mc. I carnt get it to work.

                       

                      Here is the script I have used below.

                       

                      Thanks

                       

                      var lastBtnUsed;

                       

                      main_mc.btn1.onRelease = function(){

                          processBtns(main_mc.btn1);

                       

                      }

                       

                      main_mc.btn2.onRelease = function(){

                          processBtns(main_mc.btn2);

                      }

                       

                      main_mc.btn3.onRelease = function(){

                          processBtns(main_mc.btn3);

                      }

                       

                      main_mc.btn4.onRelease = function(){

                          processBtns(main_mc.btn4);

                      }

                       

                      main_mc.btn5.onRelease = function(){

                          processBtns(main_mc.btn5);

                      }

                       

                      function processBtns(newBtn){

                          if(lastBtnUsed != null && lastBtnUsed != newBtn){

                              lastBtnUsed.gotoAndStop(1);  // reset current highlighted button

                          }

                          newBtn.gotoAndStop(2);  // highlight new button

                          lastBtnUsed = newBtn;

                      }

                      • 8. Re: Script to make button stay on hit state until another is clicked!
                        janineabc Level 1

                        ooo Its ok I've fixed it.

                         

                        Changed the script to the condensed version you sent and added in the movie clip and it works perfectly.

                         

                        Thanks allot for your help!

                        • 9. Re: Script to make button stay on hit state until another is clicked!
                          janineabc Level 1

                          Hi, sorry

                           

                          just as an addition to this...If its possible...Is there anyway that I can write in the script to highlight a certain button upon loading? So that button one is showing the 2nd frame when you first load the webpage???

                           

                          Don't know if this is possible, but would just be the finishing touches to the effect.

                           

                          Thanks

                          • 10. Re: Script to make button stay on hit state until another is clicked!
                            Ned Murphy Adobe Community Professional & MVP

                            Try...

                             

                            var lastBtnUsed = btn1;

                            btn1.gotoAndStop(2);

                             

                            btn1.onRelease = btn2.onRelease = btn3.onRelease = btn4.onRelease = function(){
                                processBtns(this);
                            }

                             

                            function processBtns(newBtn){
                                if(lastBtnUsed != null && lastBtnUsed != newBtn){
                                    lastBtnUsed.gotoAndStop(1);
                                }
                                lastBtnUsed = newBtn;
                                newBtn.gotoAndStop(2);
                            }

                             

                             

                             

                             

                             

                            • 11. Re: Script to make button stay on hit state until another is clicked!
                              janineabc Level 1

                              Hi, Thanks for this.

                               

                              It works perfectly in one section that I have used it in, however I have a small problem in the 2nd section. As my buttons are working as a sub menu to the main.

                               

                              so I have

                               

                              Main01

                              main02

                               

                                sub01

                                sub02

                                sub03

                                sub04

                                sub05

                               

                              so when I loads up main02 is highlighted which works perfectly. And then sub01 is highlighted. However If I go to another button say sub05 and then go to main 01, which is a profile page which load over the top of whichever page you are on (also makes the submenu disappear) Then I select to go back to main02 it automatically says that I am on Sub01 as it reloads the sub menu, when in actual fact the page being shown is sub05 the last one select.

                               

                              Don't know if that makes any sense or not, have tried to explain in the simplest terms possible!

                               

                              May have to give up with the submenu highlighting at the start in order to make it work and just have it on the main section! and when one is actually selected.

                               

                              Thanks