event.phase ended never trigger

Hey Guys,

I’m adding a touch event to an object like so:

object('touch', objectTouched)  

but when I get to the function:

local objectTouched = function(event) if event.phase == 'ended' then print('ended') end end

The event.phase == ‘ended’ is never triggered. Anyone experience this before? 

So I figured out the problem, the device never registers my finger going off the screen. Anyone know why that could be?

Hi jeremyapplebaum12

You need to set focus on touch event Object.

local objectTouched = function(event)

    if event.phase == “began” then

            display.getCurrentStage():setFocus( event.target )

   elseif event.phase == “ended” then

             print(“ended”)

             display.getCurrentStage():setFocus( nil )

   end

end

Best Regards,

Team, SP Technolab

www.sptechnolab.com

Thanks, that didn’t work. I’m also using scrollView so I’m wondering if that has something to do with it.

Hi @jeremyapplebaum12,

I just tested some basic code using :setFocus() as @SP Technolab suggests, and I’m getting the expected result when I drag my finger off the screen edge (on iPhone at least). So, something else must be going on in your setup or listeners or conditional logic.

Here’s the code I used:

[lua]

local testRect2 = display.newRect( display.contentCenterX,display.contentCenterY,display.contentWidth,display.contentHeight)

testRect2:setFillColor( 0.3 )

local testRect = display.newRect( display.contentCenterX-50,display.contentCenterY,display.contentWidth-100,display.contentHeight)

testRect:setFillColor( 1,0,0 )

local function objectTouched( event )

   if event.phase == “began” then

      display.getCurrentStage():setFocus( event.target )

      testRect:setFillColor( 1 )

   elseif event.phase == “ended” or event.phase == “cancelled” then

      display.getCurrentStage():setFocus( nil )

      testRect:setFillColor( 1,0,0 )

   end

   return true

end

local function object2Touched( event )

   if event.phase == “began” then

      display.getCurrentStage():setFocus( event.target )

      testRect2:setFillColor( 0,0,1 )

   elseif event.phase == “ended” or event.phase == “cancelled” then

      display.getCurrentStage():setFocus( nil )

      testRect2:setFillColor( 0.3 )

   end

   return true

end

testRect:addEventListener( “touch”, objectTouched )

testRect2:addEventListener( “touch”, object2Touched )

[/lua]

Hi jeremyapplebaum12

You need to set focus on touch event Object.

local objectTouched = function(event)

    if event.phase == “began” then

            display.getCurrentStage():setFocus( event.target )

   elseif event.phase == “ended” then

             print(“ended”)

             display.getCurrentStage():setFocus( nil )

   end

end

Best Regards,

Team, SP Technolab

www.sptechnolab.com

Thanks, that didn’t work. I’m also using scrollView so I’m wondering if that has something to do with it.

Hi @jeremyapplebaum12,

I just tested some basic code using :setFocus() as @SP Technolab suggests, and I’m getting the expected result when I drag my finger off the screen edge (on iPhone at least). So, something else must be going on in your setup or listeners or conditional logic.

Here’s the code I used:

[lua]

local testRect2 = display.newRect( display.contentCenterX,display.contentCenterY,display.contentWidth,display.contentHeight)

testRect2:setFillColor( 0.3 )

local testRect = display.newRect( display.contentCenterX-50,display.contentCenterY,display.contentWidth-100,display.contentHeight)

testRect:setFillColor( 1,0,0 )

local function objectTouched( event )

   if event.phase == “began” then

      display.getCurrentStage():setFocus( event.target )

      testRect:setFillColor( 1 )

   elseif event.phase == “ended” or event.phase == “cancelled” then

      display.getCurrentStage():setFocus( nil )

      testRect:setFillColor( 1,0,0 )

   end

   return true

end

local function object2Touched( event )

   if event.phase == “began” then

      display.getCurrentStage():setFocus( event.target )

      testRect2:setFillColor( 0,0,1 )

   elseif event.phase == “ended” or event.phase == “cancelled” then

      display.getCurrentStage():setFocus( nil )

      testRect2:setFillColor( 0.3 )

   end

   return true

end

testRect:addEventListener( “touch”, objectTouched )

testRect2:addEventListener( “touch”, object2Touched )

[/lua]