Is there a way to change the background color of a widget button?
I am using widget v2.0 and I am developing a quiz game so I would like to change the button background color to green if the user answered the question correctly or change it to red if the user clicked in the wrong answer.
Is there a way to change the background color of a widget button?
I am using widget v2.0 and I am developing a quiz game so I would like to change the button background color to green if the user answered the question correctly or change it to red if the user clicked in the wrong answer.
Thanks,
Renato
Hi.
Something like this should do the trick (to change the color of the buttons image):
local widget = require( "widget" ) -- Function to handle button events local function handleButtonEvent( event ) local phase = event.phase local target = event.target if "began" == phase then target:setFillColor( 255, 0, 0 ) elseif "ended" == phase then target:setFillColor( 0, 255, 255 ) print( "You pressed and released a button!" ) end end -- Create a group local group = display.newGroup() -- Create the button local myButton = widget.newButton { left = 100, top = 200, width = 150, height = 50, defaultFile = "default.png", overFile = "over.png", id = "button\_1", label = "Button", onEvent = handleButtonEvent, } -- Insert the button into a group: group:insert( myButton )
Is there a way to change the background color of a widget button?
I am using widget v2.0 and I am developing a quiz game so I would like to change the button background color to green if the user answered the question correctly or change it to red if the user clicked in the wrong answer.
Thanks,
Renato
Hi.
Something like this should do the trick (to change the color of the buttons image):
local widget = require( "widget" ) -- Function to handle button events local function handleButtonEvent( event ) local phase = event.phase local target = event.target if "began" == phase then target:setFillColor( 255, 0, 0 ) elseif "ended" == phase then target:setFillColor( 0, 255, 255 ) print( "You pressed and released a button!" ) end end -- Create a group local group = display.newGroup() -- Create the button local myButton = widget.newButton { left = 100, top = 200, width = 150, height = 50, defaultFile = "default.png", overFile = "over.png", id = "button\_1", label = "Button", onEvent = handleButtonEvent, } -- Insert the button into a group: group:insert( myButton )
Hey guys, I try to use that function with the latest daily build but it didn’t worked out. The color changes when I release the button, but not while I’m still pressing it.
Thanks that the widgets library is open source, I was able to find a work around, it’s not pretty but it is working. This is my work around:
[lua]
– Function to handle button events
local function handleButtonEvent( event )
local phase = event.phase
local target = event.target._view._over
if “began” == phase then
target:setFillColor( 255, 0, 0 )
elseif “ended” == phase then
target:setFillColor( 0, 255, 255 )
print( “You pressed and released a button!” )
end
end
[/lua]
The minor change was to reference the target as “event.target._view._over” instead of “event.target” directly.
I hope this might help somebody until the original solution works again.
Hey guys, I try to use that function with the latest daily build but it didn’t worked out. The color changes when I release the button, but not while I’m still pressing it.
Thanks that the widgets library is open source, I was able to find a work around, it’s not pretty but it is working. This is my work around:
[lua]
– Function to handle button events
local function handleButtonEvent( event )
local phase = event.phase
local target = event.target._view._over
if “began” == phase then
target:setFillColor( 255, 0, 0 )
elseif “ended” == phase then
target:setFillColor( 0, 255, 255 )
print( “You pressed and released a button!” )
end
end
[/lua]
The minor change was to reference the target as “event.target._view._over” instead of “event.target” directly.
I hope this might help somebody until the original solution works again.