Finger is down, move object.

Hello

I have an object on screen that needs to rotate (by lets say 5 degrees) when the user is touches a button. At the moment when I press the button, the object only moves 5 degrees then stops. Is there a way of testing that finger is still down and to keep the object rotating?

I’m guessing a runtime listener is needed? Any help would be lovely.

S [import]uid: 45932 topic_id: 12298 reply_id: 312298[/import]

Just off the top of my head you could use a timer.

[lua]function onPress (event)
– object rotation code here
end

function pressBtn (event)
if event.phase == “began” then
myTimer = timer.performWithDelay(1000,onPress,0)
elseif event.phase == “ended” then
timer.cancel(myTimer)
end[/lua]

That isn’t tested but you get the idea; there are lots of ways of handling things like this :slight_smile:

Peach [import]uid: 52491 topic_id: 12298 reply_id: 44779[/import]

If I understand you correctly, you want to have the object continue rotating when the button is still pressed, correct? If so, you were right, you have to add an event listener…or you can you can use Peach’s way. By the way Peach, It would help me a lot if you could take a look at my problem Here

[lua]_W = display.contentWidth;
_H = display.contentHeight;

local function Button( event )
local phase = event.phase
if “began” == phase then
function Rotate ()
square.rotation = square.rotation + 5;
end
Runtime:addEventListener(“enterFrame”, Rotate)
elseif “ended” == phase or “cancelled” == phase then
Runtime:removeEventListener(“enterFrame”, Rotate);
end
end

square = display.newRect(0, 0, 100, 100);
square.x = _W * 0.5;
square.y = _H * 0.5;
square:setFillColor(255,255,255);

local button = display.newRect( 0, 0, _W, _H)
button.x = _W * 0.5;
button.y = _H * 0.5;
button:setFillColor(0, 0, 0, 0)
button:addEventListener( “touch”, Button )
[import]uid: 17138 topic_id: 12298 reply_id: 44838[/import]

Thanks Khaodik! That’s exactly what I was looking for!

I couldn’t figure out how to call the runtime in the right place, but you’ve fixed it.

Cheers
S [import]uid: 45932 topic_id: 12298 reply_id: 44848[/import]

No problem, Happy to help! [import]uid: 17138 topic_id: 12298 reply_id: 44858[/import]

Sorry one final problem.

If I slightly move my finger from the button whilst the square is rotating, the square will always continue to rotate. Even when the level is reset, it continues to rotate.

I have to reset the simulator to stop it.

Any idea?

[import]uid: 45932 topic_id: 12298 reply_id: 44867[/import]

No worries, think it’s fixed.

I changed this line:

 elseif "ended" == phase or "cancelled" == phase then  

to this:

elseif "ended" == phase or "cancelled" == phase or "moved" == phase then [import]uid: 45932 topic_id: 12298 reply_id: 44870[/import]