Holding down finger event?

Hi guys!

I have a spaceship with basic left and right movement but I’m trying to figure out how I can move him while holding the button instead of constantly tapping it?
Thanks for the help! [import]uid: 41389 topic_id: 19372 reply_id: 319372[/import]

Listen for the “touch” event, it has phases for “began” and “ended”. [import]uid: 5833 topic_id: 19372 reply_id: 74784[/import]

But I want to hold it not just have it move a bit at a time [import]uid: 41389 topic_id: 19372 reply_id: 74797[/import]

You wouldn’t have to move, when you detect the “began” phase just set a flag to mark the finger as down and then when you detect the “ended” phase the finger is up. Optionally you could measure the time between the two events to have long and short holds etc. [import]uid: 5833 topic_id: 19372 reply_id: 74798[/import]

Sorry I’m new don’t quite understand what your saying,

Ok so by flag you mean setup a variable that stores whether the touch is ‘began’ or ‘ended’ and the player moves by whatever the variable is? If so then how can I make the player move without stopping?

hope that made sense :stuck_out_tongue: HAHA! thanks again for helping [import]uid: 41389 topic_id: 19372 reply_id: 74802[/import]

I haven’t tested this but something like this should work:

[code]

local isMoving = false

local player = display.newRect( 0, 0, 50, 50 )

local onEnterFrame = function( event )

if isMoving then
player.x = player.x + 1
end

end

local onTouch = function( event )

if event.phase == “began” then
isMoving = true
elseif event.phase == “ended” then
isMoving = false
end

end

Runtime:addEventListener( “enterFrame”, onEnterFrame )
Runtime:addEventListener( “touch”, onTouch )

[/code] [import]uid: 5833 topic_id: 19372 reply_id: 74803[/import]

You could also do this, more simpler if your new. Just a personal preference.

[code]
local player = display.newRect( 0, 0, 50, 50 )
– Above is the Image. Just an example.

function holdPlayer(event)
– Add your function
end
player:addEventListener(“touch”, holdPlayer)
–[[For the above code, (player:addEventListener(“touch”, holdPlayer).
The image is called player, so you call it. Then it’s basically adding
a touch function enabling you to touch it. If you would like to tap it,
replace “touch” with “tap”. You would then call the function holdPlayer, the function.

That would be to make a touch function, you could then
use the code that GrahamRanson provided to move your object.
I’m not sure what you are really trying to get at so i’ll leave that alone.

By the way, thanks a lot GrahamRanson for creating Ice! It is extremely useful!

Michael --]] [import]uid: 23689 topic_id: 19372 reply_id: 74806[/import]

YES! Thank you so much guys I finally got it to work! I struggled to get it to work for the past 30 mins only to find out I misspelled a word haha!

Thanks again for the HUGE help [import]uid: 41389 topic_id: 19372 reply_id: 74810[/import]

@ leom271 - Glad you got it working!

@ MichaelAssadi - You’re welcome, I’m mainly just glad Ice is being used :slight_smile: [import]uid: 5833 topic_id: 19372 reply_id: 74812[/import]