Trouble with restarting scene.

I have a restart  button that resets the entire scene except these apples that are falling from the sky, the code for them is as follows.

-- Insert knife io.output():setvbuf("no") -- Don't use buffer for console messages display.setStatusBar(display.HiddenStatusBar) local allTmp = {} local physics = require("physics") physics.start() physics.setGravity(0,9.8) --physics.setDrawMode( "hybrid" local function onTouch( self, event ) allTmp[self] = nil display.remove(self) return true end local function createKnife( ) -- Randomly create one of five knife images local imgNum = math.random( 1, 5 ) local tmp = display.newImage( "knife.png", 295/5, 482/5 ) tmp.myName = "tmp" -- Randomly place the knife tmp.y = -50 tmp.x = math.random( 50, 430 ) -- Scale it to make a 'smaller' balloon --tmp:scale( 0.1, 0.1 ) tmp:scale(0.3,0.3) -- add a touch listener -- Give it a body so 'gravity' can pull on it physics.addBody( tmp, { radius = tmp.contentWidth/2, bounce=1.5 } ) -- Give the body a random rotation tmp.angularVelocity = math.random( -180, 180 ) -- Give it drag so it doesn't accelerate too fast tmp.linearDamping = 1 allTmp.bounce = 10 -- Self destruct in 5 seconds timer.performWithDelay( 100000, function() allTmp[tmp] = nil display.remove( tmp ) end ) end

But when I use the code local.sceneGroup = self.view in the destroy section and click restart, it restarts everything except the apples in that code, instead it makes it double so how do I reset it completely so that it is the same falling pace and frequency every time?

On the code it says knife but I changed it to apples as an image in the actual game but never changed the code.

Basically how do i reset everything completely, only the code above doesn’t seem to reset instead it doubles.

This code isn’t relevant. We need to know how you are calling createKnife. Is it a timer, an enterFrame listener? The likelihood is that you haven’t cancelled them when exiting the scene.

Agree with nick,

@Zain Ali post the code in formatted way & post complete proper block code, so that anyone can understand clearly.

*** If you are using runtime listener / timer, make sure to stop/cancel it - before you go for reset.

-Assif

It is a timer, the code i sent was the complete code that makes it work but I checked and it is a timer so how do I cancel it when exiting the scene? I can’t find the solution anywhere.

Store all the timers ref in timerX table as :

[lua]

timerX[#timerX+1]=timer.performWithDelay(1000,function()  end)

[/lua]

Now once you store all the timers ref in timerX table, you can cancel all the timers at once using following block in hide phase of that scene.

[lua]

function scene:hide( event )

    sceneGroup = self.view

    local phase = event.phase

    if ( phase == “will” ) then

       

        for i=1,#timerX do

            timer.cancel(timerX[i])

            timerX[i]=nil

        end  

    elseif ( phase == “did” ) then

        composer.removeScene(“Your Scene Name”)

    end

end

[/lua]

-Assif

It doesn’t work it just says relaunch not even edit code.

Post the entire Scene code & Elaborate your problem .

The code I sent first is the only code that is relevant to the falling apples, nothing else applies to it but I will send the bit beneath it which may help.

-- Insert knife io.output():setvbuf("no") -- Don't use buffer for console messages display.setStatusBar(display.HiddenStatusBar) local allTmp = {} local physics = require("physics") physics.start() physics.setGravity(0,9.8) --physics.setDrawMode( "hybrid" local function onTouch( self, event ) allTmp[self] = nil display.remove(self) return true end local function createKnife( ) -- Randomly create one of five knife images local imgNum = math.random( 1, 5 ) local tmp = display.newImage( "knife.png", 295/5, 482/5 ) tmp.myName = "tmp" -- Randomly place the knife tmp.y = -50 tmp.x = math.random( 50, 430 ) -- Scale it to make a 'smaller' balloon --tmp:scale( 0.1, 0.1 ) tmp:scale(1,1) -- add a touch listener -- Give it a body so 'gravity' can pull on it physics.addBody( tmp, { radius = tmp.contentWidth/2, bounce=1.5 } ) -- Give the body a random rotation tmp.angularVelocity = math.random( -180, 180 ) -- Give it drag so it doesn't accelerate too fast tmp.linearDamping = 1 allTmp.bounce = 10 -- Self destruct in 5 seconds timer.performWithDelay( 100000, function() allTmp[tmp] = nil display.remove( tmp ) end ) end -- Create a new knife every 1/2 second forever timer.performWithDelay( 3500, createKnife, -1 ) playBtn = widget.newButton{ label="Restart", labelColor = { default={0}, over={100} }, default="button.png", over="button-over.png", width=154, height=40, onRelease = onPlayBtnRelease -- event listener function } playBtn.x = display.contentCenterX playBtn.y = display.contentHeight - 7 -- all display objects must be inserted into group sceneGroup:insert( timeDisplay ) end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen elseif phase == "did" then -- Called when the scene is now on screen -- -- INSERT code here to make the scene come alive -- e.g. start timers, begin animation, play audio, etc. end end function scene:hide( event ) local sceneGroup = self.view physics.pause() local phase = event.phase if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) physics.stop() elseif phase == "did" then physics.pause() -- Called when the scene is now off screen end end function scene:destroy( event ) -- Called prior to the removal of scene's "view" (sceneGroup) -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc. local sceneGroup = self.view physics.pause() package.loaded[physics] = nil physics = nil end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) ----------------------------------------------------------------------------------------- return scene

“I can’t find the solution anywhere”.

Seriously, I have to ask, have you ever heard of google?

A quick search for ‘cancel timer corona’ yields exactly the information you need.

Start your timer using:

local knifeTimer = timer.performWithDelay( 3500, createKnife, -1 )

Cancel it using:

timer.cancel(knifeTimer)

Sorry, last question, where do I add timer.cancel(knifeTimer) ???

@Zain ,

Kindly please read composer & Scene concept first - https://docs.coronalabs.com/api/library/composer/index.html

Hope you will correct first the missing required things for a scene that you have posted !!!

Too many mistakes in code :

  • No composer lib required

  • under show nothing called

  • where is create used

  • No declaration for sceneGroup

Please update & then resend.

-Assif

create is above the code and so is the composer, and the scene group thing works with just the self view so i left it like that, I will read the link. Thank you for being nice.

@Zain , Nice to hear !!!

Post complete code or simply attach scene.lua file !!!

Thus its become easy to debug for anyone.

-Assif

This code isn’t relevant. We need to know how you are calling createKnife. Is it a timer, an enterFrame listener? The likelihood is that you haven’t cancelled them when exiting the scene.

Agree with nick,

@Zain Ali post the code in formatted way & post complete proper block code, so that anyone can understand clearly.

*** If you are using runtime listener / timer, make sure to stop/cancel it - before you go for reset.

-Assif

It is a timer, the code i sent was the complete code that makes it work but I checked and it is a timer so how do I cancel it when exiting the scene? I can’t find the solution anywhere.

Store all the timers ref in timerX table as :

[lua]

timerX[#timerX+1]=timer.performWithDelay(1000,function()  end)

[/lua]

Now once you store all the timers ref in timerX table, you can cancel all the timers at once using following block in hide phase of that scene.

[lua]

function scene:hide( event )

    sceneGroup = self.view

    local phase = event.phase

    if ( phase == “will” ) then

       

        for i=1,#timerX do

            timer.cancel(timerX[i])

            timerX[i]=nil

        end  

    elseif ( phase == “did” ) then

        composer.removeScene(“Your Scene Name”)

    end

end

[/lua]

-Assif

It doesn’t work it just says relaunch not even edit code.

Post the entire Scene code & Elaborate your problem .

The code I sent first is the only code that is relevant to the falling apples, nothing else applies to it but I will send the bit beneath it which may help.

-- Insert knife io.output():setvbuf("no") -- Don't use buffer for console messages display.setStatusBar(display.HiddenStatusBar) local allTmp = {} local physics = require("physics") physics.start() physics.setGravity(0,9.8) --physics.setDrawMode( "hybrid" local function onTouch( self, event ) allTmp[self] = nil display.remove(self) return true end local function createKnife( ) -- Randomly create one of five knife images local imgNum = math.random( 1, 5 ) local tmp = display.newImage( "knife.png", 295/5, 482/5 ) tmp.myName = "tmp" -- Randomly place the knife tmp.y = -50 tmp.x = math.random( 50, 430 ) -- Scale it to make a 'smaller' balloon --tmp:scale( 0.1, 0.1 ) tmp:scale(1,1) -- add a touch listener -- Give it a body so 'gravity' can pull on it physics.addBody( tmp, { radius = tmp.contentWidth/2, bounce=1.5 } ) -- Give the body a random rotation tmp.angularVelocity = math.random( -180, 180 ) -- Give it drag so it doesn't accelerate too fast tmp.linearDamping = 1 allTmp.bounce = 10 -- Self destruct in 5 seconds timer.performWithDelay( 100000, function() allTmp[tmp] = nil display.remove( tmp ) end ) end -- Create a new knife every 1/2 second forever timer.performWithDelay( 3500, createKnife, -1 ) playBtn = widget.newButton{ label="Restart", labelColor = { default={0}, over={100} }, default="button.png", over="button-over.png", width=154, height=40, onRelease = onPlayBtnRelease -- event listener function } playBtn.x = display.contentCenterX playBtn.y = display.contentHeight - 7 -- all display objects must be inserted into group sceneGroup:insert( timeDisplay ) end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen elseif phase == "did" then -- Called when the scene is now on screen -- -- INSERT code here to make the scene come alive -- e.g. start timers, begin animation, play audio, etc. end end function scene:hide( event ) local sceneGroup = self.view physics.pause() local phase = event.phase if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) physics.stop() elseif phase == "did" then physics.pause() -- Called when the scene is now off screen end end function scene:destroy( event ) -- Called prior to the removal of scene's "view" (sceneGroup) -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc. local sceneGroup = self.view physics.pause() package.loaded[physics] = nil physics = nil end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) ----------------------------------------------------------------------------------------- return scene