I have an event listener attached to an object. I want the event to be fired when the user touches the object, then I want to return the distance when the user drags his finger off the object and lifts it from the screen. I calculate the distance using event.x and event.xStart (and y ) upon event “ended”.
The problem is that if the user drags his finger off of the object before lifting it from the screen, nothing happens. How can I start the event when the object is hit and end it when the finger is lifted even when it is no longer on the object? [import]uid: 3018 topic_id: 2670 reply_id: 302670[/import]
As is shown in the many examples in the SDK and the docs, you need to lock the focus on to the object being touched:
display.getCurrentStage():setFocus(object)
Then catch the “moved” events and check the position of the touch as you’re doing now. Remember to setFocus(nil) in the “ended” and “cancelled” phases. [import]uid: 3953 topic_id: 2670 reply_id: 7820[/import]
-- Subsequent touch events will target button even if they are
-- outside the stageBounds of button
display.getCurrentStage():setFocus( self )
If I set the focus to the object, shouldn’t it return “ended” even when it is ended no longer touching the event? It doesn’t [import]uid: 3018 topic_id: 2670 reply_id: 7935[/import]
It returns “ended” when the touch ends; that is, when you lift the mouse or finger. Put this in your code at the beginning of your handler & it will become clearer: