Pausing and Resuming ' Runtime:addEventListener("enterFrame", object ) '

Hello,

Building a simple scene with a few buttons (pause, and play) on the screen. 

Set up:  There is Channel 1 set up in the main.lua for the background music.

My objective: To accomplish the following with the ‘Pause’ and ‘Play’ buttons:

            First:  ‘pause’ button stops -> walking character, background music, grass, and the yellow sign

            Second: ‘play’ button resumes -> walking character, background music, grass, and the yellow sign

SO FAR, I have been able to do accomplish the following:

   ‘Pause’ button to stop the Walking Character and the Background Music

   ‘Play’ button will resume the Walking Character

I need Help for the following:

   ‘Pause’ button to stop the moving Grass, Yellow Sign

   ‘Play’ button to resume Background Music, moving Grass, and Yellow Sign

** A Screen Shot of the scene is attached

     Q1 - How Do I stop and resume the following:

                 grassGreen1.enterFrame = scrollGrass

                 Runtime:addEventListener(“enterFrame”, grassGreen1 )

                grassGreen2.enterFrame = scrollGrass

                Runtime:addEventListener(“enterFrame”, grassGreen2 )

                YellowSign.enterFrame = scrollGrass

                timer.performWithDelay( 5000, startAcc )

Below is my code:

local composer = require( “composer” )

local scene = composer.newScene()


– Code outside of the scene event functions below will only be executed ONCE unless

– the scene is removed entirely (not recycled) via “composer.removeScene()”


local musicTrack 

local grassGreen1

local grassGreen2

local YellowSign

local sheetZero = { width = 673, height = 900, numFrames = 6, sheetContentWidth = 2025, sheetContentHeight = 1804 }

local sheetZero1 = graphics.newImageSheet ( “LeoArt-0.png”, sheetZero )

local sequenceData = {

{ name = “seque0”, sheet = sheetZero1, start = 1, count = 6, time = 500, loopCount = 0, loopDirection = “forward” }

}

local myAnimation = display.newSprite ( sheetZero1, sequenceData )

myAnimation.x = display.contentWidth / 2

myAnimation.y = display.contentHeight / 2

local pausebutton

local playbutton

local introbutton

local introwindow

local function pausebtnf()

myAnimation:pause()

audio.pause( 1 )

end

local function playbtnf()

myAnimation:play()

end

local function intropop ()

  —myAnimation:pause()

    introwindow = display.newImageRect( “intro.png”, display.contentWidth*0.8, display.contentHeight*0.4)

    introwindow.x = display.contentCenterX

    introwindow.y = display.contentCenterY - 190

    local function closewindow ()

      display.remove( introwindow )

      timer.performWithDelay( 4000 , playbtnf() )

    end

   

    introwindow: addEventListener( “tap”, closewindow )

   

 end

local function scrollGrass( self, event )

if self.x < - 640 then

self.x = 640

else

self.x = self.x - self.speed

end

end

local function musicTrackfn ()

  musicTrack = audio.loadStream( “Happysong.wav” )

end

— Code below if for the moving Yellow Sign

local function startAcc()

Runtime:addEventListener(“enterFrame”, YellowSign )

end


– Scene event functions


– create()

function scene:create( event )

– The ‘Create scene’ displays objects used on the screen

– Adding Event Listener to Buttons

– ONLY executed once and when the app is loaded. So even if we leave and come back, images will be where they were last put

– SceneGroup: insert(imageName) -> otherwise image will remain on the screen on same place

local sceneGroup = self.view

– Code here runs when the scene is first created but has not yet appeared on screen

pausebutton = display.newImageRect( “pause.png”, 60, 60)

pausebutton.x = display.contentCenterX + 240

pausebutton.y = display.contentCenterY + 100

sceneGroup:insert ( pausebutton )

playbutton = display.newImageRect( “play.png”, 60, 60)

playbutton.x = display.contentCenterX + 240

playbutton.y = display.contentCenterY + 0

sceneGroup:insert ( playbutton )

introbutton = display.newImageRect( “ibtn.png”, 60, 60)

introbutton.x = display.contentCenterX + 240

introbutton.y = display.contentCenterY - 100

sceneGroup:insert ( introbutton )

YellowSign = display.newImage( “YellowSign.png” )

YellowSign.anchorX = 0; YellowSign.anchorY = 0

YellowSign.x = 750

YellowSign.y = 280 

YellowSign.speed = 3.5

sceneGroup:insert ( YellowSign )

grassGreen1 = display.newImage( “grass.png”)

grassGreen1.anchorX = 0; grassGreen1.anchorY = 0 

grassGreen1.x = 0

grassGreen1.y = 830

grassGreen1.speed = 2.5

sceneGroup:insert ( grassGreen1 )

grassGreen2 = display.newImage( “grass.png”)

grassGreen2.anchorX = 0; grassGreen2.anchorY = 0 

grassGreen2.x = 720

grassGreen2.y = 830

grassGreen2.speed = 2.5

sceneGroup:insert ( grassGreen2 )

grassGreen1.enterFrame = scrollGrass

Runtime:addEventListener(“enterFrame”, grassGreen1 )

grassGreen2.enterFrame = scrollGrass

Runtime:addEventListener(“enterFrame”, grassGreen2 )

YellowSign.enterFrame = scrollGrass

timer.performWithDelay( 5000, startAcc )

myAnimation:play()

myAnimation:toBack()

musicTrackfn()

pausebutton: addEventListener( “tap”, pausebtnf )

playbutton: addEventListener( “tap”, playbtnf )

end

– show()

function scene:show( event )

– Executed every time this scene is shown 

local sceneGroup = self.view

local phase = event.phase

if ( phase == “will” ) then

– Code here runs when the scene is still off screen (but is about to come on screen)

– Use this phase to reposition objects that may have moved during game play

elseif ( phase == “did” ) then

– Code here runs when the scene is entirely on screen

– Use this phase to start timers, animations, audio

introbutton: addEventListener( “tap”, intropop )

audio.play( musicTrack, { channel=1, loops=-1 } )

end

end

– hide()

function scene:hide( event )

local sceneGroup = self.view

local phase = event.phase

if ( phase == “will” ) then

– Code here runs when the scene is on screen (but is about to go off screen)

– This can UNDO all that happened in ‘Scene:Show:Did’

elseif ( phase == “did” ) then

– Code here runs immediately after the scene goes entirely off screen

– This can UNDO all that happened in ‘Scene:Show:Will’

end

end

– destroy()

function scene:destroy( event )

– used for when we have low memory problems

local sceneGroup = self.view

– Code here runs prior to the removal of scene’s view

end


– Scene event function listeners


scene:addEventListener( “create”, scene )

scene:addEventListener( “show”, scene )

scene:addEventListener( “hide”, scene )

scene:addEventListener( “destroy”, scene )


return scene

Have a variable called paused at the top of your lua file. In your enterFrame listeners check this is false before running the code.

Nick’s idea is a great idea and will certainly work. I would also offer up having your pause function remove the runtime listeners and the resume button, add them back.

Rob

Nick…would you please be more specific and give an example?

How would the ‘if’ statement look like?

Rob - Thanks the solution proposed worked. Basically added  the    removeEventListener to the play and pause functions. 

How about the music?

Music is put on Channel 1

There is a function for it:

local function musicTrackfn ()

 

  musicTrack = audio.loadStream( “Happysong.wav” )

 

end

 

 

In the scene:create ( event ) I have  musicTrackfn()

 

In the pause button I have   audio.pause( 1 )

 

How can I resume the background music in the play button?

[lua]

local paused = false  — at top of lua file, set to true when pausing, false when un-pausing

local function scrollGrass( self, event )

      if paused == false then

            if self.x < - 640 then

            self.x = 640

            else

            self.x = self.x - self.speed

            end

      end

end

 [/lua]

The advantage of this method is as you add more and more objects and enterFrame listeners, you don’t need to remember to delete and add their runtime listeners every time you pause and unpause.

Rob - would there be any difference in performance if you had 20 enterFrame listeners, or one enterFrame listener that calls 20 functions?

Thank you Nick. I totally see your point about how it will save form adding or deleting as more objects are added to the scene. Excellent point and time-saver! 

It worked! 

If you call audio.pause(1), you call audio.resume(1) to resume play. See:

https://docs.coronalabs.com/api/library/audio/resume.html

Rob

Have a variable called paused at the top of your lua file. In your enterFrame listeners check this is false before running the code.

Nick’s idea is a great idea and will certainly work. I would also offer up having your pause function remove the runtime listeners and the resume button, add them back.

Rob

Nick…would you please be more specific and give an example?

How would the ‘if’ statement look like?

Rob - Thanks the solution proposed worked. Basically added  the    removeEventListener to the play and pause functions. 

How about the music?

Music is put on Channel 1

There is a function for it:

local function musicTrackfn ()

 

  musicTrack = audio.loadStream( “Happysong.wav” )

 

end

 

 

In the scene:create ( event ) I have  musicTrackfn()

 

In the pause button I have   audio.pause( 1 )

 

How can I resume the background music in the play button?

[lua]

local paused = false  — at top of lua file, set to true when pausing, false when un-pausing

local function scrollGrass( self, event )

      if paused == false then

            if self.x < - 640 then

            self.x = 640

            else

            self.x = self.x - self.speed

            end

      end

end

 [/lua]

The advantage of this method is as you add more and more objects and enterFrame listeners, you don’t need to remember to delete and add their runtime listeners every time you pause and unpause.

Rob - would there be any difference in performance if you had 20 enterFrame listeners, or one enterFrame listener that calls 20 functions?

Thank you Nick. I totally see your point about how it will save form adding or deleting as more objects are added to the scene. Excellent point and time-saver! 

It worked! 

If you call audio.pause(1), you call audio.resume(1) to resume play. See:

https://docs.coronalabs.com/api/library/audio/resume.html

Rob