Changing gravity upon button press

Hi all,

I’m working on a kind of sky diving game and would like to have the gravity change when the player pushes the ‘release chute’ button.

Can anyone help with the code needed for this?

I tried this but I feel it’s wrong…

– Release chute button
local releaseChute = display.newImage (“Images/relChute.png”)
releaseChute.x = 440
releaseChute.y = 25
releaseChute.xScale = 0.35
releaseChute.yScale = 0.5

local function pressChute (event)
if event.phase == “began” then
physics.setGravity(0,12)
end
end

releaseChute:addEventListener (“touch”, pressChute)
Thanks for any help :slight_smile:
Alex

[import]uid: 120751 topic_id: 25926 reply_id: 325926[/import]

physics.setGravity(0,12) = downward force
physics.setGravity(0,-12) = upward force

How does your gravity start, pushing objects down or pulling them up? :slight_smile: [import]uid: 52491 topic_id: 25926 reply_id: 105031[/import]

I’ve created it at the top of my game file and it starts by dropping the character down but once the release chute button is pushed the character starts to fall slower. This is what’s at the top of the file:

local physics = require(“physics”)
physics.start()
physics.setGravity(-3,6)

I’m not sure how I can make it change on a button though as what I tried didn’t do anything :confused:

Thanks :slight_smile: [import]uid: 120751 topic_id: 25926 reply_id: 105046[/import]

Hey again Alex,

Try running this code on its own, it might give you a better idea about how to proceed with this;
[lua]-- Set up physics
require ( “physics” )
physics.start()
physics.setGravity( 0, 9 )

–Add an object to demonstrate gravity changes
local obj = display.newCircle( 160, 240, 25 )
physics.addBody(obj, {radius=25})

–Buttons to change gravity
local btn1 = display.newRect( 10, 10, 60, 30 )
local btn2 = display.newRect( 250, 10, 60, 30 )

–Functions to change gravity
local function gravUp()
physics.setGravity(0,-9)
end
btn1:addEventListener(“tap”, gravUp)

local function gravDown()
physics.setGravity(0,9)
end
btn2:addEventListener(“tap”, gravDown)

–Floor to prevent object from dropping down off screen
local floor = display.newRect( 0, 460, 320, 20 )
physics.addBody(floor, “static”)[/lua]

The button on the left makes gravity pull upwards, the one on the right makes it pull downward.

Peach :slight_smile: [import]uid: 52491 topic_id: 25926 reply_id: 105350[/import]