Invisible Physics Objects Glitch

Hi all,

I’m having some trouble with invisible physics objects that seem to be messing around with a game I wrote.

FYI I had come across this very problem before and resolved it with a simple .removeBody() and did the same thing here, but it backfires…

My problem lies in my you lose.lua module: the game launches to the main menu, the game starts, the player collides with the asteroid, clicks the restart button, and the game restarts with the player (greenGuy) moving backwards. I’ve no idea why this happens as I remove physics bodies and event listeners in my youlose.restart() function ( listener for which can be found in you lose.drawYouLose() ).

In case my files come out badly formatted, here is my Git repo: https://github.com/prafull2001/JumperCorona

Here are my files:

Main:

local Physics = require("physics") Physics.start() myScene = require("scene") myHero = require("hero") myGoomba = require("badguy") myButtons = require("buttons") myMenu = require("mainmenu") myLoseScreen = require("youlose") myMenu.drawMainMenu() playButton:addEventListener( "tap", myMenu.play ) function onCollision( event ) if (event.phase == "began") then if (event.object1.name == "greenGuy" and event.object2.name == "Asteroid 1") then myLoseScreen.drawYouLose() elseif (event.object1.name == "greenGuy" and event.object2.name == "Asteroid2") then myLoseScreen.drawYouLose() elseif (event.object1.name == "greenGuy" and event.object2.name == "Asteroid 3") then myLoseScreen.drawYouLose() end end end Runtime:addEventListener( "collision", onCollision )

Main Menu:

local mainmenu = {} local Physics = require("physics") Physics.start() function mainmenu.drawMainMenu() menuTheme = audio.loadStream("Music/menuTheme.ogg") menuThemeChannel = audio.play( menuTheme, { channel=1, loops=-1, fadein=3000 } ) background = display.newImage("menuBackground.png") background.x = display.contentCenterX background.y = display.contentCenterY background:scale(1.2, 1.2) playButton = display.newImage("playButton.png") playButton.x = display.contentCenterX playButton.y = display.contentCenterY - 75 playButton:scale( .3, .3 ) end function mainmenu.play() print("mainmenu - tap registered, preparing to load game..") playButton:removeEventListener( "tap", myMenu.play ) audio.stop(1) myScene.sayHello() myScene.drawScene() myHero.sayHello() myHero.drawHero() myGoomba.hello() myGoomba.drawBadGuy() myButtons.sayHello() myButtons.drawButtons() myGoomba.move() --myGoomba.detectCollision() gameTheme = audio.loadStream("Music/playTheme.ogg") gameThemeChannel = audio.play( gameTheme, { channel=2, loops=-1, fadein=0 } ) jumpButton:addEventListener("tap", myButtons.jump) backButton:addEventListener("tap", myButtons.goBack) end function mainmenu.sayHello() print("mainmenu - Hello....") end return mainmenu

Buttons:

local buttons = {} local Physics = require("physics") Physics.start() function buttons.drawButtons() jumpButton = display.newImage( "button.png") jumpButton.x = 473 jumpButton.y = 305 jumpButton.name = "Jump Button" jumpButton:scale( .2, .7 ) slideButton = display.newImage("button.png") slideButton.x = 5 slideButton.y = 305 slideButton.name = "Slide Button" slideButton:scale(.2, .7) backButton = display.newImage("backButton.png") backButton.x = display.contentCenterX - 245 backButton.y = display.contentCenterY - 100 backButton.name = "Back Button" backButton:scale(.11, .11) end function buttons.jump() --work on decreasing air time by increasing gravity... instance2:applyLinearImpulse(0, -0.2, instance2.x, instance2.y) --Applying 0 force in the X direction and -0.12 in the Y direction. Y is negative because --down is considered positive by the physics engine. --Apply force to the center of the green guy, hence instance.x & instance.y end function buttons.goBack() print("buttons - loading main menu...") audio.stop(2) Physics.removeBody( instance2 ) Physics.removeBody(obstacle[1]) Physics.removeBody(obstacle[2]) Physics.removeBody(obstacle[3]) myMenu.drawMainMenu() jumpButton:removeEventListener( "tap", myButtons.jump ) backButton:removeEventListener( "tap", myButtons.goBack ) playButton:addEventListener( "tap", myMenu.play ) --collectgarbage( "collect" ) end function buttons.sayHello() print("buttons - Hello....") end return buttons

YouLose:

local youlose = {} local Physics = require("physics") Physics.start() function youlose.drawYouLose() print("youlose - loading youlose...") youlosescreen = display.newImage( "gameover.png", 250, 168 ) youlosescreen:scale( 2, 2 ) restartButton = display.newImage("restartButton.png", 50, 230) restartButton:scale(.07,.07) restartButton:addEventListener( "tap", myLoseScreen.restart ) Runtime:removeEventListener( "colliison", onCollision ) backButton:removeEventListener( "tap", myButtons.goBack ) jumpButton:removeEventListener( "tap", myButtons.jump ) audio.stop(2) end --[[physics.removeBody( instance2 ) physics.removeBody(obstacle[1]) physics.removeBody(obstacle[2]) physics.removeBody(obstacle[3]) jumpButton:removeEventListener( "tap", myButtons.jump ) backButton:removeEventListener( "tap", myButtons.goBack ) ]] function youlose.restart() -- Physics.removeBody( instance2 ) Physics.removeBody(obstacle[1]) Physics.removeBody(obstacle[2]) Physics.removeBody(obstacle[3]) Physics.removeBody(grass1) Physics.removeBody(grass2) Physics.removeBody(grass3) jumpButton:removeEventListener( "tap", myButtons.jump ) backButton:removeEventListener( "tap", myButtons.goBack ) restartButton:removeEventListener( "tap", myLoseScreen.restart ) print("cleanup done") myScene.sayHello() myScene.drawScene() myHero.sayHello() myHero.drawHero() myGoomba.hello() myGoomba.drawBadGuy() myButtons.sayHello() myButtons.drawButtons() myGoomba.move() --myGoomba.detectCollision() gameTheme = audio.loadStream("Music/playTheme.ogg") gameThemeChannel = audio.play( gameTheme, { channel=2, loops=-1, fadein=0 } ) jumpButton:addEventListener("tap", myButtons.jump) backButton:addEventListener("tap", myButtons.goBack) end --restartButton:addEventListener( "tap", myLoseScreen.restart ) function youlose.sayHello() print("youlose - Hello....") end return youlose

Thank you for your time!!!