Hey guys I have run into a problem that I cant seem to figure out so when the "ball " hits the ground then you lose but I have it change scenes but the game scene is still there I want everything in the game.lua file to be removed/reset and I cant seem to insert stuff into scene group without getting a error
Game.lua Below
--game.lua local composer = require( "composer" ) local scene = composer.newScene() local physics = require("physics") physics.start() --physics.setDrawMode("hybrid") -- ----------------------------------------------------------------------------------------------------------------- -- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called. -- ----------------------------------------------------------------------------------------------------------------- -- local forward references should go here -- ------------------------------------------------------------------------------- -- "scene:create()" function scene:create( event ) local sceneGroup = self.view ------------------------------------------------------------------------------------------------ -- Initialize the scene here. -- Example: add display objects to "sceneGroup", add touch listeners, etc. end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local group = display.newGroup() local phase = event.phase if ( phase == "will" ) then -- Called when the scene is still off screen (but is about to come on screen). elseif ( phase == "did" ) then local ball = {} local i = 1 local startTimer local rightTimer local leftTimer local bottomTimer local removeBall -------------------------------------------------------------------------------------------------- local score = 0 local scoreTxt = display.newText( "0", 0, 0, native.systemFontBold, 80 ) scoreTxt.x = display.contentCenterX scoreTxt.y = 20 local function updateScore(event) score = score + 1 scoreTxt.text = string.format("%d", score ) end local function updateScoreDown(event) score = score - 1 scoreTxt.text = string.format("%d", score ) end -------------------------------------------------------------------------------------------------- local wallLeft = display.newImageRect("wall1.png", 15, 1200 ) wallLeft.x = display.contentWidth \* 0.02 wallLeft.y = display.contentHeight \* 0 wallLeft.value = 3 physics.addBody( wallLeft, "static", {bounce = 0} ) sceneGroup:insert(wallLeft) local wallRight = display.newImageRect("wall1.png", 15, 1200 ) wallRight.x = display.contentWidth \* 0.98 wallRight.y = display.contentHeight \* 0 wallRight.value = 2 physics.addBody( wallRight, "static", {bounce = 0} ) sceneGroup:insert(wallRight) local wallBottom = display.newImageRect("wall1.png", 15, 1200 ) wallBottom.x = display.contentWidth \* 0 wallBottom.y = display.contentHeight \* 1.076 wallBottom.rotation = 90 wallBottom.value = 1 physics.addBody( wallBottom, "static", {bounce = 0} ) sceneGroup:insert(wallBottom) ----------------------------------------------------------------------------------------------- local onCollision = function(self, event) if event.phase == "began" then local hit = self.value local other = event.other.value if other == 1 then --wallBottom display.remove(ball[hit]) bottomTimer = timer.performWithDelay( 10, updateScoreDown, 1 ) composer.removeScene("game") timer.cancel(startTimer) removeBall = timer.performWithDelay( 10, removeBall, 1 ) composer.gotoScene("mainmenu") elseif other == 2 then -- wallRight display.remove(ball[hit]) --ball[hit]:addEventListener( "collision", updateScore ) rightTimer = timer.performWithDelay( 10, updateScore, 1 ) elseif other == 3 then --wallLeft display.remove(ball[hit]) --ball[hit]:addEventListener( "collision", updateScore ) leftTimer = timer.performWithDelay( 10, updateScore, 1 ) end return true end end --------------------------------------------------------------------------------------------------------- function dragBody( event, params ) local body = event.target local phase = event.phase local stage = display.getCurrentStage() if "began" == phase then stage:setFocus( body, event.id ) body.isFocus = true if params and params.center then body.tempJoint = physics.newJoint( "touch", body, body.x, body.y ) else body.tempJoint = physics.newJoint( "touch", body, event.x, event.y ) end if params then local maxForce, frequency, dampingRatio if params.maxForce then body.tempJoint.maxForce = params.maxForce end if params.frequency then body.tempJoint.frequency = params.frequency end if params.dampingRatio then body.tempJoint.dampingRatio = params.dampingRatio end end elseif body.isFocus then if "moved" == phase then body.tempJoint:setTarget( event.x, event.y ) elseif "ended" == phase or "cancelled" == phase then stage:setFocus( body, nil ) body.isFocus = false body.tempJoint:removeSelf() end end return true end ------------------------------------------------------------------------------------------------ local function spawnBall() local spawn = math.random(display.contentWidth \* 0.2, display.contentWidth \* 0.8) ball[i] = display.newImage("zombiehead.png", 30, 30 ) ball[i]:scale(0.7,0.7) ball[i].x = spawn ball[i].y = -200 physics.addBody( ball[i], "dynamic", { bounce = 0, radius = 15 } ) ball[i].gravityScale = 0.350 ball[i].collision = onCollision ball[i]:addEventListener( "collision", ball[i] ) ball[i]:addEventListener( "touch", dragBody ) ball[i].myName = "ball" ball[i].value = i i = i + 1 local function removeBall() display.remove(ball[i]) print("test") end end startTimer = timer.performWithDelay( 700, spawnBall, -1 ) end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is on screen (but is about to go off screen). -- Insert code here to "pause" the scene. -- Example: stop timers, stop animation, stop audio, etc. elseif ( phase == "did" ) then -- Called immediately after scene goes off screen. end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's view ("sceneGroup"). -- Insert code here to clean up the scene. -- Example: remove display objects, save state, etc. end -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene