Change Button Image

Hi I am new here and i want to change my Button Image. But it dont works. It should change the Image when i click on the Button.

local widget = require( "widget" ) local function handleButtonEvent( event ) if ( "ended" == event.phase ) then print( "Button was pressed and released" ) button2.defaultFile = "english.png" end end local button2 = widget.newButton( { width = 120, height = 120, defaultFile = "german.png", label = "button", onEvent = handleButtonEvent } ) -- Center the button button2.x = display.contentCenterX button2.y = display.contentCenterY \* 1.3 -- Change the button's label text button2:setLabel( "" )

The widget.newButton() API isn’t designed for doing that. It can use two images, one normal, and one for while it’s being pressed. There are multiple ways to make buttons. You might want to look at this tutorial which covers some image swapping techniques you might want to employee instead of a widget.newButton()

Rob

and i want that when i press the button again, that the image turn back to the first image.

this code works once. :frowning:

local language = "german.png" button = display.newImage(language) button.x = 180 button.y = 220 button:addEventListener("tap", button) function button:tap (event) print("a") button:removeSelf( ) button = nil if language == "german.png" then button = display.newImage("english.png") button.x = 180 button.y = 220 language = "english.png" elseif (language == "english.png") then button = display.newImage("german.png") button.x = 180 button.y = 220 language = "german.png" end end

@Rob thanks

it works when i remove

button:removeSelf( )

but then i have one above the other… Is it bad for performance?

You need to add your touch/tap handler to your re-created button. The tutorial should give  good examples on how to solve this.

Rob

The widget.newButton() API isn’t designed for doing that. It can use two images, one normal, and one for while it’s being pressed. There are multiple ways to make buttons. You might want to look at this tutorial which covers some image swapping techniques you might want to employee instead of a widget.newButton()

Rob

and i want that when i press the button again, that the image turn back to the first image.

this code works once. :frowning:

local language = "german.png" button = display.newImage(language) button.x = 180 button.y = 220 button:addEventListener("tap", button) function button:tap (event) print("a") button:removeSelf( ) button = nil if language == "german.png" then button = display.newImage("english.png") button.x = 180 button.y = 220 language = "english.png" elseif (language == "english.png") then button = display.newImage("german.png") button.x = 180 button.y = 220 language = "german.png" end end

@Rob thanks

it works when i remove

button:removeSelf( )

but then i have one above the other… Is it bad for performance?

You need to add your touch/tap handler to your re-created button. The tutorial should give  good examples on how to solve this.

Rob