Change default and hover images on button widget

I have the following code vor a volume button (turn sound on/off):

[blockcode]

volBtn = widget.newButton{
labelColor = { default={255}, over={128} },
default=“sound-on.png”,
over=“sound-off.png”,
width=48, height=48,
onRelease = onVolBtnRelease – event listener function
}

[/blockcode]

The onVolBtnRelease function pauses and resumes the audio playback already:

[blockcode]

local function onVolBtnRelease()

if storyboard.isPlaying == true then
audio.pause()
storyboard.isPlaying = false
volBtn.default=“sound-off.png”
volBtn.over=“sound-on.png”
else
audio.resume()
storyboard.isPlaying = true
volBtn.default=“sound-on.png”
volBtn.over=“sound-off.png”
end

return true – indicates successful touch
end

[/blockcode]

The sound will stop and resume correctly, but the images won’t be changed. How can I solve this? Any idea? [import]uid: 106038 topic_id: 26180 reply_id: 326180[/import]

Sounds like you would have to recreate the button.

Example:

[code]
local volBtn

local function createVolButton(btnDefault, btnOver)
local theVolButton = widget.newButton{
labelColor = { default={255}, over={128} },
default=btnDefault,
over=btnOver,
width=48, height=48,
onRelease = onVolBtnRelease – event listener function
}

function theVolButton:destroy()
display.remove(self)
end

return theVolButton
end

–Create initial volume button
volBtn = createVolButton(“sound-on.png”, “sound-off.png”)

----Your handler
local function onVolBtnRelease()

if storyboard.isPlaying == true then
audio.pause()
storyboard.isPlaying = false
volBtn.default=“sound-off.png”
volBtn.over=“sound-on.png”
volBtn:destroy()
volBtn = createVolButton(“sound-on.png”, “sound-off.png”)
else
volBtn:destroy()
volBtn = createVolButton(“sound-off.png”, “sound-on.png”)
audio.resume()
storyboard.isPlaying = true
volBtn.default=“sound-on.png”
volBtn.over=“sound-off.png”
end

return true – indicates successful touch
end [import]uid: 84637 topic_id: 26180 reply_id: 106094[/import]

This works, thank you! I hoped not to have to create a new button, but if this is the only solution, I’ll take it :slight_smile: [import]uid: 106038 topic_id: 26180 reply_id: 106102[/import]