"break" or "return" out of a function

Is there code that can be used to stop a function from running through the remainder of its code once it reaches a “break” or “return” statement?

I’m trying to use it with a tableview at the moment:

local function onRowTouch(event) if (NOACCESS == true) then       break/return(???) end --rest of code that would run as long as NOACCESS was not true end  

Thanks for your help!

return true

“return true” definitely works as an answer to my original question. Thanks!

Follow-up question for being so helpful…

“return true” is also the cause of my trying to create this workaround.

I am trying to fix an issue with “return true” not stopping a propagation of a touch even to the object (a tableview) below it.

It DOES stop the propagation if I use it before I use other functions, but once another event occurs, “return true” no longer stops propagation.

ANY idea what could possibly be the reason?

Do I need to recreate the function every time to “reactivate” the return true working?

I keep trying and testing different things, using print statement to figure out what’s happening.

I’ve simplified my code down to super basics to see this problem in action:

function BtnTapped(event)     if event.phase == "ended" then         print("BTN TAPPED")     end     return true end local function onRowTouch( event )     print("TAPPED HERE") end  

Here’s what happens:

(Scenario 1) When the app starts, I can press the button as many times I like and the “return true” stops it so I get the print statement “BTN TAPPED” without getting the “TAPPED HERE” message. Then I tap on the tableview and get the “TAPPED HERE” message. From this point on, whenever I tap the button, I get both “BTN TAPPED” and “TAPPED HERE” as the touch propagates through both.

(Scenario 2) When the app starts, I press on the tableview first to get the “TAPPED HERE” message. From this point on, whenever I tap the button, I get both “BTN TAPPED” and “TAPPED HERE” as the touch propagates through both.

I’ve also tried adding a “return true” to the onRowTouch function with no effect.

Ok so first things first on touch events you should always return true/false after you have finished doing whatever it is you need to do.

Here is a tutorial on the basics of it: http://coronalabs.com/blog/2013/10/01/tutorial-taptouch-anatomy/

I have never used the tableview as I always just build my own using the scrollView as I have more control over what happens, but saying that if you return true on both it shouldn’t be passing through like that (or at least i don’t think it should).

Given that have you tried changing your BtnTapped to a actual tap event instead of a touch event? 

If none of the above work for you let me know and I will write something up and try to replicate it real quick and find a work around :slight_smile:

The use of “return” for this purpose terminates the function.  The use of “break” would be to escape from a loop or if statement, and continue execution after the block being broken out of.

Rob

Wow! Christopher Bishop you are the man of the hour!

I have read more than a handful of questions about this issue on the Corona forums and over at Stackoverflow. I’ve tried many different search terms and never came across this tutorial and never heard about this Tap distinction.

After a bunch more testing, I found that a tableview’s onRowTouch event has phases including “tap”, “press” and “release”

When it was reacting as a “tap”, my button’s “touch” event continued beyond its “return true”

I will now be "tap"ping all my buttons in Corona instead of looking for a touch event.phase == “ended” unless I specifically need to distinguish between the press down and the release. 

Extra! Extra!

As I kept working through other features of my app, I later had it occasionally happen that the tableview underneath my button would receive the event.phase “press” and event.phase “release” BEFORE the button above received its tap event. So that is another thing to watch out for when a button is over a tableView with different phases than “began” and “ended”.

I learned much today. Thanks for your help and pointing me in the right direction!

return true

“return true” definitely works as an answer to my original question. Thanks!

Follow-up question for being so helpful…

“return true” is also the cause of my trying to create this workaround.

I am trying to fix an issue with “return true” not stopping a propagation of a touch even to the object (a tableview) below it.

It DOES stop the propagation if I use it before I use other functions, but once another event occurs, “return true” no longer stops propagation.

ANY idea what could possibly be the reason?

Do I need to recreate the function every time to “reactivate” the return true working?

I keep trying and testing different things, using print statement to figure out what’s happening.

I’ve simplified my code down to super basics to see this problem in action:

function BtnTapped(event)     if event.phase == "ended" then         print("BTN TAPPED")     end     return true end local function onRowTouch( event )     print("TAPPED HERE") end  

Here’s what happens:

(Scenario 1) When the app starts, I can press the button as many times I like and the “return true” stops it so I get the print statement “BTN TAPPED” without getting the “TAPPED HERE” message. Then I tap on the tableview and get the “TAPPED HERE” message. From this point on, whenever I tap the button, I get both “BTN TAPPED” and “TAPPED HERE” as the touch propagates through both.

(Scenario 2) When the app starts, I press on the tableview first to get the “TAPPED HERE” message. From this point on, whenever I tap the button, I get both “BTN TAPPED” and “TAPPED HERE” as the touch propagates through both.

I’ve also tried adding a “return true” to the onRowTouch function with no effect.

Ok so first things first on touch events you should always return true/false after you have finished doing whatever it is you need to do.

Here is a tutorial on the basics of it: http://coronalabs.com/blog/2013/10/01/tutorial-taptouch-anatomy/

I have never used the tableview as I always just build my own using the scrollView as I have more control over what happens, but saying that if you return true on both it shouldn’t be passing through like that (or at least i don’t think it should).

Given that have you tried changing your BtnTapped to a actual tap event instead of a touch event? 

If none of the above work for you let me know and I will write something up and try to replicate it real quick and find a work around :slight_smile:

The use of “return” for this purpose terminates the function.  The use of “break” would be to escape from a loop or if statement, and continue execution after the block being broken out of.

Rob

Wow! Christopher Bishop you are the man of the hour!

I have read more than a handful of questions about this issue on the Corona forums and over at Stackoverflow. I’ve tried many different search terms and never came across this tutorial and never heard about this Tap distinction.

After a bunch more testing, I found that a tableview’s onRowTouch event has phases including “tap”, “press” and “release”

When it was reacting as a “tap”, my button’s “touch” event continued beyond its “return true”

I will now be "tap"ping all my buttons in Corona instead of looking for a touch event.phase == “ended” unless I specifically need to distinguish between the press down and the release. 

Extra! Extra!

As I kept working through other features of my app, I later had it occasionally happen that the tableview underneath my button would receive the event.phase “press” and event.phase “release” BEFORE the button above received its tap event. So that is another thing to watch out for when a button is over a tableView with different phases than “began” and “ended”.

I learned much today. Thanks for your help and pointing me in the right direction!