Making a Pause Button

I am making a Pause button that when pressed will pause the action.  What I want to achieve is pressing the Pause button a second time to restart the action.  Then pause again, start again.  Here is the code that will pause the action.  So when I press again I would like ‘timer.resume(tmr1)’  and ‘physics.start(true)’ to be activated.  Thanks for help!

    onButtonEvent3 = function (event )

        if event.phase == “began” then

               audio.play(s5)

        elseif event.phase == “ended” then

                timer.pause(tmr1)

                physics.pause()

                --timer.resume(tmr1)

                --physics.start(true)

         end

    end

        

    myButton3 = widget.newButton{

        defaultColor = { default={ 255, 0, 0 }, over={  0, 0, 255} },

        id = “btn001”,

        label = “pause”,

        fontSize=10,

        left = 0,

        top = 115,

        width =55, height = 50,

        onEvent = onButtonEvent3

    }

    group:insert(myButton3)

Set a flag to play when you press pause it checks the flag if it’s play then it changes it to pause if it pause them it changes to play and performs the correct play or pause code

Thank you for help!   I’ve never used a flag however I figured it out :slight_smile:  This is what you had in mind?

flag = true

    onButtonEvent3 = function (event )

        if event.phase == “began” then

            

            audio.play(s5)

         elseif    event.phase == “ended” then

             if flag == true then

                 timer.pause(tmr1)

                physics.pause()

                flag=false

            elseif flag==false then

                timer.resume(tmr1)

                 physics.start(true)

                 flag=true

             end

            

        end

    end

That’s it

Set a flag to play when you press pause it checks the flag if it’s play then it changes it to pause if it pause them it changes to play and performs the correct play or pause code

Thank you for help!   I’ve never used a flag however I figured it out :slight_smile:  This is what you had in mind?

flag = true

    onButtonEvent3 = function (event )

        if event.phase == “began” then

            

            audio.play(s5)

         elseif    event.phase == “ended” then

             if flag == true then

                 timer.pause(tmr1)

                physics.pause()

                flag=false

            elseif flag==false then

                timer.resume(tmr1)

                 physics.start(true)

                 flag=true

             end

            

        end

    end

That’s it