Button that scales on press: How to?

I want to make a widget button scale up on press and scale back down on release.

Currently I can’t figure this out with the standard event handeling in the button widget.

if event.phase == "began" then --scale button up end if event.phase == "moved" and event is over button then --scale button up if event.phase == "moved" then and event is NOT over button then --scale button down if event.phase == "ended" then --scale button down

Basically the “moved” phase is not contingent on first receiving a “began” event.

In this logic, it works fine until you start the touch event off of the button, slide your finger onto the button, and then slide off it again. 

In this scenario It will scale the button up but never scale it back down.

Since there is not a ‘touch failed’ event I can’t get it to scale the button back to normal.

Any ideas how to get his to work?

Thanks!

Gullie667

Have you tried:

if event.phase == “ended” or event.phase == “cancelled” then

Never mind… I figured it out.

Basicly the event.phase == “moved” needs to check for both the event start position and current position.

If you have a better idea lemt me know. Mine looks like this: 

Note: sf.checkForHit checks coords for a bounding box hit

if event.phase == "moved" then local function checkForMovment() if sf.checkForHit( { event.xStart, event.yStart }, buttonGroup ) then if sf.checkForHit( { event.x, event.y }, buttonGroup ) then --Scales button back to normal size in the swapActiveButton function. scaleButtonSizeUp() else scaleButtonSizeNormal() end end end checkForMovment() end

Yeah, I tried lost of things including those but “cancelled” isn’t called unless the “began” event is hit.

“ended” is only called if if the button is “released” / untouch is ended over the button.

I’m pretty sure what you want to do is the default handling for a button widget (at least when using onRelease). It highlights as long as my finger is over it, but if I slide it away it unhighlights, and highlights again if I slide it back over the button. No extra code needed.

 Jay

This works fine until you add scaling to the button… which is outside of the widget button’s scope. Then getting it to function correctly requires checking both current event position as well as the start position.

Basiclly, your finger will active the widget.newButton { onEvent = handleButtonEvent, } thread reguardless of the starting screen position of a touch event.

So you can call the function handleButtonEvent()'s  “if event.phase == “moved” then” without the touch starting on the button.

How ever there is no ( documented ) corresponding cancelled phase for events that don’t originate with a button’s touch.

Have you tried:

if event.phase == “ended” or event.phase == “cancelled” then

Never mind… I figured it out.

Basicly the event.phase == “moved” needs to check for both the event start position and current position.

If you have a better idea lemt me know. Mine looks like this: 

Note: sf.checkForHit checks coords for a bounding box hit

if event.phase == "moved" then local function checkForMovment() if sf.checkForHit( { event.xStart, event.yStart }, buttonGroup ) then if sf.checkForHit( { event.x, event.y }, buttonGroup ) then --Scales button back to normal size in the swapActiveButton function. scaleButtonSizeUp() else scaleButtonSizeNormal() end end end checkForMovment() end

Yeah, I tried lost of things including those but “cancelled” isn’t called unless the “began” event is hit.

“ended” is only called if if the button is “released” / untouch is ended over the button.

I’m pretty sure what you want to do is the default handling for a button widget (at least when using onRelease). It highlights as long as my finger is over it, but if I slide it away it unhighlights, and highlights again if I slide it back over the button. No extra code needed.

 Jay

This works fine until you add scaling to the button… which is outside of the widget button’s scope. Then getting it to function correctly requires checking both current event position as well as the start position.

Basiclly, your finger will active the widget.newButton { onEvent = handleButtonEvent, } thread reguardless of the starting screen position of a touch event.

So you can call the function handleButtonEvent()'s  “if event.phase == “moved” then” without the touch starting on the button.

How ever there is no ( documented ) corresponding cancelled phase for events that don’t originate with a button’s touch.