Change Button when touched

hello guys, i was wondering how would i make a function for a button so when its in the “begin” phase it changes that button to a hover type effect like a link and then when its in the “ended” phase, it goes on to the next scene with would be the level select screen.

and yes, im using the director class but just not exactly sure how i would use it, im a noob

local function play (event)
if event.phase = “begin” then
change button to hover

otherwise if event.phase = “ended” then
switch scene to level select

end
end

playbutton:addEventListener(“touch”, play)

[import]uid: 122076 topic_id: 21988 reply_id: 321988[/import]

For this you’d likely use ui.

See;

CoronaSDK > SampleCode > Interface > ButtonEvents

There is also the option of making the button a sprite and changing the frame depending on the event phase.

Peach :slight_smile: [import]uid: 52491 topic_id: 21988 reply_id: 87435[/import]

Hi Peach, I actually found a different way to do this but not sure if it would be a good way to do this so I would like your feed back and here is my code for example

local home1 = display.newImage(“home1.png”)
home1.x = 160
home1.y = 210

local home2 = display.newImage(“home2.png”)
home2.x = 160
home2.y = 210
home2.isVisible = false

local function hover (event)
if event.phase == “began” then
home2.isVisible = true

else if event.phase == “ended” then
home2.isVisible = false – gonna change to link to scene
end
end
end

home1:addEventListener(“touch”, hover)
this is just example code that i found out to work but still got to make it so when its in the ended phase, It will link to different scene with director class which I found out how to do [import]uid: 122076 topic_id: 21988 reply_id: 87550[/import]

This is fine; if you were doing it a lot in a scene it wouldn’t be the best way (memory) but with a button or two I think it’s perfectly acceptable.

Just make sure you are adding the objects to localGroup so they are cleaned up when you change scenes. (Otherwise you could still have them in the background, including the invisible one.)

Peach :slight_smile: [import]uid: 52491 topic_id: 21988 reply_id: 87612[/import]