4 Replies Latest reply: Jun 19, 2012 11:01 AM by kglad RSS

    Need to loop to certain point on mouse-click

    ekmaust

      Hello,

      for work I am trying to create a visual for a simple electric circuit. What I want is that when the circuit opening is clicked, it closes and the light bulb comes on. When it is clicked again, it opens and the light bulb turns off.

       

      Untitled.png

       


      I have it working fine when I try to play it one time through. However, I do not know how to send it back to the beginning and run through the entire timeline again.

       

      On frame 1 I have this actionscript:

      stop();

       

      movieClip_1.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlayFromFrame_3);

      function fl_ClickToGoToAndPlayFromFrame_3(event:MouseEvent):void
      {
      gotoAndPlay(2);
      }

       

       

      This plays till frame 11, which has the script:

       

       

      movieClip_1.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlayFromFrame);

      function fl_ClickToGoToAndPlayFromFrame(event:MouseEvent):void
      {
      gotoAndPlay(13);
      }

      stop();

       

       

      It then plays till the end (frame 24), where I put in the script:

       

      movieClip_1.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlayAtFrame);

      function fl_ClickToGoToAndPlayAtFrame(event:MouseEvent):void
      {
      gotoAndPlay(2);
      }

       

       

      Everything works great, even when it is sent back to frame 2. The problem is that when it reaches 11 again, clicking the mouse always sends it back to 2 instead of on to 13.

      I hope this makes sense, I don't really know any better ways to explain it. It's probably a simple fix, but I'm new to actionscript and this has been driving me crazy. Any help would be appreciated.

      Thanks,

      Emily

        • 1. Re: Need to loop to certain point on mouse-click
          Ned Murphy MVP

          When you keep adding event listeners to the same object, they all stay, which is probably causing problems for you.  What you should do is only assign the event listener once in frame 1 and only define the event handler function once there as well.  In the event handler function use a variable for the frame that you go to, and change the value of that variable in the different frame that you stop in.

          • 2. Re: Need to loop to certain point on mouse-click
            kglad MVP

            use:

             

             

            On frame 1 I have this actionscript:

            stop();

             

            movieClip_1.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlayFromFrame_3);

            function fl_ClickToGoToAndPlayFromFrame_3(event:MouseEvent):void
            {
            gotoAndPlay(2);
            }

             

             

            This plays till frame 11, which has the script:

             

             

            movieClip_1.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlayFromFrame);

            function fl_ClickToGoToAndPlayFromFrame(event:MouseEvent):void
            {
            gotoAndPlay(13);
            }

            stop();

             

             

            It then plays till the end (frame 24), where I put in the script:

             

            movieClip_1.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlayAtFrame24);

            function fl_ClickToGoToAndPlayAtFrame24(event:MouseEvent):void
            {
            gotoAndPlay(2);
            }

             

             

            Everything works great, even when it is sent back to frame 2. The problem is that when it reaches 11 again, clicking the mouse always sends it back to 2 instead of on to 13.

            I hope this makes sense, I don't really know any better ways to explain it. It's probably a simple fix, but I'm new to actionscript and this has been driving me crazy. Any help would be appreciated.

            Thanks,

            Emily

            • 3. Re: Need to loop to certain point on mouse-click
              ekmaust Community Member

              I'm not quite sure what I'm doing, but this is what I have so far:

              in frame 1:

               

              stop();
              {
              movieClip_1.addEventListener(MouseEvent.CLICK,event_handler);

              function event_handler(event:MouseEvent):void
              {
              var i:int = this.currentFrame;
              {
              if (1)
              gotoAndPlay(2);
              }
              {
              if (11)
              gotoAndPlay(12);
              }
              {
              if (24)
              gotoAndPlay(2);
              }
              }
              }

               

              I also have the stop(); command in frames 11 and 24.

              The issue I'm having now is that, when clicked, the animation always plays frames 2-11. It seems to only recognize the first variable value and none of the others.

              • 4. Re: Need to loop to certain point on mouse-click
                kglad MVP

                use:

                 

                 

                 

                stop();
                {
                movieClip_1.addEventListener(MouseEvent.CLICK,event_handler);

                function event_handler(event:MouseEvent):void
                {
                var i:int = this.currentFrame;
                {
                if (i==1)
                gotoAndPlay(2);
                }
                {
                if (i==11)
                gotoAndPlay(12);
                }
                {
                if (i==24)
                gotoAndPlay(2);
                }
                }
                }