Skip to main content
Participant
July 15, 2020
Answered

Help with making a button script

  • July 15, 2020
  • 2 replies
  • 486 views

I need help for a Project, I want to make a button when pressed will go to another frame example it's at frame 20 and when pressed goes straight to frame 21 but if it's not pressed it keeps looping example once it gets to frame 20 it goes back to frame 10 and keeps repeating until pressed.

    This topic has been closed for replies.
    Correct answer JoãoCésar17023019

    Hi.

     

    If I'm understanding what you want, you need to store the final frame of your animation in a property or variable. Then when the user presses the button, you change the value of this property or variable. Like this:

     

    HTML5 Canvas:

    Main timeline / frame 1 (0):

    var root = this;
    
    root.endFrame = 9; // in HTML5 documents the first frame index is 0 so the 10th frame index is 9
    
    root.stopLoop = function(e)
    {
    	root.endFrame = 19;
    };
    
    root.yourButton.on("click", root.stopLoop);

     

    Main timeline / frame 10 (9):

    this.gotoAndPlay(this.endFrame);

     

    AS3:

    Main timeline / frame 1:

    import flash.events.MouseEvent;
    
    var endFrame:uint = 10;
    
    function stopLoop(e:MouseEvent):void
    {
    	endFrame = 20;
    }
    
    yourButton.addEventListener(MouseEvent.CLICK, stopLoop);

     

    Main timeline / frame 10:

    gotoAndPlay(endFrame);

     

    Please let us know if this is what you want.

     

    Regards,

    JC

    2 replies

    JoãoCésar17023019
    Community Expert
    JoãoCésar17023019Community ExpertCorrect answer
    Community Expert
    July 16, 2020

    Hi.

     

    If I'm understanding what you want, you need to store the final frame of your animation in a property or variable. Then when the user presses the button, you change the value of this property or variable. Like this:

     

    HTML5 Canvas:

    Main timeline / frame 1 (0):

    var root = this;
    
    root.endFrame = 9; // in HTML5 documents the first frame index is 0 so the 10th frame index is 9
    
    root.stopLoop = function(e)
    {
    	root.endFrame = 19;
    };
    
    root.yourButton.on("click", root.stopLoop);

     

    Main timeline / frame 10 (9):

    this.gotoAndPlay(this.endFrame);

     

    AS3:

    Main timeline / frame 1:

    import flash.events.MouseEvent;
    
    var endFrame:uint = 10;
    
    function stopLoop(e:MouseEvent):void
    {
    	endFrame = 20;
    }
    
    yourButton.addEventListener(MouseEvent.CLICK, stopLoop);

     

    Main timeline / frame 10:

    gotoAndPlay(endFrame);

     

    Please let us know if this is what you want.

     

    Regards,

    JC

    Riley3Author
    Participant
    July 17, 2020

    Thank you so much this is what I needed

    JoãoCésar17023019
    Community Expert
    Community Expert
    July 17, 2020

    You're welcome!

    Joseph Labrecque
    Community Expert
    Community Expert
    July 15, 2020

    Did you also stop the timeline? If looping until you press the button you likely haven't stopped the timeline previous to that. Maybe I'm not understanding the issue though - please provide visuals or a code example.

    Riley3Author
    Participant
    July 15, 2020

    I'm still new to animate and learning how to use code but all I want to do is create a loop in my timeline and have a button when pressed will stop the loop  and continue playing the timeline 

    Riley3Author
    Participant
    July 16, 2020

    So when I start playing my animation once it reaches frame 20 it will loop back to frame 10 and keep playing and looping until the user pressed the button undoing the loop and playing what's after frame 20. Because I'm making a game where you have to warn the character by clicking on it and if you don't warn it, it will get hit by a car and then loop back again to the start of the scene and keep playing and once you warn the character it will stop the loop and take you to another frame where it shows him stopping evading the car. It's like the game Crossy road