Single button to display and remove same image.

Hi is it possible to have a single button when touched, display an image and touching it the 2nd time removes the same image. Something like a switch?
It would be possible to remove the image by creating another new button for that function but it means taking up more space on the small iphone display.

any advice? [import]uid: 86993 topic_id: 15876 reply_id: 315876[/import]

Hi soontcb, You can implement a switch-like system quite easily. All you need is to set a variable that holds which part of the switch and base your listeners on that switch’s variable.

[lua]–>> Declare Switch <switchOn = true
–>> Other Code <
–>> Check Switch Value <if switchOn == true then
loadImgFunc() -->> Call Function (or set event listener), inserts image <else
removeImgFunc() -->> Call Function (or set event listener), removes image <end[/lua]

In the loadImgFunc Function, you could set switchOn to false to tell the button to remove the image rather than create it. The same can also be done for the removeImgFunc Function but changing switchOn to true.

Hope I helped, Luke.
[import]uid: 75643 topic_id: 15876 reply_id: 58693[/import]

Hey there - it absolutely is :slight_smile:

This is plug and play code;

[lua]local myButton = display.newRect( 50, 400, 50, 20 )

local function pressBtn (event)
if ball == nil then
ball = display.newCircle( 160, 200, 20 )
elseif ball ~= nil then
ball:removeSelf()
ball = nil
end
end
myButton:addEventListener(“tap”, pressBtn)[/lua]

Check that out - you will notice this is recreating the image each time but really it would be better to just toggle the visibility on and off instead, which you would do like this;

[lua]local myButton = display.newRect( 50, 400, 50, 20 )

local ball = display.newCircle( 160, 200, 20 )

local function pressBtn (event)
if ball.isVisible == false then
ball.isVisible = true
elseif ball.isVisible == true then
ball.isVisible = false
end
end
myButton:addEventListener(“tap”, pressBtn)[/lua]

I hope that will be useful for you :slight_smile:

Peach [import]uid: 52491 topic_id: 15876 reply_id: 58695[/import]

Luke beat me to it. Leaving my post there in case it provides any additional info that might be of use. [import]uid: 52491 topic_id: 15876 reply_id: 58696[/import]

You could combine both our examples quite nicely though. [import]uid: 75643 topic_id: 15876 reply_id: 58703[/import]

What you are after is called short-circuiting

and the sample that peach has can be shortened further as

local myButton = display.newRect( 50, 400, 50, 20 )  
   
local ball = display.newCircle( 160, 200, 20 )  
   
local function pressBtn (event)  
 ball.isVisible == not ball.isVisible  
end  
myButton:addEventListener("tap", pressBtn)  

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15876 reply_id: 58719[/import]

Nicely done :slight_smile:

Although there’s an error on line 6, == should be =

Peach :wink: [import]uid: 52491 topic_id: 15876 reply_id: 58826[/import]

Hi thanks everyone, for the suggestions. You guys are GREAT!!
I will incorporate the code into my apps and see if I can get the results I want.

Cheers
Ben aka Soontcb [import]uid: 86993 topic_id: 15876 reply_id: 58827[/import]