Event on custom class

This is my first steps with Corona and I’m probably missing something of basic…

I want to create a class that basically contains an image an some other attributes. I want to create a class because I’ll have to create many object with the same behavior… My problem is that I’m not able to add a listener on the object but only on the image and using the event on the image I cannot access to the other class attribute:

=====================================================================

local Bttn = {}

local function click(event)

   – I would change the value of the custom attribute but I cannot access it

end

– All the Bttn code goes in this function

function Bttn:new(param)

    local self = {} – This will be an instance of Bttn (Can be any type of lua table)

    

    --------------------------------

    – Private Variables

    --------------------------------

    local image = param.image or “images/Gray.png”

    

    --------------------------------

    – Public Variables

    --------------------------------

    self.custom = param.value or 0;

    self.img = display.newImage(image);

    self.img.width = 50;

    self.img.height = 50;

    self.img.x = param.x or 0;

    self.img.y = param.y or 0;

        

    --------------------------------

    – Private Functions

    --------------------------------

    – Note on private functions. They must always be specified in a previous line of code before use.

    

    --------------------------------

    – Public Functions

    --------------------------------

    

    self.img:addEventListener(“touch”, click);

    – Return the instance!

    return self

end

– When this file is required, it will return this Bttn!

return Bttn

=====================================================================

I’ve tried to add the event listener directly on the object on the object in this way but it doesn’t work:

self:addEventListener(“touch”, clickObj);

Thanks

self is just a table which has no addEventListener function. one way around this is to change self to this

self=display.newGroup()

then it should work

or you can create your own function

after self={}

function self:addEventListener(...) self.img:addEventListener( arg[1],arg[2] ) end

you will also need to make a removeEventListener in a same way

Thanks a lot jstrahan!

welcome

self is just a table which has no addEventListener function. one way around this is to change self to this

self=display.newGroup()

then it should work

or you can create your own function

after self={}

function self:addEventListener(...) self.img:addEventListener( arg[1],arg[2] ) end

you will also need to make a removeEventListener in a same way

Thanks a lot jstrahan!

welcome