About using closures for listener wrappers

Hi, did you read the documentation? It contains examples showing exactly what you are asking.

I always recommend people to check the online documentation first. It’s one of the strong points of Corona, and it will solve almost all of your challenges down the road learning this great SDK.

Here is a direct link

https://docs.coronalabs.com/api/library/widget/newButton.html

So, where is the closure in the web page you are providing ? And the argument passing of an id ?

You like to look down on people asking questions, I can do this too :

Open your page, here is a direct link to it : https://docs.coronalabs.com/api/library/widget/newButton.html 

Now, you press “Ctrl + F” then “function(” and you see there isn’t any example of closure in this web page.

Examine the listener definitions supplied on that docs page.  Notice that they all receive an ‘event’.

The event will have a reference to the button and from there you can get the ID.

To dig into this idea, try this to see what is passed in:

local button1 = widget.newButton( { id = "button1", shape = "roundedRect", width = 160, height = 160, cornerRadius = 6, onPress = function( event ) for k,v in pairs(event) do if( k == "target" ) then for l,m in (v) do print(k,l,m) end else print(k,v) end end end, } )

Tip: Using a closure in all but the simplest cases is not really the way to go anyways.

Define the listener in advance and pass it to the buttons(s).

PS - I’m pretty sure that @anaqim believed you would understand that the listeners and closure are functionally equivalent.  Thus the post meant, ‘take a look at the listener definitions in the docs’.

Ok, thanks.

And for my douchebag friend “Anaqim” :

The newButton doc only says : “onEvent : This callback function allows you to test for the event.phase of “began”, “moved”, or “ended”.” Not talking of the precise nature of the event nor talking of any other accessible attribute.

There is the word “touch” 3 times, but not linked to an object whose name would be “touch”.

Of course once you know the existence of this, it’s easy : https://docs.coronalabs.com/api/event/touch/index.html 

@tristan.robet - That is an inappropriate post.  You may not like the post and suggestion by @anaqim.  As well, you may not have liked the tone.  But it was correct.  All you need is in the docs.

UPDATE:   This is how one makes it onto my list of ‘do not help’ users.   That said, I don’t want to be too rushed in this response.  So, please follow the forums guidelines in the future.

UPDATE 2: Re: use of word ‘tone’ above.  I find it helpful to remember that ‘tone’ is a hard thing to measure in written words.  It is best to assume a message was written with the best of intentions and not read any emotion into it.

I don’t look down on people for asking questions, but arrogant behaviour like you are displaying here gives me a good reason not to answer at all in the first place.

I asked if you read the docs and provided the link telling you it has examples of exactly what you are asking about at the bottom. How is this wrong?

Then you come at me complaining that your assumed solution doesn’t show up when doing a page search?

RTFM

Seriously, if you can’t be bothered learning lua and Corona SDK, best of luck to you…

“How is this wrong?”

I guess u just didnt really read my original question, and you didnt read my detailed explanation on why your answer was wrong.

So keep your journey writing without reading if it’s what you like my friend

Thanks RG! 

I’m at my computer right now but the original answer was made on my phone in between things, which frankly, is not my preferred medium of communication, especially as spelling takes a large hit, so if my tone was understood as being not appropriate, that was not my intention, but since the reply was in a tone with a clear message I struggle to missunderstand… oh well, what can you do  :smiley:

It’s ok man… Just don’t go “RTFM” when you have no time to make a proper answer, especially when the answer is not in the manual page you are providing, I’m pretty sure nobody will take it with joy and love  :wink:

Now to inject humor into the post… :slight_smile:

Only time 03:06 … 3:18 applies

https://www.youtube.com/watch?v=1hoSYvwnv_E

Well it was a provoked response, so that’s that. I still claim the solution to your problem is on that page, as how you used listeners was wrong, which is why it didn’t work, and the correct way to do this is down on the page. This is also something used alot so our is important to get the concept right, or syntax if you like.

Pardon the spelling mistakes, back on mobile device :slightly_smiling_face:

No man, don’t go deeper in your wrongness, at this point, you still have not understood the original question if you think you brought anything to answer it : u brought nothing. Your very first answer was a “rtfm” and all your following answers too.

Next time u try to answer a question : first step is to understand it.

“I have a button, and I want to pass button id to a listener” there is no such example in the doc you provide : end of discussion.

seriously?  :blink:

the first example on the documentation page i provided goes like this:

local widget = require( "widget" ) -- Function to handle button events local function handleButtonEvent( event ) if ( "ended" == event.phase ) then print( "Button was pressed and released" ) end end -- Create the widget local button1 = widget.newButton( { left = 100, top = 200, id = "button1", label = "Default", onEvent = handleButtonEvent } )

by adding a single line (let me know if you cant find it), i get the id from the button passed to the function like this:

local widget = require( "widget" ) -- Function to handle button events local function handleButtonEvent( event ) if ( "ended" == event.phase ) then print( "Button was pressed and released" ) print(event.target.id) end end -- Create the widget local button1 = widget.newButton( { left = 100, top = 200, id = "button1", label = "Default", onEvent = handleButtonEvent } )

this is fundamental lua and corona behaviour, something you’d know if you’d actually read the documentation instead of embarking on this pissing contest.

and for the record, my RTFM comment was not my first reply post but my second post after reading your less than lovely reply to my first post.

Classic Corona forum thread. Ask a question that has twelve thousand answers on Google, get arsey when your laziness is pointed out.

local function listener() print("silence...") end Runtime:addEventListener("enterFrame",listener,-1)