Make a Button Disappear

I’m trying to make a button disappear when you press on it. It would work, but I can’t place the button before the function, because the button needs the function in order to work. Here is the code:
local buttonHandler = function(event)
if event.phase == “release” then
if event.id == “play” then
bluebutton.x = display.contentWidth/0
bluebutton.y = display.contentHeight/0
myText.x = display.contentWidth/0
blueball.x = display.contentWidth/0
blueball.y = display.contentHeight/22
end
end
end
local playbutton = ui.newButton{
default = “buttonPurple.png”,
over = “buttonPurpleOver.png”,
onEvent = buttonHandler,
text = “Play”,
id = “play”,
size = 20,
emboss = true
}

playbutton.x = display.contentWidth/2
playbutton.y = display.contentHeight/1.6

Is it even possible to make it disappear by the press of itself? Any help is appreciated! [import]uid: 30901 topic_id: 5782 reply_id: 305782[/import]

Hey banana,

I was looking for the same thing last night. I’m very new to this so I’m not certain what this would look like without the timer but here is some code I found on the site that worked for me:

local function button1Release ()
director:changeScene(“See”, none)
timer.performWithDelay( 1000, function() removeButton1( button1 ) end )
end
And as part of my ui.newButton I have:

onRelease = button1Release
So the button disappears after 1 second.

Hope that helps.

Eric

[import]uid: 9059 topic_id: 5782 reply_id: 19870[/import]

that’s very likely to break. what if the director changes the scene before the timer fires?

chiquita just set a forward reference to your object or function

either of these should work…

[lua]local playbutton – forward reference so handler can see it

local buttonHandler = function(event)
if event.phase == “release” then
if event.id == “play” then
playbutton:removeSelf()
– change scene etc
end
end
end

– is already defined as local, therefore don’t redefine with local here
playbutton = ui.newButton{
default = “buttonPurple.png”,
over = “buttonPurpleOver.png”,
onEvent = buttonHandler,
text = “Play”,
id = “play”,
size = 20,
emboss = true
}[/lua]

or

[lua]local buttonHandler – forward reference so button can see it

local playbutton = ui.newButton{
default = “buttonPurple.png”,
over = “buttonPurpleOver.png”,
onEvent = buttonHandler,
text = “Play”,
id = “play”,
size = 20,
emboss = true
}

– is already defined as local, don’t redefine with local here
buttonHandler = function(event)
if event.phase == “release” then
if event.id == “play” then
playbutton:removeSelf()
– change scene etc
end
end
end[/lua]
[import]uid: 6645 topic_id: 5782 reply_id: 19874[/import]

Thanks jmp909. This is very helpful. [import]uid: 9059 topic_id: 5782 reply_id: 19877[/import]

please use the [lua] [/lua] tags around your code, it makes it easier for the rest of us to read, which means you’re more likely to get help :slight_smile: [import]uid: 6645 topic_id: 5782 reply_id: 20001[/import]

Thanks jmp909. I was actually going to ask how to do that in my initial reply. :slight_smile: [import]uid: 9059 topic_id: 5782 reply_id: 20032[/import]

Thank you. I knew about the forward references but I completely missed the part about leaving the “local” off of the definition. [import]uid: 19315 topic_id: 5782 reply_id: 20127[/import]