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.
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.
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
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.
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.
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.
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