Search code examples
flashactionscriptactionscript-2flash-cs3

toggle play/pause button


Im new to this flash and I am using as2 for the action script I wanted to know if there are any good tutorials on how to create a toggle button so far this is all I have.

on (release) {
play ();
}

on (release) {
stop ();
}

I wanted so that when you hit playit would start the animation but showing the pause button and vice versa.


Solution

  • You need to keep track of the state of your toggle: ie whether the animation is playing or not when the button is pressed.

    So the code on your button might be:

    on (release){
    _root.toggleMe();//assuming you want to start/stop the main timeline
    }
    

    And then in the main timeline, you could define the toggleMe() function like this:

    var isPlaying:Boolean = false; // track state of animation - paused to start
    
    function toggleMe():Void {
        if (isPlaying) {
            stop();
            isPlaying = false;
        } else {
            play();
            isPlaying = true;
        }
    }
    
    stop();
    

    [EDIT: changed the code to control the main timeline]