transition.to question

Hello,

There are some problems with this code:

local function handleTouch( event ) if event.phase == "began" then audio.play(flush) transition.to(rect, {rotation = -20, time = 100}) elseif event.phase == "ended" then transition.to(rect, {rotation = 0, time = 100}) end end rect:addEventListener( "touch", handleTouch )

Sometimes when I touch the the ‘rect’ and let go, it goes to rotation -20 instead of back to 0. How can I fix this?

here is the full code

display.setStatusBar( display.HiddenStatusBar ) local rect = display.newRect( 123,0, 50, 12 ) rect:setFillColor( 0.64, 0.64, 0.64 ) rect.x = display.contentCenterX rect.y = display.contentCenterY local function handleTouch( event ) if event.phase == "began" then audio.play(flush) transition.to(rect, {rotation = -20, time = 100}) elseif event.phase == "ended" then transition.to(rect, {rotation = 0, time = 100}) end end rect:addEventListener( "touch", handleTouch )

Put in some print statements and see what values you are getting for your touch events.

Rob

Maybe try transition.cancel() before the rotation back to 0

It still won’t work. It only happens when I touch the top right and buttom left corners of the rect

Nevermind I fixed it. I changed it to Runtime:addEventListener.

Are you sure a runtime listener is what you want? That will make the handle move no matter where on the screen you touch.

I think the problem you were running into is that touch events are only passed to the object if they are actually on it* . When you touch the handle it moves. Depending on where you touch it, when you release, the event may not occur on top of it any more, and so there is no “ended” touch phase event to tell the handle to move back. You can see this easily with your code above. If you click and release in the centre of the handle, it works fine. If you do it toward the top left or bottom right corners, the handle moves out of your touch, and when you release the particular event is not on the handle, and so isn’t applied to it.

* unless you use setFocus()

Yes you’re right. How can I add  setFocus(  ) to my code so the handle works no matter where I touch it?

In the link I gave you, the example at the bottom shows exactly how to do it.

Put in some print statements and see what values you are getting for your touch events.

Rob

Maybe try transition.cancel() before the rotation back to 0

It still won’t work. It only happens when I touch the top right and buttom left corners of the rect

Nevermind I fixed it. I changed it to Runtime:addEventListener.

Are you sure a runtime listener is what you want? That will make the handle move no matter where on the screen you touch.

I think the problem you were running into is that touch events are only passed to the object if they are actually on it* . When you touch the handle it moves. Depending on where you touch it, when you release, the event may not occur on top of it any more, and so there is no “ended” touch phase event to tell the handle to move back. You can see this easily with your code above. If you click and release in the centre of the handle, it works fine. If you do it toward the top left or bottom right corners, the handle moves out of your touch, and when you release the particular event is not on the handle, and so isn’t applied to it.

* unless you use setFocus()

Yes you’re right. How can I add  setFocus(  ) to my code so the handle works no matter where I touch it?

In the link I gave you, the example at the bottom shows exactly how to do it.