Hi Guys,
I’m a new developer here and I’m still evaluating Corona. So far so good but I’ve hit a brick wall.
From everywhere in my object I can access its properties and methods using ‘self’, whether the method is called from within the object or externally. There is a baffling exception to this. When a method is triggered (using ‘self’) from a handler attached to a graphic created within the object, the properties and methods of the object are not available from the called method using ‘self’ even though the method is called successfully.
For example:
MyObject = {}
function MyObject:new( x, y, rotation )
-- create graphics
local image= display.newImage( "image.png" );
-- place on screen
image.x, image.y = x, y
image.rotation = rotation;
--create object
local object = { test = "RESPONSE" }
object.image = image;
setmetatable(object, { \_\_index = MyObject }) -- Inheritance
--click listener
image:addEventListener( "tap", self.click);
return object;
end
function MyObject:select()
print(self.test);
return true;
end
function MyObject:selectObject()
self:select();
end
I edited this down to simplify the issue. Hopefully I’ve not put in any typos. Basically the issue is that if I click on the image in the object, the ‘select()’ method fires okay but ‘self.test’ prints only ‘nil’. If I point the event handler at 'selectObject() I get an error because the ‘self:select()’ method is evaluating as ‘nil’. But if I run ‘select()’ or ‘selectObject()’ from elsewhere within the object or externally it prints “RESPONSE” As expected.
Am I missing something here? Or is this a bug? [import]uid: 31362 topic_id: 5895 reply_id: 305895[/import]
[import]uid: 31362 topic_id: 5895 reply_id: 20224[/import]
Thanks! I went with the second example because I assume that print(obj.test) would have slightly better performance compared to print(imageClicked.myparent.test) - assuming I’m doing something more complex than printing. Or am I over simplifying?