I want to make a class, that holds some graphics and dispatch events from this class when a button is tapped… here is my simple code:
MyClass.lua:
local Class = {} local Class\_mt = { \_\_index = Class } -- metatable ------------------------------------------------- -- PRIVATE FUNCTIONS ------------------------------------------------- local function tapListener( e ) -- local; only visible in this module local event = { name = "customEvent", myscustomvar = "this is my custom var" } Class:dispatchEvent( event ) end ------------------------------------------------- -- PUBLIC FUNCTIONS ------------------------------------------------- function Class.new( ) -- constructor local rect = display.newRect( 0, 0, 50, 50 ) rect:addEventListener( "tap", tapListener ) local newClass = { body = rect } return setmetatable( newClass, Class\_mt ) end ------------------------------------------------- return Class
and then create this class and listen to some events being dispached:
local MyClass = require( "MyClass" ) local mynewclasss = MyClass.new() function myCustomEvent(e) print(e.myscustomvar) end mynewclass:addEventListener("customEvent", myCustomEvent)
How to make it work? Any ideas?