Adding functions to display objects that can access self

I have a group that I’ve attached an update() function to. The function is meant to resize all the elements within the group:

[lua]local map = display.newGroup()
map.update = function(scale)
– scale all children
end[/lua]

What I want to know is how I can get the function to refer to the display object directly with [lua]self[/lua], since more than one instance of this map may be generated.

I want to be able to access the children, as well as other variables and attributes I’ve bolted on to the group. [import]uid: 52771 topic_id: 17926 reply_id: 317926[/import]

I finally found the answer. I can use the colon syntax to create methods as long as the functions aren’t local. The colon syntax automatically passes [lua]self[/lua] into the function.

[lua]local function newMap(x, y, name)
local map = display.newRect(x, y, 50, 50)
– add an attribute
map.name = name

– add a method to test self
function map:update()
print(self.name … ’ : ’ … self.x … ', ’ … self.y)
end

return map
end

– create two maps with different variables
local map = newMap(10, 10, ‘first’)
local map2 = newMap(100, 100, ‘second’)

– call the method to see if it can call self properly
map:update()
map2:update()[/lua] [import]uid: 52771 topic_id: 17926 reply_id: 68524[/import]