5 Replies Latest reply on Aug 31, 2010 8:14 AM by kirrwed

    Making a button go to a specific frame

    kirrwed Level 1

      I'm having trouble with this.  I'm just practicing for the moment and not expert in flash code at all.  I have a button on frame #1.  On that frame, I used the following code I found on the web (which I tried using both AS2 and AS3):

       

      stop();
      function buttonClick(event:MouseEvent);void{
      gotoAndPlay(2);

       

      I get errors of varying sorts.  What should I use?  AS2 or AS3?  And what code should I assign to the button (or that button's frame?) to get it to go to frame 2 once clicked?

        • 1. Re: Making a button go to a specific frame
          mldb88 Level 2

          For AS2, you would put the stop(); command on the first frame in the main timeline.

          Then you would put the code:

          on(release){

          gotoAndPlay(2);

          }

           

          Inside the button (select the button to add actions to it, you dont need to put this on the frames in the button object)

          • 2. Re: Making a button go to a specific frame
            Ned Murphy Adobe Community Professional & MVP

            The code you show is an incomplete/incorrect attempt to use AS3.  For AS3 you need to assign an instance name to the button and then assign an event listener to the button using the instance name to target it.  The code goes in the frame of the timeline that coincides with the button's frame (preferably on a separate layer for actionscript).  So if you gave the button an instance name of "btn", the complete/correct code for it would be...

             

            stop(); // this is a timeline command, not a button command

             

            btn.addEventListener(MouseEvent.CLICK, buttonClick);

             

            function buttonClick(evt:MouseEvent):void {

                 gotoAndPlay(2);

            }

            1 person found this helpful
            • 3. Re: Making a button go to a specific frame
              kirrwed Level 1

              mldb88, thanks, that worked for AS2.

               

              Ned, your AS3 code works great if I have one button.  If I add a second button either to the same frame on a different layer, or even on a different frame on a different layer, I get the following error:

               

              Description-1021: Duplicate function definition.   Source-function buttonClick(evt.MouseEvent):void {

               

              I had put two buttons on separate layers in frame 1.  On click, the first button is supposed to go to frame 2.  The second button is supposed to go to frame 3. I have adjusted the code for different button instance names and checked that the correct frames to-go-to are in the code.  The error doesn't occur until I add a second button.

               

              In other words, I built the first button and second frame first.  I tested it and it worked fine.  Then I went back and added another layer to frame 1 with a second button and that seems to throw it all off.

              • 4. Re: Making a button go to a specific frame
                Ned Murphy Adobe Community Professional & MVP

                You cannot have the same function names used more than once, so you would be best to have unique functions defined for each button if their actions are unique.  So you might want to have btn1Click, btn2Click, etc... as different functions for different buttons.

                • 5. Re: Making a button go to a specific frame
                  kirrwed Level 1

                  Thanks again, Ned.