Composer not removing the button click events while moving to the next scene

Composer not removing the button click events while moving to the next scene.so that the image which comes after the button click is not removed and shows up in scene2 and scene 3.Experts please help. i am new to programming. :slight_smile:

here is the code and project file.

------------------------------------------------------------------------ -- -- scene1.lua -- ------------------------------------------------------------------------ -- Require the widget library local widget = require( "widget" ) -- Use the require function to include the Corona "composer" module so -- we can create a new scene. local composer = require( "composer" ) -- Use composer to create a new scene. local scene = composer.newScene() -- This function is a call-back. When the user taps the button, this -- function will get called. function button1released( event ) print( "Going to scene 2!" ) composer.gotoScene( "scene2", "slideLeft", 800 ) end function habitat1pressed(event) local piclion = display.newImageRect("lion.jpg",275,183) piclion.x = 600 piclion.y = 700 end function habitat2pressed(event) local picbear = display.newImageRect("bear.jpg",275,183) picbear.x = 250 picbear.y = 700 end function scene:create( event ) print( "Scene 1 create!" ) local sceneGroup = self.view local helpText = display.newText( "Scene #1", 380, 10, native.systemFont, 55 ) helpText:setFillColor( 1.0, 1.0, 1.0 ) local button1 = widget.newButton( { width = 300, height = 100, defaultFile = "button\_gray.png", overFile = "button\_gray\_over.png", label = "Next", labelColor = { default = { 1.0, 1.0, 1.0, 1.0 }, }, fontSize = 50, onRelease = button1released } ) button1.x = 600 button1.y = 1100 local habitat1 = widget.newButton( { width = 340, height = 200, defaultFile = "savannah.jpg", onPress = habitat1pressed } ) habitat1.x = 600 habitat1.y = 510 local habitat2 = widget.newButton( { width = 340, height = 200, defaultFile = "polar.jpg", onPress = habitat2pressed } ) habitat2.x = 250 habitat2.y = 510 sceneGroup:insert( habitat2 ) sceneGroup:insert( habitat1 ) sceneGroup:insert( button1 ) sceneGroup:insert( helpText ) end function scene:show( event ) local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen. print("Scene 1 will show.") display.setDefault( "background", 0, 0, 0 ) 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. print("Scene 1 did show.") end end function scene:hide( event ) local phase = event.phase if 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.) print("Scene 1 will hide.") elseif phase == "did" then -- Called when the scene is now off screen. print("Scene 1 did hide.") end end function scene:destroy( event ) print( "Destroying scene 1's view!" ) end ------------------------------------------------------------------------ -- Add event listeners for all of the scene events we want to get called for! -- https://docs.coronalabs.com/api/type/EventListener/addEventListener.html -- https://docs.coronalabs.com/api/event/scene/index.html scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) ------------------------------------------------------------------------ -- Finally, we return the scene that we just defined so composer can -- make use of it. return scene

For Corona scenes to self clean you must insert your objects into the main scene view.

In scene:create() create yourself a displayGroup (forward declare it at the top of your code) and insert that intoĀ  sceneGroup.

Any objects you make insert them into that displayGroup. Ā Then Corona scene manager will be able to clean it for you.

Thank You sir for the response.

I have created a new display group in scene:create

local group = display.newGroup()

Then inserted the object at the top of the code.

group:insert( piclion)

but the error coming is ā€œscene1.lua:32: attempt to index global ā€˜groupā€™ (a nil value)ā€

That is because you havenā€™t forward declared it. Ā Put this as the first line

local group

then this in scene:create

group = display.newGroup()

Ā the console stopped showing errors but the child object are still displaying in scene2

 scene1.lua -- ------------------------------------------------------------------------ local group -- Require the widget library local widget = require( "widget" ) -- Use the require function to include the Corona "composer" module so -- we can create a new scene. local composer = require( "composer" ) -- Use composer to create a new scene. local scene = composer.newScene() -- This function is a call-back. When the user taps the button, this -- function will get called. function button1released( event ) print( "Going to scene 2!" ) composer.gotoScene( "scene2", "slideLeft", 800 ) end local function habitat2pressed(event) picbear = display.newImageRect("bear.jpg",275,183) picbear.x = 250 picbear.y = 700 group:insert( picbear ) end local function habitat1pressed(event) piclion = display.newImageRect("lion.jpg",275,183) piclion.x = 600 piclion.y = 700 group:insert( piclion ) end function scene:create( event ) print( "Scene 1 create!" ) local sceneGroup = self.view group = display.newGroup()
sceneGroup:insert(group)

Is what you are missing. You have to do everything I said for it to work not just bits of it.

Oh it worked!! :)Ā Solved the Problem.Thank You very much sir for the help.

For Corona scenes to self clean you must insert your objects into the main scene view.

In scene:create() create yourself a displayGroup (forward declare it at the top of your code) and insert that intoĀ  sceneGroup.

Any objects you make insert them into that displayGroup. Ā Then Corona scene manager will be able to clean it for you.

Thank You sir for the response.

I have created a new display group in scene:create

local group = display.newGroup()

Then inserted the object at the top of the code.

group:insert( piclion)

but the error coming is ā€œscene1.lua:32: attempt to index global ā€˜groupā€™ (a nil value)ā€

That is because you havenā€™t forward declared it. Ā Put this as the first line

local group

then this in scene:create

group = display.newGroup()

Ā the console stopped showing errors but the child object are still displaying in scene2

 scene1.lua -- ------------------------------------------------------------------------ local group -- Require the widget library local widget = require( "widget" ) -- Use the require function to include the Corona "composer" module so -- we can create a new scene. local composer = require( "composer" ) -- Use composer to create a new scene. local scene = composer.newScene() -- This function is a call-back. When the user taps the button, this -- function will get called. function button1released( event ) print( "Going to scene 2!" ) composer.gotoScene( "scene2", "slideLeft", 800 ) end local function habitat2pressed(event) picbear = display.newImageRect("bear.jpg",275,183) picbear.x = 250 picbear.y = 700 group:insert( picbear ) end local function habitat1pressed(event) piclion = display.newImageRect("lion.jpg",275,183) piclion.x = 600 piclion.y = 700 group:insert( piclion ) end function scene:create( event ) print( "Scene 1 create!" ) local sceneGroup = self.view group = display.newGroup()
sceneGroup:insert(group)

Is what you are missing. You have to do everything I said for it to work not just bits of it.

Oh it worked!! :)Ā Solved the Problem.Thank You very much sir for the help.

Hi, Iā€™m having a similar problem, but itā€™s removing the Runtime event listener or at least I think thatā€™s what it is.

--Load the composer library which is used for creating scenes/menus local composer = require( "composer" ) local scene = composer.newScene() composer.removeHidden( ) --composer.recycleOnSceneChange = true --Load the physics library local physics = require( "physics" ) --Load table library local table = require( "table" ) local math = require("math") --Set physics and gravity physics.start(true) physics.setGravity( 0, 100 ) --composer.removeScene( "title\_screen" ) --Seed the random number generator math.randomseed( os.time() ) --Load in audio local jumpSound = audio.loadSound( "SFX\_Jump\_04.wav" ) local jumpOptions = { channel = 2, } audio.setVolume( 0.5, { channel = 2 } ) local backgroundMusic = audio.play( "Reaper Music.wav" ) local backgroundOptions = { channel = 1, loop = -1, duration = 150000 } audio.setVolume( 0.3, { channel = 1 } ) --audio.play( backgroundMusic, backgroundOptions ) --Initialize jumpcounter local jumpcounter = 0 --Load the sprite sheet with all the platform information local sheetInfo = require("platforms") local platformSheet = graphics.newImageSheet( "platforms.png", sheetInfo:getSheet() ) --local sprite = display.newSprite( platformSheet, {frames={sheetInfo:getFrameIndex("sprite")}} ) local sheetInfo2 = require("StuffSprite") local decorSheet = graphics.newImageSheet( "StuffSprite.png", sheetInfo2:getSheet() ) --local sprite2 = display.newSprite( decorSheet, {frames={sheetInfo2:getFrameIndex("sprite")}} ) local options = { frames = { --Frame 1 (Reaper1-1) { x = 0, y = 0, width = 80, height = 80, }, --Frame 2 (Reaper1-2) { x = 80, y = 0, width = 80, height = 80, }, --Frame 3 (Reaper1-3) { x = 160, y = 0, width = 80, height = 80, }, --Frame 4 (Reaper1-4) { x = 240, y = 0, width = 80, height = 80, }, --Frame 5 (Reaper2-1) { x = 320, y = 0, width = 80, height = 80, }, --Frame 6 (Reaper2-2) { x = 400, y = 0, width = 80, height = 80, }, --Frame 7 (Reaper2-3) { x = 0, y = 80, width = 80, height = 80, }, --Frame 8 (Reaper2-4) { x = 80, y = 80, width = 80, height = 80, }, --Frame 9 (Reaper3-1) { x = 160, y = 80, width = 88, height = 82, }, --Frame 10 (Reaper3-2) { x = 88, y = 184, width = 88, height = 88, }, --Frame 11 (Reaper3-3) { x = 176, y = 184, width = 88, height = 100, }, --Frame 12 (Reaper3-4) { x = 264, y = 184, width = 80, height = 94, }, --Frame 13 (Reaper3-5) { x = 344, y = 184, width = 88, height = 118, }, --Frame 14 (Reaper3-6) { x = 0, y = 302, width = 88, height = 118, }, --Frame 15 (Reaper3-7) { x = 88, y = 302, width = 88, height = 118, }, --Frame 16 (Reaper3-8) { x = 176, y = 302, width = 88, height = 118, }, --Frame 17 (Reaper3-9) { x = 264, y = 302, width = 88, height = 118, }, --Frame 18 (Reaper3-10) { x = 248, y = 80, width = 88, height = 104, }, --Frame 19 (Reaper3-11) { x = 336, y = 80, width = 100, height = 104, }, --Frame 20 (Reaper3-12) { x = 0, y = 184, width = 88, height = 85, } }, sheetContentWidth = 512, sheetContentHeight = 512 } local player = graphics.newImageSheet( "reapersprite.png", options ) local sequence\_runningReaper = { { name = "running", frames = { 1, 2, 3, 4 }, time = 1000, loopCount = 0, loopDirection = "forward" }, { name = "jump", frames = { 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }, time = 700, loopCount = 1, loopDirection = "forward" } } --function load () --end function scene:create( event ) local sceneGroup = self.view --Load the display physics.setDrawMode( "hybrid" ) local background = display.newImageRect( "1Background.png", 1500, 900 ) background.x = display.contentCenterX background.y = display.contentCenterY sceneGroup:insert( background ) --Shapes for all the platform parts and the reverse of each platform section --local B3shape = { } --local reverseB3shape = { -926,-611, -157,-324 } local score = 0 scoreText2 = display.newText( "Score: ", 11, 50, native.systemFontBold, 80 ) sceneGroup:insert( scoreText2 ) scoreText = display.newText( score, 500, 50, native.systemFontBold, 80 ) sceneGroup:insert( scoreText ) local platformStart = display.newImageRect( platformSheet, 10, 552, 255 ) platformStart.x = display.contentWidth -900 platformStart.y = display.contentHeight -200 sceneGroup:insert( platformStart ) local treeStart = display.newImageRect( decorSheet, 9, 200, 200 ) treeStart.x = platformStart.x - 100 treeStart.y = platformStart.y - 170 sceneGroup:insert( treeStart ) local gravestoneStart = display.newImageRect( decorSheet, 3, 50, 100 ) gravestoneStart.x = platformStart.x + 100 gravestoneStart.y = display.contentHeight -365 sceneGroup:insert( gravestoneStart ) local soulStart = display.newImageRect( decorSheet, 4, 30, 40 ) soulStart.x = platformStart.x + 30 soulStart.y = platformStart.y - 135 sceneGroup:insert( soulStart ) local platform1 = display.newImageRect( platformSheet, 1, 100, 100 ) platform1.x = display.contentWidth -500 platform1.y = display.contentHeight -399 sceneGroup:insert( platform1 ) local platform2 = display.newImageRect( platformSheet, 11, 300, 150 ) platform2.x = display.contentWidth -320 platform2.y = display.contentHeight -393 sceneGroup:insert( platform2 ) local platform3 = display.newImageRect( platformSheet, 1, 100, 100 ) platform3.x = display.contentWidth -140 platform3.y = display.contentHeight -399 platform3:scale( -1, 1 ) sceneGroup:insert( platform3 ) local platform4 = display.newImageRect( platformSheet, 7, 300, 150 ) platform4.x = display.contentWidth +200 platform4.y = display.contentHeight -322 sceneGroup:insert( platform4 ) local platform5 = display.newImageRect( platformSheet, 8, 300, 200 ) platform5.x = display.contentWidth +482 platform5.y = display.contentHeight -391 sceneGroup:insert( platform5 ) local platform6 = display.newImageRect( platformSheet, 6, 100, 100 ) platform6.x = display.contentWidth +680 platform6.y = display.contentHeight -440 sceneGroup:insert( platform6 ) local reaper = display.newSprite( player, sequence\_runningReaper ) reaper:setSequence("running") reaper:play( ) reaper.x = platformStart.x reaper.y = platformStart.y - 165 sceneGroup:insert( reaper ) --local arrow = display.newImageRect( "arrow-landing.png", 100, 100 ) --arrow.x = display.contentCenterX --arrow.y = display.contentCenterY speed = 4 value = 100 --Start with Math calculations local function update( event ) --update background updateBackgrounds() end function updateBackgrounds() score = score + 1 scoreText.text = score --This line of code controls platform movement --Change the number inside parenthesis to change speed higher number mean faster --The minus sign makes the object move to the left platformStart.x = platformStart.x - (speed) soulStart.x = soulStart.x - (speed) treeStart.x = treeStart.x - (speed) gravestoneStart.x = gravestoneStart.x - (speed) platform1.x = platform1.x - (speed) platform2.x = platform2.x - (speed) platform3.x = platform3.x - (speed) platform4.x = platform4.x - (speed) platform5.x = platform5.x - (speed) platform6.x = platform6.x - (speed) --"Moves" the platforms back to the right so they can be run again if ( platformStart.x \< -500 ) then soulStart.alpha = 1 platformStart.x = 1400 end if (gravestoneStart.x \< -500 ) then gravestoneStart.x = platformStart.x + 100 end if ( soulStart.x \< -500 ) then soulStart.x = platformStart.x + 30 end if ( treeStart.x \< -500 ) then treeStart.x = platformStart.x - 100 end if ( platform1.x \< -500 ) then --soul.alpha = 1 platform1.x = platformStart.x + 400 end if ( platform2.x \< -500 ) then --soul.alpha = 1 platform2.x = platform1.x + 200 end if ( platform3.x \< -500 ) then --soul.alpha = 1 platform3.x = platform2.x + 140 end if ( platform4.x \< -500 ) then --soul.alpha = 1 platform4.x = platform3.x + 340 end if ( platform5.x \< -500 ) then --soul.alpha = 1 platform5.x = platform4.x + 282 end if ( platform6.x \< -500 ) then platform6.x = platform5.x + 202 speed = speed + 0.5 value = value \* 4 end if ( reaper.x \>= soulStart.x-1 and reaper.x \<= soulStart.x+1 and reaper.y \>= soulStart.y-60 and reaper.y \<= soulStart.y-30 ) then score = score + value soulStart.alpha = 0 end --These add the physics bodies to the platfroms and reaper physics.addBody( platformStart, "static", { bounce = 0, friction = 0.2} ) physics.addBody( reaper, "dynamic", { bounce = 0, friction = 0.2} ) physics.addBody( platform1, "static", {bounce = 0, friction = 0.2} ) physics.addBody( platform2, "static", { bounce = 0, friction = 0.2} ) physics.addBody( platform3, "static", { bounce = 0, friction = 0.2} ) physics.addBody( platform4, "static", {bounce = 0, friction = 0.2} ) physics.addBody( platform5, "static", {bounce = 0, friction = 1.0} ) physics.addBody( platform6, "static", {bounce = 0, friction = 0.2} ) reaper.isFixedRotation = true local function collisionfunction ( self, event ) jumpcounter = 0 reaper:setSequence( "running" ) reaper:play( ) end reaper.collision = collisionfunction if ( reaper.x \< -300 or reaper.y \> 810 ) then if ( score \> highscore ) then highscore = score end dead() end local function ascend () if jumpcounter \< 2 then --audio.play( jumpSound, jumpOptions ) reaper:setLinearVelocity( 0, -1000 ) reaper:setSequence( "jump" ) reaper:play( ) jumpcounter = jumpcounter + 1 end end --Listens for the user to tap to make the reaper jump Runtime:addEventListener( "touch", ascend ) reaper:addEventListener( "collision" ) end --This calls the update function or run the function continuously local myTimer = timer.performWithDelay( 16.667, update, -1 ) function dead ( ) --physics.pause( ) timer.cancel( myTimer ) Runtime:removeEventListener( "touch", ascend ) reaper:removeEventListener( "collision" ) reaper.x = platformStart.x reaper.y = platformStart.y - 165 speed = 2 value = 100 score = 0 --physics.stop( ) --composer.removeScene( "game" ) composer.gotoScene( "title\_screen" ) end end --platform shape function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then end end function scene:destroy( event ) local sceneGroup = self.view end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

Hi and welcome to the forums!

Iā€™m not 100% sure what youā€™re experiencing since this thread has to do with not removing buttons, but you call your ā€œupdateBackgroundsā€ function 60 times per second and each time you execute this code:

 physics.addBody( platformStart, "static", { bounce = 0, friction = 0.2} ) physics.addBody( reaper, "dynamic", { bounce = 0, friction = 0.2} ) physics.addBody( platform1, "static", {bounce = 0, friction = 0.2} ) physics.addBody( platform2, "static", { bounce = 0, friction = 0.2} ) physics.addBody( platform3, "static", { bounce = 0, friction = 0.2} ) physics.addBody( platform4, "static", {bounce = 0, friction = 0.2} ) physics.addBody( platform5, "static", {bounce = 0, friction = 1.0} ) physics.addBody( platform6, "static", {bounce = 0, friction = 0.2} )

You only should add the bodies once unless youā€™re going to remove them every time. I would move these to where you create the objects and get them out of your update loop.

Rob

Oh my gosh, thank so much!! :DĀ  Sorry, I didnā€™t post to the right thread, I didnā€™t know where to post new thread.

Hi, Iā€™m having a similar problem, but itā€™s removing the Runtime event listener or at least I think thatā€™s what it is.

--Load the composer library which is used for creating scenes/menus local composer = require( "composer" ) local scene = composer.newScene() composer.removeHidden( ) --composer.recycleOnSceneChange = true --Load the physics library local physics = require( "physics" ) --Load table library local table = require( "table" ) local math = require("math") --Set physics and gravity physics.start(true) physics.setGravity( 0, 100 ) --composer.removeScene( "title\_screen" ) --Seed the random number generator math.randomseed( os.time() ) --Load in audio local jumpSound = audio.loadSound( "SFX\_Jump\_04.wav" ) local jumpOptions = { channel = 2, } audio.setVolume( 0.5, { channel = 2 } ) local backgroundMusic = audio.play( "Reaper Music.wav" ) local backgroundOptions = { channel = 1, loop = -1, duration = 150000 } audio.setVolume( 0.3, { channel = 1 } ) --audio.play( backgroundMusic, backgroundOptions ) --Initialize jumpcounter local jumpcounter = 0 --Load the sprite sheet with all the platform information local sheetInfo = require("platforms") local platformSheet = graphics.newImageSheet( "platforms.png", sheetInfo:getSheet() ) --local sprite = display.newSprite( platformSheet, {frames={sheetInfo:getFrameIndex("sprite")}} ) local sheetInfo2 = require("StuffSprite") local decorSheet = graphics.newImageSheet( "StuffSprite.png", sheetInfo2:getSheet() ) --local sprite2 = display.newSprite( decorSheet, {frames={sheetInfo2:getFrameIndex("sprite")}} ) local options = { frames = { --Frame 1 (Reaper1-1) { x = 0, y = 0, width = 80, height = 80, }, --Frame 2 (Reaper1-2) { x = 80, y = 0, width = 80, height = 80, }, --Frame 3 (Reaper1-3) { x = 160, y = 0, width = 80, height = 80, }, --Frame 4 (Reaper1-4) { x = 240, y = 0, width = 80, height = 80, }, --Frame 5 (Reaper2-1) { x = 320, y = 0, width = 80, height = 80, }, --Frame 6 (Reaper2-2) { x = 400, y = 0, width = 80, height = 80, }, --Frame 7 (Reaper2-3) { x = 0, y = 80, width = 80, height = 80, }, --Frame 8 (Reaper2-4) { x = 80, y = 80, width = 80, height = 80, }, --Frame 9 (Reaper3-1) { x = 160, y = 80, width = 88, height = 82, }, --Frame 10 (Reaper3-2) { x = 88, y = 184, width = 88, height = 88, }, --Frame 11 (Reaper3-3) { x = 176, y = 184, width = 88, height = 100, }, --Frame 12 (Reaper3-4) { x = 264, y = 184, width = 80, height = 94, }, --Frame 13 (Reaper3-5) { x = 344, y = 184, width = 88, height = 118, }, --Frame 14 (Reaper3-6) { x = 0, y = 302, width = 88, height = 118, }, --Frame 15 (Reaper3-7) { x = 88, y = 302, width = 88, height = 118, }, --Frame 16 (Reaper3-8) { x = 176, y = 302, width = 88, height = 118, }, --Frame 17 (Reaper3-9) { x = 264, y = 302, width = 88, height = 118, }, --Frame 18 (Reaper3-10) { x = 248, y = 80, width = 88, height = 104, }, --Frame 19 (Reaper3-11) { x = 336, y = 80, width = 100, height = 104, }, --Frame 20 (Reaper3-12) { x = 0, y = 184, width = 88, height = 85, } }, sheetContentWidth = 512, sheetContentHeight = 512 } local player = graphics.newImageSheet( "reapersprite.png", options ) local sequence\_runningReaper = { { name = "running", frames = { 1, 2, 3, 4 }, time = 1000, loopCount = 0, loopDirection = "forward" }, { name = "jump", frames = { 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }, time = 700, loopCount = 1, loopDirection = "forward" } } --function load () --end function scene:create( event ) local sceneGroup = self.view --Load the display physics.setDrawMode( "hybrid" ) local background = display.newImageRect( "1Background.png", 1500, 900 ) background.x = display.contentCenterX background.y = display.contentCenterY sceneGroup:insert( background ) --Shapes for all the platform parts and the reverse of each platform section --local B3shape = { } --local reverseB3shape = { -926,-611, -157,-324 } local score = 0 scoreText2 = display.newText( "Score: ", 11, 50, native.systemFontBold, 80 ) sceneGroup:insert( scoreText2 ) scoreText = display.newText( score, 500, 50, native.systemFontBold, 80 ) sceneGroup:insert( scoreText ) local platformStart = display.newImageRect( platformSheet, 10, 552, 255 ) platformStart.x = display.contentWidth -900 platformStart.y = display.contentHeight -200 sceneGroup:insert( platformStart ) local treeStart = display.newImageRect( decorSheet, 9, 200, 200 ) treeStart.x = platformStart.x - 100 treeStart.y = platformStart.y - 170 sceneGroup:insert( treeStart ) local gravestoneStart = display.newImageRect( decorSheet, 3, 50, 100 ) gravestoneStart.x = platformStart.x + 100 gravestoneStart.y = display.contentHeight -365 sceneGroup:insert( gravestoneStart ) local soulStart = display.newImageRect( decorSheet, 4, 30, 40 ) soulStart.x = platformStart.x + 30 soulStart.y = platformStart.y - 135 sceneGroup:insert( soulStart ) local platform1 = display.newImageRect( platformSheet, 1, 100, 100 ) platform1.x = display.contentWidth -500 platform1.y = display.contentHeight -399 sceneGroup:insert( platform1 ) local platform2 = display.newImageRect( platformSheet, 11, 300, 150 ) platform2.x = display.contentWidth -320 platform2.y = display.contentHeight -393 sceneGroup:insert( platform2 ) local platform3 = display.newImageRect( platformSheet, 1, 100, 100 ) platform3.x = display.contentWidth -140 platform3.y = display.contentHeight -399 platform3:scale( -1, 1 ) sceneGroup:insert( platform3 ) local platform4 = display.newImageRect( platformSheet, 7, 300, 150 ) platform4.x = display.contentWidth +200 platform4.y = display.contentHeight -322 sceneGroup:insert( platform4 ) local platform5 = display.newImageRect( platformSheet, 8, 300, 200 ) platform5.x = display.contentWidth +482 platform5.y = display.contentHeight -391 sceneGroup:insert( platform5 ) local platform6 = display.newImageRect( platformSheet, 6, 100, 100 ) platform6.x = display.contentWidth +680 platform6.y = display.contentHeight -440 sceneGroup:insert( platform6 ) local reaper = display.newSprite( player, sequence\_runningReaper ) reaper:setSequence("running") reaper:play( ) reaper.x = platformStart.x reaper.y = platformStart.y - 165 sceneGroup:insert( reaper ) --local arrow = display.newImageRect( "arrow-landing.png", 100, 100 ) --arrow.x = display.contentCenterX --arrow.y = display.contentCenterY speed = 4 value = 100 --Start with Math calculations local function update( event ) --update background updateBackgrounds() end function updateBackgrounds() score = score + 1 scoreText.text = score --This line of code controls platform movement --Change the number inside parenthesis to change speed higher number mean faster --The minus sign makes the object move to the left platformStart.x = platformStart.x - (speed) soulStart.x = soulStart.x - (speed) treeStart.x = treeStart.x - (speed) gravestoneStart.x = gravestoneStart.x - (speed) platform1.x = platform1.x - (speed) platform2.x = platform2.x - (speed) platform3.x = platform3.x - (speed) platform4.x = platform4.x - (speed) platform5.x = platform5.x - (speed) platform6.x = platform6.x - (speed) --"Moves" the platforms back to the right so they can be run again if ( platformStart.x \< -500 ) then soulStart.alpha = 1 platformStart.x = 1400 end if (gravestoneStart.x \< -500 ) then gravestoneStart.x = platformStart.x + 100 end if ( soulStart.x \< -500 ) then soulStart.x = platformStart.x + 30 end if ( treeStart.x \< -500 ) then treeStart.x = platformStart.x - 100 end if ( platform1.x \< -500 ) then --soul.alpha = 1 platform1.x = platformStart.x + 400 end if ( platform2.x \< -500 ) then --soul.alpha = 1 platform2.x = platform1.x + 200 end if ( platform3.x \< -500 ) then --soul.alpha = 1 platform3.x = platform2.x + 140 end if ( platform4.x \< -500 ) then --soul.alpha = 1 platform4.x = platform3.x + 340 end if ( platform5.x \< -500 ) then --soul.alpha = 1 platform5.x = platform4.x + 282 end if ( platform6.x \< -500 ) then platform6.x = platform5.x + 202 speed = speed + 0.5 value = value \* 4 end if ( reaper.x \>= soulStart.x-1 and reaper.x \<= soulStart.x+1 and reaper.y \>= soulStart.y-60 and reaper.y \<= soulStart.y-30 ) then score = score + value soulStart.alpha = 0 end --These add the physics bodies to the platfroms and reaper physics.addBody( platformStart, "static", { bounce = 0, friction = 0.2} ) physics.addBody( reaper, "dynamic", { bounce = 0, friction = 0.2} ) physics.addBody( platform1, "static", {bounce = 0, friction = 0.2} ) physics.addBody( platform2, "static", { bounce = 0, friction = 0.2} ) physics.addBody( platform3, "static", { bounce = 0, friction = 0.2} ) physics.addBody( platform4, "static", {bounce = 0, friction = 0.2} ) physics.addBody( platform5, "static", {bounce = 0, friction = 1.0} ) physics.addBody( platform6, "static", {bounce = 0, friction = 0.2} ) reaper.isFixedRotation = true local function collisionfunction ( self, event ) jumpcounter = 0 reaper:setSequence( "running" ) reaper:play( ) end reaper.collision = collisionfunction if ( reaper.x \< -300 or reaper.y \> 810 ) then if ( score \> highscore ) then highscore = score end dead() end local function ascend () if jumpcounter \< 2 then --audio.play( jumpSound, jumpOptions ) reaper:setLinearVelocity( 0, -1000 ) reaper:setSequence( "jump" ) reaper:play( ) jumpcounter = jumpcounter + 1 end end --Listens for the user to tap to make the reaper jump Runtime:addEventListener( "touch", ascend ) reaper:addEventListener( "collision" ) end --This calls the update function or run the function continuously local myTimer = timer.performWithDelay( 16.667, update, -1 ) function dead ( ) --physics.pause( ) timer.cancel( myTimer ) Runtime:removeEventListener( "touch", ascend ) reaper:removeEventListener( "collision" ) reaper.x = platformStart.x reaper.y = platformStart.y - 165 speed = 2 value = 100 score = 0 --physics.stop( ) --composer.removeScene( "game" ) composer.gotoScene( "title\_screen" ) end end --platform shape function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then end end function scene:destroy( event ) local sceneGroup = self.view end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

Hi and welcome to the forums!

Iā€™m not 100% sure what youā€™re experiencing since this thread has to do with not removing buttons, but you call your ā€œupdateBackgroundsā€ function 60 times per second and each time you execute this code:

 physics.addBody( platformStart, "static", { bounce = 0, friction = 0.2} ) physics.addBody( reaper, "dynamic", { bounce = 0, friction = 0.2} ) physics.addBody( platform1, "static", {bounce = 0, friction = 0.2} ) physics.addBody( platform2, "static", { bounce = 0, friction = 0.2} ) physics.addBody( platform3, "static", { bounce = 0, friction = 0.2} ) physics.addBody( platform4, "static", {bounce = 0, friction = 0.2} ) physics.addBody( platform5, "static", {bounce = 0, friction = 1.0} ) physics.addBody( platform6, "static", {bounce = 0, friction = 0.2} )

You only should add the bodies once unless youā€™re going to remove them every time. I would move these to where you create the objects and get them out of your update loop.

Rob

Oh my gosh, thank so much!! :DĀ  Sorry, I didnā€™t post to the right thread, I didnā€™t know where to post new thread.