CoronaSDK: Passing function to a class

Thursday, July 10, 2014

Maybe is already answered but I cannot fid it and probably is because I cannot make the right question.... I will try to explain me:

I made a class that helps me to create buttons and when I click the button is executing a function that is inside the class. I do not know if it's possible but I would like to have the tap / touch function outside the class.

Here the code:


function, posY, size, title) -- constructor

local button = tools.loadText( title, posX, posY, "Roboto-Regular", size, {0/255, 0/255, 0/255})
group:insert( button )
button.alpha = 0

local buttonEffect =, posY, 600, button.contentHeight*2)

button:toFront( )

button.x = posX
button.y = posY
button.size = size
button.title = title


function button:show(delay)
print( button.title.." --> show" )
button:addEventListener( "tap", button ), { time = 900, alpha = 1, delay = delay, transition=easing.inQuart })

function button:tap(event) print( self.title.." --> click" ) self:removeEventListener( "tap", self )


return true


Here my main.lua

local button = require ("classes.Button")

button1 =, 1300, 38, "LOGIN")

-- As everytime I create a button will have the same animation but different behaviour, I would like to be notified here when user make the tap to have the code outside the class and be able to reuse this class in other projects.

I hope I explained myself.

Thanks in advance.