Change scene after a collision, HELP !

Hi,

I have a problem that I have to resolve rapidly … In the scene:show() I put a collision function and I want go to the scene:hide() when the collision occure, but it doesn’t work !  -_-

The timer stop so the onCollision function works but I have put this code : print (“yes !!!”) on the scene:hide() to check if this scene works but it never work … I don’t understand why so if you can help me I will be very happy !  :wink:

This is my code :

 --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) option = {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 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

Can’t you just add composer.gotoScene()? If not, could you explain what you are trying to do again, I don’t really get it.

Sorry, i find the problem thank you, but now I have another problem … when I change scene, 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:

Hey lucas,

seems like your code is pretty messed up in general.

You’ve got a funtion called “movearmadillo” whcih runs over an enterFrame listener. Inside the function you add a touch listener. So you are adding a touch listener every frame (30 or 60 times a second), which is never a good thing.

Concerning your error, the “movearmadillo” function is getting called over and over, even when you leave the scene. That causes the error, as there is no armadillo after you’ve left the scene.

To correct this you should remove the enterFrame Listeners as you leave the scene. This is a general practice you should apply to all Runtime Listeners.

Greetings

Torben

HALLELUJAH !!! 

Torbenratzlaff you save me !!! *-*

I know my code is very messed up but it’s my first code on Corona SDK and I just discover how it’s work ! Professionals must vomit seeing my code but my goal is just to make a game that works !  :slight_smile:

Thanks a lot and keep helping noobs !  :wink:

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 !

Can’t you just add composer.gotoScene()? If not, could you explain what you are trying to do again, I don’t really get it.

Sorry, i find the problem thank you, but now I have another problem … when I change scene, 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:

Hey lucas,

seems like your code is pretty messed up in general.

You’ve got a funtion called “movearmadillo” whcih runs over an enterFrame listener. Inside the function you add a touch listener. So you are adding a touch listener every frame (30 or 60 times a second), which is never a good thing.

Concerning your error, the “movearmadillo” function is getting called over and over, even when you leave the scene. That causes the error, as there is no armadillo after you’ve left the scene.

To correct this you should remove the enterFrame Listeners as you leave the scene. This is a general practice you should apply to all Runtime Listeners.

Greetings

Torben

HALLELUJAH !!! 

Torbenratzlaff you save me !!! *-*

I know my code is very messed up but it’s my first code on Corona SDK and I just discover how it’s work ! Professionals must vomit seeing my code but my goal is just to make a game that works !  :slight_smile:

Thanks a lot and keep helping noobs !  :wink:

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 !