Force event.phase to end.

Hi

I am trying to force an event.ended after a period of time or if the user does not release from a touch event.

I am getting an error inside the timer, event.phase = “ended” --attempt to index local ‘event’ (a nil value)

Maybe not the best way to do this. If I have a falling object under gravity sometimes the phase ended is not fired as the object has moved to quickly or the user does not release?

function splat(event)

if  event.phase == “began”  then

 splatX = event.x

 splatY = event.y

 needToFire = true

 Print “Event started”

   myTimer = timer.performWithDelay(1000, function() 

   local event

   event.phase = “ended”

   myTimer = nil

   end,1)

elseif (event.phase == “ended”  and needToFire == true) then 

   if myTimer ~= nil then

   timer.cancel(myTimer); myTimer = nil

   end

needToFire = false

 print “Event Ended”

 return true

end

Andrew,

Have you considered taking the action you want to accomplish in the “began” phase instead?  This is sometimes a better solution than waiting for a user to lift their finger.

Oh, and if I’ve missed the point of your question, don’t forget about setFocus()

This will ensure you get the “ended” or “cancelled” response.

-Ed

Hi Ed

The set focus was helpful thank you.  I still have the issue when the user does not lift their finger.

needToFire = true set true on event.began with the below enterFrame listener and function, and set false on ended. Basically its an explosion that does not stop until the user releases.  Is there a better way or can I force an event ended. 

Runtime:addEventListener( “enterFrame”, handleEnterFrame )

local function handleEnterFrame( event)

if (needToFire == true) then

fireLasers()

end

end

Hi @andrew085,

You can “force” events by using the “:dispatchEvent()” API. It’s detailed here:

http://docs.coronalabs.com/daily/api/type/EventListener/dispatchEvent.html

Take care,

Brent

Andrew,

Have you considered taking the action you want to accomplish in the “began” phase instead?  This is sometimes a better solution than waiting for a user to lift their finger.

Oh, and if I’ve missed the point of your question, don’t forget about setFocus()

This will ensure you get the “ended” or “cancelled” response.

-Ed

Hi Ed

The set focus was helpful thank you.  I still have the issue when the user does not lift their finger.

needToFire = true set true on event.began with the below enterFrame listener and function, and set false on ended. Basically its an explosion that does not stop until the user releases.  Is there a better way or can I force an event ended. 

Runtime:addEventListener( “enterFrame”, handleEnterFrame )

local function handleEnterFrame( event)

if (needToFire == true) then

fireLasers()

end

end

Hi @andrew085,

You can “force” events by using the “:dispatchEvent()” API. It’s detailed here:

http://docs.coronalabs.com/daily/api/type/EventListener/dispatchEvent.html

Take care,

Brent