Long Press ......

Hi,
any one have idea about how to detect Long Press.

Thanks. [import]uid: 11428 topic_id: 17222 reply_id: 317222[/import]

Use timer.performWithDely() to check how long press has been going for :slight_smile: [import]uid: 52491 topic_id: 17222 reply_id: 64995[/import]

Hi Peach ,

In my code i have two option one for simple fire & one is power fire.

If is this power fire (It’s 3 in every level ) then allow long press event (change images of Tank during longtouch event) otherwise not.

So in this case i want know isLongPress before the “ended” phase of “Runtime:addEventListener(“touch”,onTouch)”.

[import]uid: 11428 topic_id: 17222 reply_id: 64998[/import]

You’d use a timer. You’d start it when the event.phase == “began” and end it on “ended” then see how long it was pressed for and if it was more than 2 seconds (or whatever) make it a long press.

Get it? [import]uid: 52491 topic_id: 17222 reply_id: 65011[/import]

Hi Peach,
Thanks

My problem is 70% solved with timer.
I was used timer.performWithDelay() in event.phase == “began” .
But problem is if fire is fast one after another then some time Powerfire is twice & second thing is in some case if it is not Longpress then also PowerFire is called.

My code is :

[lua]local isLongPress = false
local powerLaserCount = 0

local onTouch = function( event )
eventPhase = event.phase

if event.phase == “began” then
isLongPress = false

local timerlistenerCount
if powerLaserCount < 4 then

local function listenerCount:timer( event )

if event.count == 10 and eventPhase ~= “moved” then

if eventPhase ~= “ended” then
isLongPress = true
powerLaserCount = powerLaserCount + 1

– Set Power tank animation here

local function listenerCount2:timer( event )

– Stop Power tank animation here
local function laser_fire:timer( event )
fire(1)
end
fire_timer = timer.performWithDelay(300,laser_fire,1)
end
timer.performWithDelay(2000, listenerCount2,1)
end
end
end
timerlistenerCount = timer.performWithDelay(50, listenerCount,10)

end
end

elseif event.phase == “moved” then
– Set move code here

elseif event.phase == “ended” then
eventPhase = event.phase

if isLongPress == false then
function laser_fire:timer( event )
fire(0)
end
fire_timer = timer.performWithDelay(300,laser_fire,1)
end
end

end
Runtime:addEventListener(“touch”,onTouch)[/lua] [import]uid: 11428 topic_id: 17222 reply_id: 65362[/import]

I have added < lua > tags to your code. Please use them. (It makes it a lot easier to read.)

The firing twice issue just means you aren’t setting the ability back to false quickly enough; can you do it at the same time the (long) laser is fired? [import]uid: 52491 topic_id: 17222 reply_id: 65373[/import]

you can try this,

[lua]local myTimer
local isLong = false
local function detectLong(e)
if “began” == e.phase then
myTimer = timer.performWithDelay(1000,function ()
isLong = true
end)
elseif “ended” == e.phase then
if isLong then
print(“long”)
else
print(“not long”)
end
isLong = false
if myTimer then
timer.cancel(myTimer)
myTimer = nil
end

end

end
Runtime:addEventListener(“touch”,detectLong)[/lua]

:slight_smile: [import]uid: 12482 topic_id: 17222 reply_id: 65380[/import]