listening for only the touch ended event on a new scene // touch hold across scenes

I am new, but I have been making a lot of progress over the last three weeks.  I’ve gotten a lot of core work done (a main menu, authentication and FB login with parse.com, recording & uploading videos).  Now I am turning my attention to the playback experience.  Many thanks!

I am implementing a scapchat like feature where on the home screen you tap on a button, and then go to a new scene where you watch videos continuously until you remove your finger from the scene.

I am experiencing a problem because I have to re-tap to get an “ended” event, even though you are holding 
In the home screen, I trigger the video playback with a widget.onPress to trigger the watching scene. (This works.)

Then, in my watch scene I put a standard touch listener in:

[lua]local function touchListener( event )

  print( "touch phase = "…tostring(event.phase) )

    if ( event.phase == “ended” ) then  

    composer.gotoScene( “home” ) – go back home if you lift you finger

    end

    return true  --prevents touch propagation to underlying objects

end

[/lua]

 
And also this in scene:show, phase == “will”
 
[lua]Runtime:addEventListener(“touch”, touchListener)

[/lua]

It works, but only if you re-tap the video.  Nothing happens when you first lift your finger.
 

I found this previous conversation, but this technique seems to only work for a button within a scene and not when a user is already touching the screen from an onPress in a previous scene.

https://stackoverflow.com/questions/16473220/touch-hold-event-in-corona-sdk

Any ideas? Thanks!

Have you tried setting the focus to your touch? Also you should check for “cancelled” as well as “ended” in your logic if they stop touching for whatever reason you want to bail so in your example “cancelled” would also qualify :slight_smile:

function object:touch( event ) if event.phase == "began" then print( "Touch Began") -- set touch focus display.getCurrentStage():setFocus( self ) self.isFocus = true elseif self.isFocus then if event.phase == "ended" or event.phase == "cancelled" then print("Touch Ended") -- reset touch focus display.getCurrentStage():setFocus( nil ) self.isFocus = nil end end return true end object:addEventListener( "touch", object )

Thanks for the suggestion!  Unfortunately, I can’t get that to work.  I put your code in the scene that I want to detect the lifting of the finger without a “began” event, with this modification:

function Runtime:touch ( event )

But it still has the same issue that you have to first lift your finger to trigger a begin event.  And reading your code, it seems that the touch listener has to get a ‘began’ event first.

I could try to put the code back in the other file, so that it is in the same scene as the initial button press.  Unfortunately, I will then lose the composer benefits I was seeking.

But any other ideas how to do to detect a touch release event WITHOUT a ‘began’ event? 
 

I’m not sure I follow the problem.  If you get a began event that you don’t want, simply ignore it.

Rob

I was never able to get it to work as two separate scenes, but I found a workaround.  

If I put the video playback in the same scene as the button that launches the video, then it can all work as I intended.  

Thanks for your input Christopher and Rob!

Have you tried setting the focus to your touch? Also you should check for “cancelled” as well as “ended” in your logic if they stop touching for whatever reason you want to bail so in your example “cancelled” would also qualify :slight_smile:

function object:touch( event ) if event.phase == "began" then print( "Touch Began") -- set touch focus display.getCurrentStage():setFocus( self ) self.isFocus = true elseif self.isFocus then if event.phase == "ended" or event.phase == "cancelled" then print("Touch Ended") -- reset touch focus display.getCurrentStage():setFocus( nil ) self.isFocus = nil end end return true end object:addEventListener( "touch", object )

Thanks for the suggestion!  Unfortunately, I can’t get that to work.  I put your code in the scene that I want to detect the lifting of the finger without a “began” event, with this modification:

function Runtime:touch ( event )

But it still has the same issue that you have to first lift your finger to trigger a begin event.  And reading your code, it seems that the touch listener has to get a ‘began’ event first.

I could try to put the code back in the other file, so that it is in the same scene as the initial button press.  Unfortunately, I will then lose the composer benefits I was seeking.

But any other ideas how to do to detect a touch release event WITHOUT a ‘began’ event? 
 

I’m not sure I follow the problem.  If you get a began event that you don’t want, simply ignore it.

Rob

I was never able to get it to work as two separate scenes, but I found a workaround.  

If I put the video playback in the same scene as the button that launches the video, then it can all work as I intended.  

Thanks for your input Christopher and Rob!