Search code examples
luacoronasdkgravity

Corona Gravity Glitch


I am making a game using Corona SDK that utilizes the physics engine and am experiencing quite an annoying glitch: every time I hit an asteroid and hit the back button, press play again, the green guy has an even higher jump value.

I would appreciate any and all help!!

Here is my code:

Main file:

local Physics = require("physics")
Physics.start()


centerX = display.contentCenterX
centerY = display.contentCenterY



myScene = require("scene")
myHero = require("hero")
myGoomba = require("badguy")
myButtons = require("buttons")
myMenu = require("mainmenu")


myMenu.drawMainMenu()
playButton:addEventListener( "tap", myMenu.play )

MainMenu:

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..")

    audio.stop(1)

    myScene.sayHello()
    myScene.drawScene()


    myHero.sayHello()
    myHero.drawHero() 



    myGoomba.hello()
    myGoomba.drawBadGuy()


    myButtons.sayHello()
    myButtons.drawButtons()


    myGoomba.move()


    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 mailmen

Buttons:

local buttons = {}

local Physics = require("physics")
Physics.start()


function buttons.drawButtons()
    jumpButton = display.newImage( "button.png")
    jumpButton.x = 473
    jumpButton.y = 305
    jumpButton:scale( .2, .7 )

    slideButton = display.newImage("button.png")
    slideButton.x = 5
    slideButton.y = 305
    slideButton:scale(.2, .7)

    backButton = display.newImage("backButton.png")
    backButton.x = display.contentCenterX - 245
    backButton.y = display.contentCenterY - 100
    backButton:scale(.11, .11)

end

function buttons.jump()

    --work on decreasing air time by increasing gravity...
    instance2:applyLinearImpulse(0, -0.12, 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)
    myMenu.drawMainMenu()
    --reset gravity and jump-impulse values here

end


function buttons.sayHello()
    print("buttons - Hello....")
end


return buttons

Hero:

local hero = {}

local Physics = require("physics")
Physics.start()


function hero.drawHero()

    print("hero - loading hero...")

    greenGuysheet = graphics.newImageSheet( "greenman.png", { width=128, height=128, numFrames=15 } )

    -- play 15 frames every 500 ms
    instance2 = display.newSprite( greenGuysheet, {start=1, count=15, time=500 } )
    instance2.name = "greenGuy"
    Physics.addBody(instance2, "dynamic", {radius = 20, bounce = 0})
    instance2.x =100
    instance2.y = 260   
    instance2.gravityScale = 2.5

    instance2:play()

end





function hero.sayHello()
    print("hero - Hello....")
end


return hero

Solution

  • It seems you don't use composer module. It is recommended to use it to manage scenes. Read more about it in tutorial Introducing the Composer API.

    I'm not sure but it seems you are adding event listener

    jumpButton:addEventListener("tap", myButtons.jump)
    backButton:addEventListener("tap", myButtons.goBack)
    

    each time you call mainmenu.play function. After you go back to game and tap on jump button listener is called twice. So you need remove tap listeners before you go to menu or add tap listeners only once. Read more about event listeners on Corona documentation.