Change function of button once it has been pressed?

How would I change the function of the button once it is pressed, for example. Click a button makes an object spawn, then clicking it again makes it despawn the object, then click again makes it spawn again… etc. Right now I have two separate buttons to do this task, how could I just make it one button.

Here are the two separate buttons

local platform = display.newImage( scene.perRunGroup, "shapes/platform1.png") platform.alpha = 0 platform.myName = "platform" local widget = require( "widget" ) -- Function to handle button events local function handleButtonEvent( event ) physics.addBody( platform, "dynamic", physicsData:get("platform")) platform.alpha = 1 platform.gravityScale = 0 platform.linearDamping = 900000000000000000000 platform.angularDamping = 9000000000000000000 platform.isFixedRotation=true platform.x = 270 platform.y = 35 platform.rotation = 0 if ( "ended" == event.phase ) then print( "Button was pressed and released" ) end end local ap1 = widget.newButton( { width = 45, height = 45, defaultFile = "buttons/spawn.png", overFile = "buttons/spawnover.png", label = "button", onEvent = handleButtonEvent } ) -- Center the button ap1.x = 25 ap1.y = 569 -- Change the button's label text ap1:setLabel( "P1" ) sceneGroup:insert(ap1) ------------------------------------------------------- local widget = require( "widget" ) -- Function to handle button events local function handleButtonEvent( event ) platform.alpha = 0 physics.removeBody(platform) if ( "ended" == event.phase ) then print( "Button was pressed and released" ) end end local rp1 = widget.newButton( { width = 45, height = 45, defaultFile = "buttons/spawn.png", overFile = "buttons/spawnover.png", label = "button", onEvent = handleButtonEvent } ) -- Center the button rp1.x = 25 rp1.y = 615 -- Change the button's label text rp1:setLabel( "X" ) sceneGroup:insert(rp1)

You can just create your own flag.  Something along these lines:

local platform = display.newImage( scene.perRunGroup, "shapes/platform1.png") platform.alpha = 0 platform.myName = "platform" local widget = require( "widget" ) -- Function to handle button events local function handleButtonEvent( event ) if ( "ended" == event.phase ) then if self.wasClicked then self.wasClicked = false platform.alpha = 0 physics.removeBody(platform) else self.wasClicked = true physics.addBody( platform, "dynamic", physicsData:get("platform")) platform.alpha = 1 platform.gravityScale = 0 platform.linearDamping = 900000000000000000000 platform.angularDamping = 9000000000000000000 platform.isFixedRotation=true platform.x = 270 platform.y = 35 platform.rotation = 0 end end end local ap1 = widget.newButton( { width = 45, height = 45, defaultFile = "buttons/spawn.png", overFile = "buttons/spawnover.png", label = "button", onEvent = handleButtonEvent } ) -- Center the button ap1.x = 25 ap1.y = 569 -- set button's flag ap1.wasClicked = false -- Change the button's label text ap1:setLabel( "P1" )

Cheers, what if I wanted to make a third or 4th click do something different? Like 2nd click removes body and then third click doesn’t something different? Like change gravity (just an example)

Change the flag to a number, and increment the number with each click.  On the last one set it back to 1.

You can just create your own flag.  Something along these lines:

local platform = display.newImage( scene.perRunGroup, "shapes/platform1.png") platform.alpha = 0 platform.myName = "platform" local widget = require( "widget" ) -- Function to handle button events local function handleButtonEvent( event ) if ( "ended" == event.phase ) then if self.wasClicked then self.wasClicked = false platform.alpha = 0 physics.removeBody(platform) else self.wasClicked = true physics.addBody( platform, "dynamic", physicsData:get("platform")) platform.alpha = 1 platform.gravityScale = 0 platform.linearDamping = 900000000000000000000 platform.angularDamping = 9000000000000000000 platform.isFixedRotation=true platform.x = 270 platform.y = 35 platform.rotation = 0 end end end local ap1 = widget.newButton( { width = 45, height = 45, defaultFile = "buttons/spawn.png", overFile = "buttons/spawnover.png", label = "button", onEvent = handleButtonEvent } ) -- Center the button ap1.x = 25 ap1.y = 569 -- set button's flag ap1.wasClicked = false -- Change the button's label text ap1:setLabel( "P1" )

Cheers, what if I wanted to make a third or 4th click do something different? Like 2nd click removes body and then third click doesn’t something different? Like change gravity (just an example)

Change the flag to a number, and increment the number with each click.  On the last one set it back to 1.