I my quest of moving my functions to OOP and at the same time using metatables, i have some doubts.
I will give you a simple example of a spinner function I just remade using OOP and metatables.
function menu.spinner() local \_M = {x=display.contentWidth\*.5, y=display.contentHeight\*.5, width=128, height=128} local mt = { \_\_index = \_M } function \_M.start(self) self.view:start() end function \_M.stop(self) self.view:stop() end function \_M.delete(self) display.remove(self.view) self.view=nil self=nil end local instance = {} function instance.new(params) local widget = require( "widget" ) params=params or {} setmetatable(params, mt) params.view=widget.newSpinner( {x=params.x, y=params.y, width=params.width, height=params.height} ) return params end return instance end
to use you just need:
local spinnerClass=ui.spinner() local spinnerInstance=spinnerClass.new() spinnerInstance:start() --spinnerInstance:stop() --spinnerInstance:delete()
I’ve a lot of this simple functions that simplify my life building apps.
My question is. I’m I doing it right? is there a better version out there using OOP? if it was you how would you do it?
regards,
Carlos.