Toggle button help

How can i make a toggle button?

I want to add an event listener to the background so that everytime i touch it, i want gravity to switch directions without any delay.

Does anyone know how to do this? [import]uid: 42126 topic_id: 9136 reply_id: 309136[/import]

Hmmm I JUST posted a ToggleClass a couple weeks ago! Check it out here: http://github.com/AdamBuchweitz/ToggleClass

Not 100% what you’re asking for, but it should point you in the right direction.

Hope it helps,

a. [import]uid: 12405 topic_id: 9136 reply_id: 33338[/import]

How would you implement this, lets say for:

I want a single button to toggle between physics.setGravity(0, -9.81) and physics.setGravity(0, 9.81) every time I press it.

Can this be done with the class you’ve provided, and if so how?

Thanks [import]uid: 42126 topic_id: 9136 reply_id: 33382[/import]

Hey there,

Use an if statement - when the button is pressed if gravity is set one way, turn it the opposite way, elseif it’s set the other way then reverse it again.

Eg;

[lua]physics.setGravity(0, 9.81)
gravity = downward

function flipgravity (event)
if gravity == downward then
physics.setGravity(0, -9.81)
gravity = upward
elseif gravity == upward then
physics.setGravity(0, 9.81)
gravity = upward
end
end
yourbutton:addEventListener(“tap”, flipgravity)[/lua]

Hope that helps :slight_smile:

Peach [import]uid: 52491 topic_id: 9136 reply_id: 33399[/import]