Skip to main content
Participant
Aug 7, 2017 8:13 PM UTC
Answered

How do I play and stop a movie clip with a single button?

Using HTML5 canvas.

I have a movie clip (mcTurn) on my timeline that plays in a loop and I have a button (button_1) that I want to use to control the movie clip with a play/stop function.

So far I got the movie clip to stop by using the following script:

this.button_1.addEventListener("click", stopAnim.bind(this));

function stopAnim()

{

  this.mcTurn.stop();

}

But how do I add a play function to this code? I want the movie to resume playing again when clicked.

Back in the day I used the on(press) and on(release) functions for this, so I'm a little behind... I am not well-versed with action script 3 nor with HTML5 canvas functions.

Thank you very much!

    This topic is closed to new replies. Start a new post to keep the conversation going.
    ClayUUIDCorrect answer
    Legend
    Aug 7, 2017 8:59 PM UTC

    Of course that doesn't work, it's AS3 code. Try this instead:

    this.button_1.addEventListener("click", stopStartAnim.bind(this));

    function stopStartAnim() {

        var clip = this.mcTurn;

        if (clip.paused) {

            clip.play();

        }

        else {

            clip.stop();

        }

    }


    Or this pointlessly clever version:

    this.button_1.addEventListener("click", stopStartAnim.bind(this));

    function stopStartAnim() {

        var clip = this.mcTurn;

        clip[clip.paused ? "play" : "stop"]();

    }

    22 replies

    Participant
    Aug 21, 2024 4:08 PM UTC

    To add the play function, you can toggle between play and stop like this:

    ```javascript
    this.button_1.addEventListener("click", toggleAnim.bind(this));

    function toggleAnim() {
    if (this.mcTurn.isPlaying) {
    this.mcTurn.stop();
    this.mcTurn.isPlaying = false;
    } else {
    this.mcTurn.play();
    this.mcTurn.isPlaying = true;
    }
    }
    ```

    This will allow your button to both play and stop the movie clip.

    Translate
    Inspiring
    Aug 7, 2017 10:49 PM UTC

    Here's another clever solution that let's you reuse the function for different movieclips. The EventTogglePlaying function returns another function, which get triggered by the click event.

    this.button_1.addEventListener("click", EventTogglePlaying(this.mcTurn));

    function EventTogglePlaying (mc) {

         return function () {

              mc.paused? mc.play() : mc.stop();

         }

    }

    Translate
    Legend
    Aug 7, 2017 10:56 PM UTC

    Now you get to explain closures to everyone.

    Translate
    Inspiring
    Aug 7, 2017 11:20 PM UTC

    What do you mean by closures in this case?

    Translate
    Colin Holgate
    Inspiring
    Aug 7, 2017 8:25 PM UTC

    Can you read through this discussion, the last messages should be the most useful:

    https://forums.adobe.com/message/9748910?et=watches.email.outcome#9748910

    Translate
    Participant
    Aug 7, 2017 8:43 PM UTC

    Thanks for your reply Colin, I did try this but didn't work:

    var playing:Boolean = false;

    mcTurn.stop();

    function startmcTurn(event:MouseEvent):void

    {

        if(mcTurn.currentFrame == mcTurn.totalFrames) {

             playing = false;

       }

        if(playing){

               mcTurn.stop();

        } else {

               mcTurn.play();

        }

        playing = !playing;

    }

    button_1.addEventListener(MouseEvent.CLICK, startmcTurn);

    Translate
    Legend
    Aug 7, 2017 8:49 PM UTC

    Of course that doesn't work, it's AS3 code. Try this instead:

    this.button_1.addEventListener("click", stopStartAnim.bind(this));

    function stopStartAnim() {

        var clip = this.mcTurn;

        if (clip.paused) {

            clip.play();

        }

        else {

            clip.stop();

        }

    }

    Translate