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!

Listen for the "touch" event, it has phases for "began" and "ended".

But I want to hold it not just have it move a bit at a time

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.

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 :P HAHA! thanks again for helping

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 )

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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 --]]

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

@ leom271 - Glad you got it working!

@ MichaelAssadi - You're welcome, I'm mainly just glad Ice is being used :-)

views:1353 update:2011/12/28 9:26:54
corona forums © 2003-2011