I am having this same exact problem on the same practice project . I thought the answer sgs and roaminggamer gave me was correct but I guess it’s not because I am having the same problem . I tried switching the code around and trying to take some things out but none of that helped .
game.lua:
-- requires local composer = require( "composer" ) local scene = composer.newScene() local physics = require("physics") physics.start() physics.setGravity(0,9.8) local top = display.contentCenterY - display.actualContentHeight/2 local bottom = top + display.actualContentHeight local left = display.contentCenterX - display.actualContentWidth/2 local right = left + display.actualContentWidth -- background function scene:create(event) local screenGroup = self.view local randomImage = math.random(1,33) local background = display.newImageRect("images/background"..randomImage..".jpg",display.contentWidth,display.contentHeight) background.x = display.contentCenterX background.y = display.contentCenterY screenGroup:insert(background) end local timeLimit = 100 local currentTime = 0 local timerUpTimer currentTimeText = display.newText(currentTime, 160, 20, native.systemFontBold, 20) currentTimeText:setTextColor(0,1,1) local function timerUp() currentTime = currentTime + 1 currentTimeText.text = currentTime if currentTime \>= timeLimit then display.remove(currentTimeText) timer.cancel(timerUpTimer) print("Good job beating the game !") end end timerUpTimer = timer.performWithDelay(1000, timerUp, 0) timerr = timer.performWithDelay(1000,timerDown,timeLimit) local allBalloons = {} local timerHandle local function onTouch( self, event ) allBalloons[self] = nil display.remove(self) return true end local options = { effect = "fade", time = 400 } -- This function stops the game local function endGame() -- Stop the timer timer.cancel(timerHandle) -- Iterate over 'allBalloons' table and destroy all balloons for k,v in pairs( allBalloons ) do display.remove(v) end composer.gotoScene("restart",options) end -- Generic/Common enterFrame listener used by each balloon local function enterFrame( self ) -- End the Game if any balloon's CENTER falls below the bottom of the screen if( self.y \>= bottom ) then timer.cancel(timerUpTimer) endGame() end end -- Generic/Common finalize event listener used by each balloon local function finalize( self ) Runtime:removeEventListener("enterFrame",self) end local function createBalloon( ) -- Randomly create one of five balloon images local imgNum = math.random( 1, 5 ) local tmp = display.newImageRect( "balloon" .. imgNum .. ".png", 195/5, 282/5 ) -- Store reference to balloon object in allBalloons table allBalloons[tmp] = tmp -- Randomly place the balloon tmp.y = top-50 tmp.x = math.random( left + 50, right - 50 ) -- Scale it to make a 'smaller' balloon --tmp:scale( 0.1, 0.1 ) -- add a touch listener tmp.touch = onTouch tmp:addEventListener( "touch" ) -- Give it a body so 'gravity' can pull on it physics.addBody( tmp, { radius = tmp.contentWidth/2} ) -- 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.5 -- Self destruct in 5 seconds timer.performWithDelay( 5000, function() allBalloons[tmp] = nil display.remove( tmp ) end ) -- attach generic enterFrame listener and listen for it tmp.enterFrame = enterFrame Runtime:addEventListener("enterFrame", tmp) -- attach generic finalize listener and listen for it tmp.finalize = finalize tmp:addEventListener("finalize") end -- Create a new baloon every 1/2 second forever timerHandle = timer.performWithDelay( 160, createBalloon, -3 ) function scene:show(event) local sceneGroup = self.view composer.removeScene("restart") end function scene:hide(event) local sceneGroup = self.view 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
I scan this code day and night and I see absolutely no error ,.