End Game and Collision Help !

I don’t understand … I’ve tryied many things but when tu collision occure i don’t know why but I can’t go to the scene:hide() … what did I have to do please ?  :frowning:

--Game scene local composer = require("composer") local scene = composer.newScene() local musicGame = audio.loadStream("musicGame.wav") audio.play( musicGame , {loops=200 }) --audio.stop(musicMenu) --Physic function local physics = require ("physics") physics.start () physics.setGravity(0, 0) --physics.setDrawMode("hybrid") local leftWall = display.newRect(0, display.contentCenterY, 1, 700) local rightWall = display.newRect(320, display.contentCenterY, 1, 700) local flooring = display.newRect (display.contentCenterX, 520, 500, 1 ) local ceiling = display.newRect(display.contentCenterX, -44, 500, 1 ) physics.addBody(leftWall, "static", { bounce = 0.1}) physics.addBody(rightWall, "static", { bounce = 0.1}) physics.addBody(ceiling, "static", { bounce = 0.1}) physics.addBody(flooring, "static", { bounce = 0.1}) local armadillo local function spawnMeteor() local meteor = display.newImage("meteor.png") physics.addBody(meteor, "dynamic",{friction = 1.0, radius={200}}) meteor.x, meteor.y = math.random (5, 315), math.random (-40, 515) meteor.myName = "meteor" return meteor end function scene:create(event) local sceneGroup = self.view --Background local background = display.newImage("fond.png", 200, 250, 480, 70) armadillo = display.newImage("arm.png") meteor1 = spawnMeteor() meteor2 = spawnMeteor() gauche = display.newImage("boutongauche.png") droit = display.newImage("boutondroit.png") bas = display.newImage("boutonbas.png") haut = display.newImage("boutonhaut.png") score = 0 scoreTxt = display.newText(score, 300, 30, font, 50) scoreTxt.x = 160 scoreTxt.y = -23 sceneGroup:insert(background) sceneGroup:insert(scoreTxt) sceneGroup:insert(meteor1) sceneGroup:insert(meteor2) sceneGroup:insert(gauche) sceneGroup:insert(droit) sceneGroup:insert(bas) sceneGroup:insert(haut) sceneGroup:insert(armadillo) end function scene:show (event) local phase = event.phase if (phase == "will") then local sceneGroup = self.view \_W = display.contentWidth; \_H = display.contentHeight; motionx = 0; motiony = 0 speed = 3; armadillo.x = 160 armadillo.y = 200 physics.addBody(armadillo, "dynamic",{ bounce =0.8,friction = 1.0}) armadillo.gravityScale = 0 armadillo.isFixedRotation = true armadillo.ID = "arma" armadillo.myName = "player" gauche.x = 50; gauche.y = 460; droit.x = 150; droit.y = 460 bas.x = 100; bas.y = 460 haut.x = 100; haut.y = 410 local meteor1 local meteor2 elseif (phase =="did") then local sceneGroup = self.view local function movearmadillo (event) armadillo.x = armadillo.x + motionx; armadillo.y = armadillo.y + motiony local function stop (event) if event.phase =="ended" then motionx = 0; motiony = 0 end end Runtime:addEventListener("touch", stop ) end Runtime:addEventListener("enterFrame", movearmadillo) function gauche:touch() motionx = -speed; end gauche:addEventListener("touch",gauche) function droit:touch() motionx = speed; end droit:addEventListener("touch",droit) function haut:touch() motiony = -speed; end haut:addEventListener("touch", haut) function bas:touch() motiony = speed; end bas:addEventListener("touch", bas) function moveMeteor() transition.to(meteor1, {time=2000, x=math.random(5, 315), y=math.random(-40,515), onComplete=moveMeteor}) end moveMeteor() function moveMeteor2() transition.to(meteor2, {time=2000, x=math.random(5, 315), y=math.random(-40,515), onComplete=moveMeteor2}) end moveMeteor2() local function updateScore() score = score + 1 scoreTxt.text = score end scoreTimer = timer.performWithDelay(1000, updateScore, -1) --function endGame () --if (event.onCollision == "began") then --composer.gotoScene("hide") --local option = {effect = "fade", time = 200} --print("okay !") --elseif (event.onCollision == "ended") then --print("non") --end --end --Runtime:addEventListener("collision", endGame) --end function onCollision(event) if(event.object1.myName == "player" and event.object2.myName == "meteor") or (event.object1.myName == "meteor" and event.object2.myName == "player") or (event.object1.myName == "player" and event.object2.myName == "meteor2") or (event.object1.myName == "meteor2" and event.object2.myName == "player") then print(event.object1.myName, event.object2.myName, score) timer.cancel(scoreTimer) composer.loadScene("hide") end end Runtime:addEventListener("collision", onCollision) end end function scene:hide (event) local sceneGroup = self.view local phase = event.phase if(phase == "will") then print ("yes !!!") moveMeteor = false moveMeteor2 = false elseif (phase == "did") then composer.gotoScene ( "gameOver", {effect = "fade", time = 200}) end end scene:addEventListener("show", scene) scene:addEventListener("create", scene) scene:addEventListener("hide", scene) return scene

scene:hide() gets called automatically when you use composer.gotoScene(). “hide” is not a scene you tell composer to go to. Instead of your collision function having composer.loadScene(“hide)”, just use composer.gotoScene(“nameofscene”). This will change the scene and scene:hide() will be called as automatically as the scene changes. You don’t need to call composer.gotoScene() inside the scene:hide() function, as you are already changing scene.

Oh YES ! Thank you !!!

I think it’s the last problem that I have to resolve :

when I change scene to go to GameOver.lua , this message appear :

playGame.lua96: attempt to perform arithmetic on field ‘x’ (a nil value) stack traceback:

 

I haven’t this problem before and I can’t do whithout ‘x’ for my code … what I have to do please ? :confused:

I can’t see what would be causing that. Are you at some point removing armadillo or setting it equal to something else? The error suggests that armadillo is a table that does not have an ‘x’ property, so I assume you’ve made some kind of change to it that isn’t in your code above.

I don’t change anything … the problem occure when I supposed to go to the next scene so that’s strange … :confused:

And the problem is at the second : armadillo.x, not the first and its the same problem with the second armadillo.y !

I don’t understand …

And is that the problem does not come from the fact that I say that the value is equal to itself plus the MotionX? Is he not be a different value before and after the ‘equal’?

No … it isn’t logical ! :confused:

Are you doing anything in scene:hide() that you weren’t doing before?

Or in your collision function?

No no, I don’t change anything …

I noticed something really strange … i put this code to see what happend :

local function movearmadillo (event) if gauche.x == 50 then armadillo.x = armadillo.x + motionx; armadillo.y = armadillo.y + motiony local function stop (event) if event.phase =="ended" then motionx = 0; motiony = 0 end end Runtime:addEventListener("touch", stop ) end end Runtime:addEventListener("enterFrame", movearmadillo)

and like this that work ! But, I star the game, i play once the game, no problem, I go to the GameOver scene, no problem, and here, if I go to the menu that’s good but if I go to playAgain the same message appears … so that’s mean the problem is postponed for a lap …

I put my entire code, I don’t know, if you see something …

 --Game scene local composer = require("composer") local scene = composer.newScene() local musicGame = audio.loadStream("musicGame.wav") musicGame = audio.play( musicGame , {loops=200 }) --audio.stop(musicMenu) --Physic function local physics = require ("physics") physics.start () physics.setGravity(0, 0) --physics.setDrawMode("hybrid") local leftWall = display.newRect(0, display.contentCenterY, 1, 700) local rightWall = display.newRect(320, display.contentCenterY, 1, 700) local flooring = display.newRect (display.contentCenterX, 524, 500, 1 ) local ceiling = display.newRect(display.contentCenterX, -44, 500, 1 ) physics.addBody(leftWall, "static", { bounce = 0.1}) physics.addBody(rightWall, "static", { bounce = 0.1}) physics.addBody(ceiling, "static", { bounce = 0.1}) physics.addBody(flooring, "static", { bounce = 0.1}) local armadillo local function spawnMeteor() local meteor = display.newImage("meteor.png") physics.addBody(meteor, "dynamic",{friction = 1.0, radius={200}}) meteor.x, meteor.y = math.random (5, 315), math.random (-40, 200) meteor.myName = "meteor" return meteor end function scene:create(event) local sceneGroup = self.view --Background local background = display.newImage("fond.png", 200, 250, 480, 70) armadillo = display.newImage("arm.png") meteor1 = spawnMeteor() meteor2 = spawnMeteor() gauche = display.newImage("boutongauche.png") droit = display.newImage("boutondroit.png") bas = display.newImage("boutonbas.png") haut = display.newImage("boutonhaut.png") score = 0 scoreTxt = display.newText(score, 300, 30, font, 50) scoreTxt.x = 160 scoreTxt.y = -23 sceneGroup:insert(background) sceneGroup:insert(scoreTxt) sceneGroup:insert(meteor1) sceneGroup:insert(meteor2) sceneGroup:insert(gauche) sceneGroup:insert(droit) sceneGroup:insert(bas) sceneGroup:insert(haut) sceneGroup:insert(armadillo) end function scene:show (event) local phase = event.phase if (phase == "will") then local sceneGroup = self.view \_W = display.contentWidth; \_H = display.contentHeight; motionx = 0; motiony = 0 speed = 3; armadillo.x = 160 armadillo.y = 400 physics.addBody(armadillo, "dynamic",{ bounce =0.8,friction = 1.0}) armadillo.gravityScale = 0 armadillo.isFixedRotation = true armadillo.ID = "arma" armadillo.myName = "player" gauche.x = 50; gauche.y = 460; droit.x = 150; droit.y = 460 bas.x = 100; bas.y = 460 haut.x = 100; haut.y = 410 local meteor1 local meteor2 elseif (phase =="did") then local sceneGroup = self.view local function movearmadillo (event) if gauche.x == 50 then armadillo.x = armadillo.x + motionx; armadillo.y = armadillo.y + motiony local function stop (event) if event.phase =="ended" then motionx = 0; motiony = 0 end end Runtime:addEventListener("touch", stop ) end end Runtime:addEventListener("enterFrame", movearmadillo) function gauche:touch() motionx = -speed; end gauche:addEventListener("touch",gauche) function droit:touch() motionx = speed; end droit:addEventListener("touch",droit) function haut:touch() motiony = -speed; end haut:addEventListener("touch", haut) function bas:touch() motiony = speed; end bas:addEventListener("touch", bas) function moveMeteor() transition.to(meteor1, {time=2000, x=math.random(5, 315), y=math.random(-40,515), onComplete=moveMeteor}) end moveMeteor() function moveMeteor2() transition.to(meteor2, {time=2000, x=math.random(5, 315), y=math.random(-40,515), onComplete=moveMeteor2}) end moveMeteor2() local function updateScore() score = score + 1 scoreTxt.text = score end scoreTimer = timer.performWithDelay(1000, updateScore, -1) --function endGame () --if (event.onCollision == "began") then --composer.gotoScene("hide") --local option = {effect = "fade", time = 200} --print("okay !") --elseif (event.onCollision == "ended") then --print("non") --end --end --Runtime:addEventListener("collision", endGame) --end function onCollision(event) if(event.object1.myName == "player" and event.object2.myName == "meteor") or (event.object1.myName == "meteor" and event.object2.myName == "player") or (event.object1.myName == "player" and event.object2.myName == "meteor2") or (event.object1.myName == "meteor2" and event.object2.myName == "player") then print(event.object1.myName, event.object2.myName, score) composer.gotoScene("gameOver", {effect = "fade", time = 200}) end end Runtime:addEventListener("collision", onCollision) end end function scene:hide (event) local sceneGroup = self.view local phase = event.phase if(phase == "will") then timer.cancel(scoreTimer) audio.pause("musicGame") composer.setVariable("scoreTimer", scoreTimer) moveMeteor = false moveMeteor2 = false elseif (phase == "did") then --composer.gotoScene ( "gameOver", {effect = "fade", time = 200}) end end scene:addEventListener("show", scene) scene:addEventListener("create", scene) scene:addEventListener("hide", scene) return scene

The problem is resolved !!! *-*

I just had to remove the enterFrame …

I think I ended up bothering you now !

Thank you thank you so much Hasty for all you have done for me, I’m am really grateful !!! (And to accept my English very bad, I’m French! ;) )

Again thank you and good continuation in the proramation (and helping noobs like me! :D )

Bye !

Please, help me for my other problem here : https://forums.coronalabs.com/topic/62743-game-if-time-then-new-object-problem/?p=325622

Thak you !