Can I *force* a touch-event cancellation?

Is there any way that I can force the system to cancel a touch event? Let’s say the user begins a touch, and starts moving around the screen. I basically want to force the event to cancel out after a piece of logic (that I set), thus effectively ending the entire touch event (and then, ideally, performing some other action upon that occurence).

From the documention on touch events, we have this:

event.phase is a string identifying where in the touch sequence the event occurred:
“began” : a finger touched the screen.
“moved” : a finger moved on the screen.
“stationary” : a finger is touching the screen but hasn’t moved from the previous event.
“ended” : a finger was lifted from the screen.
“cancelled” : the system cancelled tracking of the touch.

I want to set the phase to “cancelled”… but it says the system must cancel it. Can I instruct the system to do so via code? If so, how? Any help is appreciated!

Brent
[import]uid: 9747 topic_id: 3793 reply_id: 303793[/import]

supposedly you can dispatch an event

from the docs:
[lua]-- Sometime later, create an event and dispatch it
local event = { name=“myEventType”, target=image }
image:dispatchEvent( event )[/lua]

try setting the name to “cancelled” and then in your handler remember to call [lua]return true[/lua] to say that it is handled.

i can’t confirm this works but that’s your best bet [import]uid: 6645 topic_id: 3793 reply_id: 11558[/import]

Oh, this is interesting. Thanks for the pointer, @jmp909. Brent @IgnisDesign, did this work? Were you able to force the touch event to cancel?

Edit: I tried it and it gave me a runtime error:

?:0: attempt to index a nil value
stack traceback:
[C]: ?
?: in function ‘dispatchEvent’

All I did was to add two lines of code:

[lua]local event = { name=“cancelled”, target=myObject }
myObject:dispatchEvent()[/lua]

I’d so like to figure this one out. I’d appreciate any help.

Edit2: I guess what I don’t understand is how I might add return true to the handler. Not sure what might be the handler in this case… and if myObject is the handler, how would I “return true” to it???

Naomi [import]uid: 67217 topic_id: 3793 reply_id: 85998[/import]

after some testing i find that you can dispatch a custom phase
and if you want to remove touch events, you can try something like this

[lua]obj:dispatchEvent( { name = “touch”, target = obj, phase = “customPhase” } )[/lua]

then on the touch listener
[lua]function listener( event )
if event.phase == “moved” then
event.target.x = event.x
event.target.y = event.y
elseif event.phase == “customPhase” then
obj:removeEventListener( “touch”, listener )
end
end
obj:addEventListener( “touch”, listener )[/lua]

not sure if this is what you’re looking for…

edit: here’s a little app that demonstrates what i was talking about, basically you drag the big circle to the small circle
if it collides, it removes touch event, waits 3 secs, reset to original place then add the touch events back

[lua]display.setStatusBar( display.HiddenStatusBar )
local physics = require( “physics” )
physics.start( ); physics.setGravity( 0, 0 )

local _W = display.contentWidth
local _H = display.contentHeight

local obj = display.newCircle( 50, 50, 50 )
physics.addBody( obj, “dynamic” )
local checkpoint = display.newCircle( _W * 0.5, _H * 0.5, 10 )
physics.addBody( checkpoint, “dynamic”, { isSensor = true } )

function drag( event )
local phase = event.phase
local self = event.target

if phase == “moved” then
self.x = event.x
self.y = event.y
elseif phase == “customPhase” then
obj:removeEventListener( “touch”, drag )
timer.performWithDelay( 3000, function( )
self.x = 50
self.y = 50
obj:addEventListener( “touch”, drag )
end )
end
end
obj:addEventListener( “touch”, drag )

function collide( event )
local phase = event.phase
local self = event.target
local target = event.other

if phase == “began” then
print ( “collide” )
obj:dispatchEvent( { name = “touch”, target = obj, phase = “customPhase” } )
end
end
obj:addEventListener( “collision”, collide )[/lua] [import]uid: 114118 topic_id: 3793 reply_id: 86014[/import]

Edit: xplatgaming’s method makes more sense [import]uid: 44607 topic_id: 3793 reply_id: 86016[/import]

@xplatgaming and @kc, thank you for your tips!

It occurred to me last night as I went to bed about using setFocus, i.e., using runtime listener to fire when a flag tells it to fire, and it would nil out the focus from the object like this:

[lua]display.getCurrentStage():setFocus(nil);
myObject.isFocus = false;[/lua]

It might work, but it might not. If not, I’ll try the approach @xplatgaming suggested above.

Thanks again!

Naomi
[import]uid: 67217 topic_id: 3793 reply_id: 86134[/import]

Update: Brent @IgnisDesign posted how to use the dispatchEvent command properly here. I think I found the real solution for my problem at last. Thank you all.

Edit: @xplatgaming, I was a bit overwhelmed by your code sample when I saw it the first time this morning, but I see how you included the dispatchEvent command. All I need to do sometimes is to take a breath and tackle some complex & unfamiliar looking code one line at a time. Honestly, I should’ve done it the first time I saw it. Thanks again for the sample code you put together. It’s super.

Naomi [import]uid: 67217 topic_id: 3793 reply_id: 86182[/import]