object oriented programming

hi,

how I can add 1 eventlistener to object, created from another object:

local rect={meta={},imagen= nil,nombre=nil,estado=nil}

function rect:new(nombre,x,y)

    self.nombre=nombre

    self.imagen=display.newImage(nombre…".png",x,y)

return setmetatable ({},self.meta )                                         

end 

function rect:run()

  self.estado =‘running’ 

  print ('estoy '… self.estado )

  return 

end 

rect.meta.__index=rect 

local personaje=rect:new(“leon”,200,200) 

print("personaje.nombre: "…personaje.nombre)

personaje:addEventListener(“tap”, run) ¿¿error??? 

thanks you 

Some quick but partial answers / questions:

  1. Look for posts by dmccuskey for an answer to this.

  2. Try to write your code in english, not spanish, so other people will understand your code - in the long run it is better, trust me.

  3. In function rect:run() you say “return”, but you don’t specify whay you return. Why is this line in there?

  4. Your code won’t find the function “run” because actually your function is called “rect.run(rect)” or “rect:run()”.

  5. Your OOP code seems to be fragmented and shuffled, with bits and pieces all over the place - maybe it’s me, but it looks weird. What is the base class / prototype, and what is the instance? You seem to mix and match, instead of having a clear division.

  6. Make sure your function to be called in the addEventListener method is NOT anonymous.

Sorry to give you half answers, but with some research these points hold the clue.

personaje has inherited all the properties and methods of rect, in fact if I put “personaje:run()” works fine

My problem is how to add the eventlistener to personaje which is a table…

thanks for your help I will continue investigating

Have you tried adding the eventListener to personaje.imagen? The reason I’m asking is because theoretically you need to add eventListeners to displayObjects.

hi thomas,

finally I have done as you say on personaje.imagen, but not as I was doing

I dont Know if there will be other ways to do better, but this works fine.

I’m starting to develop with corona, and sometimes I have trouble to find documentation and takes me a long time

local rect={meta={},nombre=nil,estado=nil,imagen={}}

function rect:new()   

return setmetatable ({},self.meta )                                         

end 

function rect:init(nombre,x,y)

    self.nombre=nombre…x

    self.imagen=display.newImage(nombre…".png",x,y)

local function onTap(event)

    if event.phase == “ended” then

       print(“RECT tapped!”…self.nombre )

    end

end

self.imagen:addEventListener(“touch”, onTap)

end

rect.meta.__index=rect

local personaje= rect:new()

personaje:init(“leon”,200,200)

THANK YOU VERY MUCH FOR YOUR INTEREST!!

Hi Antonio,

Good to see you’re learning - this is tricky stuff by the way, once you’ve mastered this you’ve just about done the hardest part.

Just want to let you know that you will probably have trouble removing the eventListener later on, because in your “removeEventListener” code Lua will not know what the onTap function is. A better solution is to make this function a “part” of your self object, by calling it self.onTap or something like that. That way you’ll be able to recall and specify this exact same function later on to remove it.

A common error is for people to declare a function like onTap again in another place, and then remove. It feels like you are removing the same function, because it is called the same, and does the same thing as the original onTap function, but actually it is another function than the first one! It’s like telling a brother to go to work in a factory, and then telling his twin brother (with the same name) to stop working there. You think the result is the same but it’s not: the first brother still keeps working :wink:

Some quick but partial answers / questions:

  1. Look for posts by dmccuskey for an answer to this.

  2. Try to write your code in english, not spanish, so other people will understand your code - in the long run it is better, trust me.

  3. In function rect:run() you say “return”, but you don’t specify whay you return. Why is this line in there?

  4. Your code won’t find the function “run” because actually your function is called “rect.run(rect)” or “rect:run()”.

  5. Your OOP code seems to be fragmented and shuffled, with bits and pieces all over the place - maybe it’s me, but it looks weird. What is the base class / prototype, and what is the instance? You seem to mix and match, instead of having a clear division.

  6. Make sure your function to be called in the addEventListener method is NOT anonymous.

Sorry to give you half answers, but with some research these points hold the clue.

personaje has inherited all the properties and methods of rect, in fact if I put “personaje:run()” works fine

My problem is how to add the eventlistener to personaje which is a table…

thanks for your help I will continue investigating

Have you tried adding the eventListener to personaje.imagen? The reason I’m asking is because theoretically you need to add eventListeners to displayObjects.

hi thomas,

finally I have done as you say on personaje.imagen, but not as I was doing

I dont Know if there will be other ways to do better, but this works fine.

I’m starting to develop with corona, and sometimes I have trouble to find documentation and takes me a long time

local rect={meta={},nombre=nil,estado=nil,imagen={}}

function rect:new()   

return setmetatable ({},self.meta )                                         

end 

function rect:init(nombre,x,y)

    self.nombre=nombre…x

    self.imagen=display.newImage(nombre…".png",x,y)

local function onTap(event)

    if event.phase == “ended” then

       print(“RECT tapped!”…self.nombre )

    end

end

self.imagen:addEventListener(“touch”, onTap)

end

rect.meta.__index=rect

local personaje= rect:new()

personaje:init(“leon”,200,200)

THANK YOU VERY MUCH FOR YOUR INTEREST!!

Hi Antonio,

Good to see you’re learning - this is tricky stuff by the way, once you’ve mastered this you’ve just about done the hardest part.

Just want to let you know that you will probably have trouble removing the eventListener later on, because in your “removeEventListener” code Lua will not know what the onTap function is. A better solution is to make this function a “part” of your self object, by calling it self.onTap or something like that. That way you’ll be able to recall and specify this exact same function later on to remove it.

A common error is for people to declare a function like onTap again in another place, and then remove. It feels like you are removing the same function, because it is called the same, and does the same thing as the original onTap function, but actually it is another function than the first one! It’s like telling a brother to go to work in a factory, and then telling his twin brother (with the same name) to stop working there. You think the result is the same but it’s not: the first brother still keeps working :wink: