Detect first touch while finger is on screen

Hello,

I am modifying the first tutorial, to have more logical behaviour on touch the balloon. I want the balloon to react on touch (not on tap), but to ignore all touch events, except the first one, while finger is on screen. I tried different methods, and the most succeful is this one:

touch\_status=0 local function pushBalloon( event )   if touch\_status==0 then     touch\_status=1     balloon:applyLinearImpulse( 0, -0.35, balloon.x, balloon.y )   end end local function releaseTouch()     touch\_status=0     return true end balloon:addEventListener( "tap", releaseTouch )  balloon:addEventListener( "touch", pushBalloon )

This is work for me, but skips some touches (maybe 1 touch on every 3-4 touches is skipped). This leave the feeling for laggy and unstable behaviour. Is it possible to fix this somehow? Thank you

[Please use the code edit button when posting code!]

If you don’t want to react to tap events, don’t listen for them. I think you are confusing the phases of the touch event with the touch and tap event types.

You should go back to the guide for handling input: https://docs.coronalabs.com/guide/events/touchMultitouch/index.html

If you don’t want to handle tap events then simply don’t listen for the “tap” event. (The penultimate line in your code should not be there.)

If you want to detect the initial contact and release of a user touch on the device you need to check for “began” and “ended” phases in the “touch” event listener function.

If you only want to handle one touch at a time you simply don’t enable multitouch. If you want to handle one touch on an object, but want multiple objects to be able to handle a touch each (at the same time), you should set a flag on the object in the “began” phase and ignore any other touches while the flag is set. For example:

system.activate("multitouch") -- remove this to allow only one touch on one balloon at any time local function touch(e) -- check that the balloon is not already handling a different touch event if (e.target.hasFocus and e.target.eventId ~= e.id) then return false end if (e.phase == "began") then display.currentStage:setFocus( e.target, e.id ) e.target.hasFocus = true -- this is the flag which is set when a touch has begun on the object e.target.eventId = e.id -- do something to the balloon here, like move it around... e.target.x, e.target.y = e.x, e.y return true elseif (e.target.hasFocus) then -- do something to the balloon here, like move it around... e.target.x, e.target.y = e.x, e.y if (e.phase == "moved") then else display.currentStage:setFocus( e.target, nil ) e.target.hasFocus = false e.target.eventId = nil end return true end return false end for i=1, 3 do local balloon = display.newCircle( i\*150, i\*120, 70 ) balloon:addEventListener( "touch", touch ) end

Thank you.

Checking for touch began phase do the job :).

[Please use the code edit button when posting code!]

If you don’t want to react to tap events, don’t listen for them. I think you are confusing the phases of the touch event with the touch and tap event types.

You should go back to the guide for handling input: https://docs.coronalabs.com/guide/events/touchMultitouch/index.html

If you don’t want to handle tap events then simply don’t listen for the “tap” event. (The penultimate line in your code should not be there.)

If you want to detect the initial contact and release of a user touch on the device you need to check for “began” and “ended” phases in the “touch” event listener function.

If you only want to handle one touch at a time you simply don’t enable multitouch. If you want to handle one touch on an object, but want multiple objects to be able to handle a touch each (at the same time), you should set a flag on the object in the “began” phase and ignore any other touches while the flag is set. For example:

system.activate("multitouch") -- remove this to allow only one touch on one balloon at any time local function touch(e) -- check that the balloon is not already handling a different touch event if (e.target.hasFocus and e.target.eventId ~= e.id) then return false end if (e.phase == "began") then display.currentStage:setFocus( e.target, e.id ) e.target.hasFocus = true -- this is the flag which is set when a touch has begun on the object e.target.eventId = e.id -- do something to the balloon here, like move it around... e.target.x, e.target.y = e.x, e.y return true elseif (e.target.hasFocus) then -- do something to the balloon here, like move it around... e.target.x, e.target.y = e.x, e.y if (e.phase == "moved") then else display.currentStage:setFocus( e.target, nil ) e.target.hasFocus = false e.target.eventId = nil end return true end return false end for i=1, 3 do local balloon = display.newCircle( i\*150, i\*120, 70 ) balloon:addEventListener( "touch", touch ) end

Thank you.

Checking for touch began phase do the job :).