Collision not getting registered after pressing replay. (Using sceneoverlay)

Hi, first question on Corona forum! :slight_smile:

I started making a game in corona, everything works well. Except when I try to replay the game by showing game again the touch listener works fine, but the collision listeners aren’t  working at all. Hope to find a solution here:

--composer for new levels local composer = require( "composer" ) local scene = composer.newScene() local physics = require ("physics") physics.start() display.setStatusBar( display.HiddenStatusBar ) --local time = display.newText(duration,) --need to know no of player,duration local no\_of\_player local duration local timerText local paramsToBringBack local paramsToBringBackText local star local function getPlayerCount(level) playercount=2 if level=="Medium" then playercount=3 end if level == "Difficult" then playercount=4 end --print("playa"..playercount) return playercount end -- player count acc to difficulty --create player local function spawnPlayers() local player = display.newImage("images/captur.PNG") player.x=math.random(40,166) player.y=math.random(66,404) player.name="player" physics.addBody(player,"static") return player end function scene:create( event ) local sceneGroup = self.view -- CREATING OBJECTS --get level local params=event.params local level= params.level paramsToBringBack=level --decide no of players no\_of\_player=getPlayerCount(level) --print("No"..no\_of\_player) --table for players local player={} local bg = display.newImageRect("images/backg.jpg",display.contentWidth\*2,display.contentHeight\*3) star= display.newImage("images/fball.png") star.x = 160 star.y = 500 star.name = "star" physics.addBody(star,"dynamic") physics.setGravity(0,0) local goalpost = display.newImageRect("images/fbpost.png",display.contentWidth+display.contentWidth\*0.5,100) goalpost.x = 90 goalpost.y = 0 --resize goalpost.name = "goal" physics.addBody(goalpost,"static") --spwan players acc. to count for i=1,no\_of\_player,1 do --print("Spawn") player[i]=spawnPlayers() end -- local player1 = display.newImage("images/captur.PNG") -- player1.x = 69 -- player1.y = 165 -- player1.name="player1" -- physics.addBody(player1,"static") -- local player2 = display.newImage("images/captur.PNG") -- player2.x = 235 -- player2.y = 197 -- player2.name="player2" -- physics.addBody(player2,"static") local goaltxt=("") local displayGoal = display.newText( goaltxt, 160, 240, "Arial", 60 ) local halfW= display.contentWidth\*0.5 local halfH= display.contentHeight\*0.5 score=0 scoreText= display.newText("Score : "..score,halfW-100,0,native.systemFont,25) --display.newText("Score : ",halfW-40,10,native.systemFont,26) duration= 10 timerText= display.newText("Time : "..duration,halfW+90,0,native.systemFont,25) sceneGroup:insert(bg) sceneGroup:insert(star) sceneGroup:insert(goalpost) -- sceneGroup:insert(player1) -- sceneGroup:insert(player2) sceneGroup:insert(displayGoal) sceneGroup:insert(scoreText) sceneGroup:insert(timerText) for i=1,#player do sceneGroup:insert(player[i]) end -- DEFINING FUNCTIONS --to open game over screen. local function showGameOver() -- this method can be called to go to next page local composer= require "composer" local options = { isModal = true, effect = "fade", time = 500, params = { paramsToBringBackText=paramsToBringBack, scoreText=score } } composer.showOverlay( "gameover", options ) end -- decreases time local function decreaseTimeByOneSec() duration=duration-1 timerText.text="Time : "..duration if duration== 0 then showGameOver() end end --calling above function after every one sec timer.performWithDelay(1000,decreaseTimeByOneSec,duration) --hides text "Goal!" function hideMyText(event) -- print ("hideMyText") displayGoal.isVisible = false end function touchScreen( event ) if event.phase == "began" then --print(event.x) --print(event.y) transition.to(star, {time=1000, x=event.x, y=event.y, tag="star"}) end end function onLocalPreCollision( self, event ) if event.other.name == "goal" then --print("You scored a goal") displayGoal.isVisible=true displayGoal.text="Goal!" score=score+1 scoreText.text="Score : "..score --timer.performWithDelay(1000,hideMyText) end end function onCollision(event) --print(event.phase) if(event.contact.isTouching) then --print("collide!") end end function pushStarToOriginalPosition() --print("Star to original position") if star then transition.to(star, {x = 160, y= 500, time=0, onComplete=timer.performWithDelay(1000,hideMyText)}) end end function movePlayer(player) --print("Move soldier.") --move others for i=1,no\_of\_player-1,1 do --print("for sure.") transition.to(player[i],{time=1000,x=math.random(40,166),y=math.random(66,404)}) end --move one player transition.to( player[no\_of\_player], { time=1000, x=math.random(40,166), y=math.random(66,404), onComplete = function() movePlayer(player) end }) end function callmovePlayer() -- body print("Here") movePlayer(player) end function onLocalPostCollision( self, event ) print("Just collided!") if(event.contact.isTouching) then --stopping star transition so that it doesnt go in goal. transition.cancel("star") pushStarToOriginalPosition() print(event.selfElement) end-- body end movePlayer(player) Runtime:addEventListener("touch", touchScreen) star.preCollision = onLocalPreCollision star:addEventListener( "preCollision", star ) Runtime:addEventListener("collision", onCollision) star.postCollision = onLocalPostCollision star:addEventListener( "postCollision", star ) 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. physics.start() end end function scene:hide( event ) local sceneGroup = self.view 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.) elseif phase == "did" then physics.pause() Runtime:removeEventListener("touch", touchScreen) star:removeEventListener( "preCollision", star ) Runtime:removeEventListener("collision", onCollision) star:removeEventListener( "postCollision", star ) -- Called when the scene is now off screen end end function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's "view" (sceneGroup) -- -- INSERT code here to cleanup the scene -- ek.g. remove display objects, remove touch listeners, save state, etc. --composer.removeScene("level1",true) -- sceneGroup:removeSelf() -- sceneGroup=nil end function scene:gameAgain() physics.start() print("Inside gameagain") star.preCollision = onLocalPreCollision star:addEventListener( "preCollision", star ) Runtime:addEventListener("collision", onCollision) star.postCollision = onLocalPostCollision star:addEventListener( "postCollision", star ) end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) ----------------------------------------------------------------------------------------- return scene

here’s my gameover.lua

----------------------------------------------------------------------------------------- -- -- level1.lua -- ----------------------------------------------------------------------------------------- local composer = require( "composer" ) local scene = composer.newScene() -- -- include Corona's "physics" library -- local physics = require "physics" -- physics.start(); physics.pause() -------------------------------------------- -- forward declarations and other locals local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth\*0.5 -- include Corona's "widget" library local widget = require "widget" local paramsToBringBack local txt\_gameover local txt\_score local score\_recv --replay button handler local function onPlayBtnRelease(event) --composer.removeScene("game",true) local options = { effect = "fade", time = 500, params = { level=paramsToBringBack } } composer.hideOverlay( "fade",400) return true -- indicates successful touch end function scene:create( event ) local sceneGroup = self.view --get params local params = event.params print("level"..params.paramsToBringBackText) paramsToBringBack=params.paramsToBringBackText -- Called when the scene's view does not exist. -- -- INSERT code here to initialize the scene -- e.g. add display objects to 'sceneGroup', add touch listeners, etc. score\_recv=params.scoreText local bg = display.newImageRect("images/backg.jpg",display.contentWidth\*2,display.contentHeight\*3) txt\_gameover= display.newText( "Game over!", 160, 240, "Arial", 60 ) txt\_score= display.newText( "Score : "..score\_recv, 160, 300, "Arial", 40 ) local ReplayBtn = widget.newButton{ label="Replay", fontSize="20", font="Bold", labelColor = { default={ 1, 1, 1 }, over={ 0, 0, 0, 0.5 } }, defaultFile="images/buttonoverlay.png", overFile="images/buttonoverlay.png", width=154, height=50, onRelease = onPlayBtnRelease -- event listener function } ReplayBtn.x = display.contentWidth\*0.5 ReplayBtn.y = display.contentHeight - 80 -- all display objects must be inserted into group sceneGroup:insert( bg) sceneGroup:insert( txt\_gameover) sceneGroup:insert( txt\_score) sceneGroup:insert( ReplayBtn) --hides text "Goal!" function hideMyText(event) print ("hideMyText") txt\_gameover.isVisible = false txt\_score.isVisible = false end end function scene:show( event ) local sceneGroup = self.view --get params local params = event.params local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen txt\_score.text="Score : "..params.scoreText txt\_gameover.isVisible = true txt\_score.isVisible = true physics.start() 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. txt\_gameover.isVisible = false end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase local parent = event.parent 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.) parent:gameAgain() physics.pause() elseif phase == "did" then -- Called when the scene is now off screen --hideMyText() 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 -- package.loaded[physics] = nil -- physics = nil -- sceneGroup:removeSelf() -- sceneGroup=nil end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) ----------------------------------------------------------------------------------------- return scene

I am a beginner in Lua and Corona, so never mind the comments. Open to other suggestions too, but would be happy if this gets answered.

Thank you.

  1. Posting your entire game will not encourage people to help - boil the code down to the part which is going wrong. This will also help you work out the problem on your own.

  2. Your code is a huge mess. Tidy it up… Create individual modules for the objects in your game and stop making functions global.

  3. Your listeners have no return values which is pretty much the most important element of any listener function. Touch, tap and collision listeners must all return true if they are handling the event they are responding to, otherwise they must return false.

Implement at least the advice here and you will be improving your chances hugely of solving your problem. Then come back to the forum with a smaller set of code and a more specific question.

  1. Posting your entire game will not encourage people to help - boil the code down to the part which is going wrong. This will also help you work out the problem on your own.

  2. Your code is a huge mess. Tidy it up… Create individual modules for the objects in your game and stop making functions global.

  3. Your listeners have no return values which is pretty much the most important element of any listener function. Touch, tap and collision listeners must all return true if they are handling the event they are responding to, otherwise they must return false.

Implement at least the advice here and you will be improving your chances hugely of solving your problem. Then come back to the forum with a smaller set of code and a more specific question.