4 Replies Latest reply on Feb 23, 2011 2:09 AM by Moll O' Moon

    My AS3 buttons only work the first time

    Moll O' Moon



      I have set up my buttons so that are each a movieclip with 'over' and 'out' states/frame labels. They exist on the main timeline. The actionscript controlling them is situated in a frame on the main timeline and the frame holding that actionscript is on the same point within the main timeline where the buttons are introduced. I am using actionscript 3.


      The buttons have been set up so that they navigate to another frame on the same main timeline and on that frame is a movie. The movie dimensions are smaller than the main stage so the buttons are still accessible when each movie is played.


      The problem is my buttons are only working once - by that I mean they go to the correct place when clicked the first time, but when they are clicked again they navigate elsewhere.


      Totally doing my head in trying find a solution and if anyone can help and show me what I'm doing wrong that would be really really helpful. Here's the code I used for the buttons...




      function mainBtnOver(event:MouseEvent):void {
      function mainBtnOut(event:MouseEvent):void {


      function mainBtn1Down(event:MouseEvent):void {
      function mainBtn2Down(event:MouseEvent):void {
      function mainBtn3Down(event:MouseEvent):void {
      function mainBtn4Down(event:MouseEvent):void {
      function mainBtn5Down(event:MouseEvent):void {


      ////////// Button 1 Listeners
      mainBtn1.addEventListener(MouseEvent.ROLL_OVER, mainBtnOver);
      mainBtn1.addEventListener(MouseEvent.ROLL_OUT, mainBtnOut);
      mainBtn1.addEventListener(MouseEvent.CLICK, mainBtn1Down);
      ////////// Button 2 Listeners
      mainBtn2.addEventListener(MouseEvent.ROLL_OVER, mainBtnOver);
      mainBtn2.addEventListener(MouseEvent.ROLL_OUT, mainBtnOut);
      mainBtn2.addEventListener(MouseEvent.CLICK, mainBtn2Down);
      ////////// Button 3 Listeners
      mainBtn3.addEventListener(MouseEvent.ROLL_OVER, mainBtnOver);
      mainBtn3.addEventListener(MouseEvent.ROLL_OUT, mainBtnOut);
      mainBtn3.addEventListener(MouseEvent.CLICK, mainBtn3Down);
      ////////// Button 4 Listeners
      mainBtn4.addEventListener(MouseEvent.ROLL_OVER, mainBtnOver);
      mainBtn4.addEventListener(MouseEvent.ROLL_OUT, mainBtnOut);
      mainBtn4.addEventListener(MouseEvent.CLICK, mainBtn4Down);
      ////////// Button 5 Listeners
      mainBtn5.addEventListener(MouseEvent.ROLL_OVER, mainBtnOver);
      mainBtn5.addEventListener(MouseEvent.ROLL_OUT, mainBtnOut);
      mainBtn5.addEventListener(MouseEvent.CLICK, mainBtn5Down);


      I'm sure it's me doing something very stupid - I'm very new to AS3. Any help much appreciated.


      Thanks :-)

        • 1. Re: My AS3 buttons only work the first time
          somascope Level 3

          If the frame labels you are going to have movie clips that have your presentation content, you should use gotoAndStop("frameLabel") instead of gotoAndPlay("frameLabel"). There's no need to play the main timeline when you go to and show any of these movie clips. I'm not sure if this is your issue, though... Using your code, I had no problems.


          Also, you can set up your animated buttons in a way that eliminates the need to have your ROLL_OVER and ROLL_OUT events. This isn't necessarily better, but just another method. Although I might use other ways to animate a button, I do like to reduce the code it takes to use the buttons in an interface, and only use CLICK in most cases.


          • 2. Re: My AS3 buttons only work the first time
            Ned Murphy Adobe Community Professional & MVP

            How are your buttons situated in the timeline?  Do you leave the frames where they exist and return to them later on only to find they don't work, or are they always present?  Your buttons should only have one keyframe in their layer and that layer should extend the full length of the timeline where the buttons are needed.  That way the code assigned in their first frame remains in effect throughout the timeline.

            1 person found this helpful
            • 3. Re: My AS3 buttons only work the first time
              Moll O' Moon Level 1

              Thank you so much Somascope - you've completely saved the day . I changed gotoAndPlay("frameLabel") to gotoAndStop("frameLabel") and that has fixed the problem.


              Thanks again

              • 4. Re: My AS3 buttons only work the first time
                Moll O' Moon Level 1

                Thanks for looking into this too Ned - really really appreciate it. The buttons and actionscript exist at the same point on the timeline. The buttons are only sitting on the one keyframe that runs the full length of the timeline. I was worried about the actionscript sitting on its one lonely keyframe as there's no visual reference on the timeline saying that it's still there (later on in timeline) in the way a physical stage element shows itself on the timeline.


                Your mention - 'That way the code assigned in their first frame remains in effect throughout the timeline' is a very very useful tip.


                Thanks again