Hi, i’ve followed some guides on how to implement OOP with metatables in my game. Everything was cool until I noticed I had to make all my own functions for corona functions like setFillColor and such (maybe i’m wrong here).
The thing is that when I tried to use a touch even listener, i was facing the same error, the image generated is not recognized as such.
This is a test project to explain what i’m trying to do:
--character.lua local character = {} local character\_mt = {\_\_index = character} function character.new(x,y,width,height,group) local image = display.newRect(x,y,width,height) local newCharacter = { image = image} image.anchorX = 0 image.anchorY = 0 if group then group:insert(image) end return setmetatable(newCharacter,character\_mt) end function character:setColor(r,g,b,a) self.image:setFillColor(r,g,b) a = a or 1 self.image.alpha = a end
--main.lua local character = require("character") --all ok, creates it and local myChar = character.new(400,200,100,100) myChar:setColor(0.5,1,1) --test function local function press(event) if event.phase == "ended" then print("pressed char") end end --doesnt work, myChar is nil myChar:addEventListener("touch",press)
So basically i would like to know how can I use an event listener in this case? And if it’s possible… how can I do to not have to create all the functions from scratch in my character.lua module (stuff like “image.alpha”, “image.isVisible”,etc,etc basically every property i dont wan’t to modify)?
Thanks!