Hey Corona devs,
I’ve got a simple custom component and I’m specifying an onEvent method as follows:
local button1 = customComponent.create({label="Label1", onEvent=onEvent1}) local button2 = customComponent.create({label="Label2", onEvent=onEvent2}) function onEvent1() print('onEvent1') end function onEvent2() print('onEvent2') end
The issue I’m facing is that the ‘onEvent2’ method is being called when I tap button1 (even though I’ve specified it to call onEvent1).
This is an example of how I’m creating the customComponent (stripped down)
local M = {} local handleTouchEvent local onEventHandler function M.create( options ) view = display.newGroup() onEventHandler = options.onEvent view:addEventListener("touch",handleTouchEvent) -- (THIS DOES NOT WORK AND IS MY ISSUE) -- view:addEventListener("touch",options.onEvent) -- (COMMENTED OUT, BUT THIS WORKS AS EXPECTED) return view end handleTouchEvent = function(event) -- do some custom stuff here -- ... -- now call the onEvent method passed in via the options var onEventHandler(event) end
How can I get each custom component above to call their respective onEvent functions? I’m pretty sure it’s something to do with scoping.
Thanks 