Having read the docs it seems that it should be possible to do this:
Because objects can be reordered in the hierarchy, using integer
indices to access display group children is fragile. If you move a child
above its sibling, all integer indices in your code must be updated.In most cases, it’s easiest to refer to a display object by its variable name. Variables can be named whatever you wish except for names that begin with a number or names that are reserved by Lua or Corona. These naming rules apply to all variables in Lua, so please commit them to memory.
This would lead me to believe that the following works:
local g = display.newGroup() local rect = display.newRect(g, 0, 0, 50, 50) print(g[1]) -- Prints table reference to rect print(g[rect]) -- Prints nil but it should work?
I think I am misunderstanding something. I have had a look at the lua-users wiki Table Tutorial but that has not really helped me.
What I am ultimately trying to do is put 25 tiles (each tile is a display group containing 3 images) into another display group for organizational purposes so that I can reference the tile display groups by key and thus manipulate them. As the doc suggest using indexes is not really suitable for this kind of task because inserting new tiles into the group changes the order of the tiles in the table.
Well, what I’m after is getting the displayObject by reference and not index. I.e. g[“rect”] or g.rect without having to iterate through all the displayObjects in the group. Is that possible? It would be neater than keeping track of them in a separate table.