Skip navigation
ekmaust
Currently Being Moderated

Need to loop to certain point on mouse-click

Jun 19, 2012 7:44 AM

Tags: #actionscript #loop #mouse_click

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

 
Replies
  • Currently Being Moderated
    Jun 19, 2012 7:48 AM   in reply to ekmaust

    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.

     
    |
    Mark as:
  • kglad
    72,228 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 19, 2012 7:49 AM   in reply to ekmaust

    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

     
    |
    Mark as:
  • kglad
    72,228 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 19, 2012 11:01 AM   in reply to ekmaust

    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);
    }
    }
    }

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points