disabling a Ui button

Can anyone please help me figure out how to disable a UI button. I am using the latest Ui however, I tried button1.isActive=false and the solution on the forum nothing works for me so far. He’s what I am trying to do, I want to Disabled button1, when Load button is touch and change the OverSCR image on button1 with another image. Here my code below please help me figure out what I am doing wrong here? Thank you for the help.

button1 = ui.newButton{
defaultSrc = “img.png”,
overSrc = “img”,
defaultX = 35,
defaultY = 40,
overX = 35,
overY = 40,
–onEvent = button1inactive,
–onPress = button1Click,
–onRelease=button1inactive,

id =”button1?,
text =”",
font =”Arial”,
textColor = { 51, 51, 51, 255 },
align = “left”,
size = 22,

}

LoadButton = ui.newButton{
defaultSrc = “img2.png”,
overSrc = “img2.png”,
defaultX = 35,
defaultY = 40,
overX = 35,
overY = 40,
–onEvent = button1inactive,
–onPress = button1Press,
–onRelease = button1Release,
id =”button1?,
text =”",
font =”Arial”,
textColor = { 51, 51, 51, 255 },
align = “left”,
size = 22,
}

function LoadButton:touch( event )

if event.phase == “release” and button1.isActive and X==Y then

button1.isActive = false

– I also want the change the overSrC image for button1 here. How to do that?
esle
button1.isActive=true

end

end

LoadButton:addEventListener (“touch”, LoadButton)

Thank you in advance [import]uid: 104131 topic_id: 19555 reply_id: 319555[/import]

I do this in my game.

try this:

[lua]-- Forward reference so the event handlers can
– access these variables prior to them being
– created
local button1, button2

function button1Event( event )
if ( event.phase == “release” and button1.isActive then
– button 1 pressed
– do something here
– now toggle it off and show button2
button1.isVisible = false
button2.isVisible = true
end
end

button1 = ui.newButton{
defaultSrc = “img.png”,
overSrc = “img.png”,
defaultX = 35,
defaultY = 40,
overX = 35,
overY = 40,
onEvent = button1Event,
id =”button1?,
text ="",
font =”Arial”,
textColor = { 51, 51, 51, 255 },
align = “left”,
size = 22,
}

function button2Event( event )
if ( event.phase == “release” and button2.isActive then
– button 2 pressed
– do something here
– now toggle it off and show button1
button1.isVisible = true
button2.isVisible = false
end
end

button2 = ui.newButton{
defaultSrc = “img2.png”,
overSrc = “img2.png”,
defaultX = 35,
defaultY = 40,
overX = 35,
overY = 40,
onEvent = button2Event,
id =”button2?,
text ="",
font =”Arial”,
textColor = { 51, 51, 51, 255 },
align = “left”,
size = 22,
}

– Set the first button as showing and hide the second button
button1.isVisible = true
button2.isVisible = false[/lua]

NOTE: You can also just have one event handler and check the event._id value to compare it to “button1” or “button2” and do what you need based on that. [import]uid: 16734 topic_id: 19555 reply_id: 75934[/import]

Thank you for the help. I appreciate it. Do you know if it is possible to change the button image?

Ex. button1:setnewImage(“img”) – I don’t know if it is possible to change the image after the button is defined.

Thanks again.

[import]uid: 104131 topic_id: 19555 reply_id: 75998[/import]

@e1fabre,

Why do you want to change the button image? It would help me figure out a solution to your issue if you can tell me what you want to do.

For instance, I needed to be able to show a “disabled” image during certain phases of my game. In order to do this I could do one of two things. I could hide my button and show a separate image, or I could modify the ui code to handle this for me. I chose the latter.

Also, I notice you aren’t using the “over” image. Well, you use it, but you are specifying the same image as your default image. This means the user will have no visual feedback when they press a button. You may want to create a separate image for the button press (this would be the “over” image). [import]uid: 16734 topic_id: 19555 reply_id: 76025[/import]

Thanks Ken. I wanted another image so that the button could be highlighted when it is touched. I figure out another way to do it by setting the button.alpha=0.5.

My big problem now is that I want to make sure when the button1.alpha=0.5 some of my ui buttons are disabled. Here’s my code below. I am trying to figure out why the Function1 below works when using “if button1.isActive==false and button2.isActive==false” as a condition and same function Function1A doesn’t work when using “if button1.alpha==.5” as a test condition. Sorry for the long email. Basically all I want to do is when the button1.alpha=0.5 disable some buttons. I just can’t figure it out. Thanks again Ken for the help.

function guessbutton1: touch (event) then

if (event.phase==“ended” ) then
if (button1.isActive==true) then

if guess1text==b1text then
button1.alpha=0.5

–enabledBnt() – This function works

enabledBnt2() --this function does not work when condition is button1.alpha=0.5

else
button2.isActive=true
button3.isActive=true

end

end

end

return true
end

guessbutton1:addEventListener (“touch”, guessbutton1)

Function1-- This function works if used “if button1.isActive==false and button2.isActive==false” as test see below
enableBnt=function()

if button1.isActive==false and button2.isActive==false then
button1.isActive=true
button1.isActive=true
button2.isActive=true
button3.isActive=true

else

button1.isActive=false
button1.isActive=false
button2.isActive=false
button3.isActive=false

end
end

–Function1A- This same function doesn’t work if when I use button.alpha=0.5. “if button1.alpha==.5” as test condition.
Please See below. I need to test for button.alpha not show how.

enableBnt2=function()

if button1.alpha==.5 then

button1.isActive=true
button1.isActive=true
button2.isActive=true
button3.isActive=true

else

button1.isActive=false
button1.isActive=false
button2.isActive=false
button3.isActive=false

end
end
[import]uid: 104131 topic_id: 19555 reply_id: 76273[/import]

@e1fabre,

You should just use the overSrc image for your button press image. I realize it is named poorly, but the intent is to have one image for up (normal) and one frame for down (pressed).

defaultSrc -> this is your normal/up button
overSrc -> this is your pressed/down button

if you need to enable/disable other buttons why not just use a local variable. Something like:

[lua]local showSetOne = true

function updateButtons()
if ( showSetOne == true ) then
button1.isVisible = true
button2.isVisible = true
button3.isVisible = false
button4.isVisible = false
else
button1.isVisible = false
button2.isVisible = false
button3.isVisible = true
button4.isVisible = true
end
end

– if some action occurs then change the set
showSetOne = false
updateButtons()[/lua] [import]uid: 16734 topic_id: 19555 reply_id: 76291[/import]

Thanks so much Ken. Your way of doing it works. Thanks again. [import]uid: 104131 topic_id: 19555 reply_id: 78788[/import]