Unable to detect event 'ended'

i have my code below, which to detect left/right click and move the player respectively.

 local function onArrowEvent(event)  
 local t = event.target  
 print('event.phase = ' .. event.phase)  
 if event.phase == 'began' then  
 if t.moveDir == 'left' then  
 moveX = moveX + (-3)  
 end  
 if t.moveDir == 'right' then  
 moveX = moveX + (3)  
 end  
 end  
 if event.phase == 'ended' then  
 if t.moveDir == 'left' then  
 moveX = moveX + (3)  
 end  
 if t.moveDir == 'right' then  
 moveX = moveX + (-3)  
 end  
 end  
  
 end  
 btnLeftArrow:addEventListener('touch', onArrowEvent)  
 btnRightArrow:addEventListener('touch', onArrowEvent)  

it worked fine if i touch & release the left/right button (coz event ‘began’, ‘ended’ are triggered)
But if i touch & drag till i am far away from the left/right button, only ‘began’ and ‘moved’ events are triggered. in such case, the ‘ended’ event will not be detect.

How can we get rid of such situation?

Thanks for the pointer [import]uid: 10373 topic_id: 22438 reply_id: 322438[/import]

THis is one of those things that keeps coming up.
Buttons are designed this way: if the user lets go when not on top of the button, you don’t fire.

One possibly better way is to NOT assign a listener to the buttons.
Instead assign one to the screen.
Detect events and see which button is being ‘pressed’ by checking the x,y co-eds of the event.
If it is over one of your buttons, do stuff.
If not, treat it in the same way as an ended phase.

Its how you create ‘hotspots’ in an image. [import]uid: 108660 topic_id: 22438 reply_id: 89460[/import]

Found the answer by jz adding in ‘setFocus’

[code]
local function onArrowEvent(event)
local t = event.target
print('event.phase = ’ … event.phase)
if event.phase == ‘began’ then

display.getCurrentStage():setFocus(t)

if t.moveDir == ‘left’ then
moveX = moveX + (-1 * playerMoveSpeed)
end
if t.moveDir == ‘right’ then
moveX = moveX + (playerMoveSpeed)
end
end
if event.phase == ‘ended’ or event.phase == ‘cancelled’ then
if t.moveDir == ‘left’ then
moveX = moveX + (playerMoveSpeed)
end
if t.moveDir == ‘right’ then
moveX = moveX + (-1 * playerMoveSpeed)
end

display.getCurrentStage():setFocus(nil)
end

end
btnLeftArrow:addEventListener(‘touch’, onArrowEvent)
btnRightArrow:addEventListener(‘touch’, onArrowEvent)
[/code] [import]uid: 10373 topic_id: 22438 reply_id: 89459[/import]

Using event.phase == “moved” can also be useful, depending on the type of app :wink: [import]uid: 52491 topic_id: 22438 reply_id: 89550[/import]