How to access specific objects in display group by named index?

I am currently working with storyboard and have managed to implement display.newText and display.newImage objects to my display group (“self.view”, nicknamed “group”).

Then I found out that using “group:insert” on such objects enters them into a numbered array for the display group, and does not maintain their name as an index of that group.

For example, I would like to be able to insert a display.newImage object called “newCard” and then be able to reference it later by using “group.card”.

I have tried:

[lua]

local newCard = display.newImage( imageDirectory )

group.card = newCard

[/lua]

as opposed to

[lua]

local newCard = display.newImage( imageDirectory )

group:insert( newCard )

[/lua]

However newCard does not function in the display group properly when this is done using the first example. The second example works of course, but does not allow me to reference that object by named index, only by array position/number.

I have read the following documents:

http://docs.coronalabs.com/guide/media/displayObjects/index.html

http://developer.coronalabs.com/content/application-programming-guide-graphics-and-drawing

And although they discussed similar topics, I feel they did not adequately explain this specific issue. Any help (main question in bold above) would be much appreciated!!!

Just use newcard to access it
newcard.x
newcard.y

Thanks for the response jstrahan.

I don’t feel that’s a good option for this particular setup. Reason being, newCard won’t exist upon leaving the function that this is in (and that’s good, because I don’t want stray variables to keep referencing an image that is supposed to be destroyed along with the group it’s in eventually).

The only reference to this image once this function is done should be contained in the displayGroup, and when I modify that object I would like to be able to do so via indexing the displayGroup (group), albeit with a specific named index, not with the array position.

Do any other ideas come to mind? I’d be surprised if everyone just uses the array references for their display groups, what with the way positions will constantly be changing as you add and remove objects?

if the object is in a group then the object still exist. unless your over writing the prev. one. then only the last one can be referenced

Yes, I realize the object still exists, but in the setup I’m proposing the only thing referencing that object is the group. And that’s a good thing. My point is I can no longer use the local “newCard” reference to manipulate the object, because that reference won’t exist outside of the function (nor would I want it to).

perhaps if you post some code i would understand better sorry

i treat Corona objects as “black boxes” – i let them handle the display but don’t rely upon them to keep track of MY stuff the way I need it kept track of.

that said, it’s trivially easy to extend Corona objects to keep track of stuff the way YOU want to.  given what you’ve coded so far, all you need is to use BOTH of your code snippets:

group:insert(newCard) – this actually puts it in the display

group.card = newCard – this extends the group object with a reference to your object

so that at some later point you’d be able to do (for example, et al):

group:remove(group.card) – use that reference to remove from display

group.card = nil – and don’t forget to release that lingering reference

another approach would be to add a “list” for things added to the group, rather than individual properties, for example:

group.myHandyListOfStuff = {}

group.myHandyListOfStuff[“newCard”] = newCard

– now you have “named” access to those object references

(and 500 other developers would give you 500 alternate ways of doing similar, take your pick)

You can actually just combine your two snippets like this:

[lua]

local newCard = display.newImage( imageDirectory )  – Create the card

group:insert( newCard )  – Make the card a part of the group

group.card = newCard    – Create a custom property of the group to enable us to access the card by a string key

[/lua]

Hope this helps.

  • Andrew

@jstrahan

No need to apologize - I was just trying to clarify what I meant. I appreciate that you tried to help but I think we misunderstood each other. I would put bigger code snippets of my own in but there’s so much that’s interconnected in my program now I thought it would be easier to describe a generic circumstance.

@davebollinger and aukStudios

These approaches seem to work so far based on some quick testing I did. Thank you very much! I will report back here if things go pear-shaped. Hopefully this will be useful to others wondering the same thing.

Thanks again!

Just use newcard to access it
newcard.x
newcard.y

Thanks for the response jstrahan.

I don’t feel that’s a good option for this particular setup. Reason being, newCard won’t exist upon leaving the function that this is in (and that’s good, because I don’t want stray variables to keep referencing an image that is supposed to be destroyed along with the group it’s in eventually).

The only reference to this image once this function is done should be contained in the displayGroup, and when I modify that object I would like to be able to do so via indexing the displayGroup (group), albeit with a specific named index, not with the array position.

Do any other ideas come to mind? I’d be surprised if everyone just uses the array references for their display groups, what with the way positions will constantly be changing as you add and remove objects?

if the object is in a group then the object still exist. unless your over writing the prev. one. then only the last one can be referenced

Yes, I realize the object still exists, but in the setup I’m proposing the only thing referencing that object is the group. And that’s a good thing. My point is I can no longer use the local “newCard” reference to manipulate the object, because that reference won’t exist outside of the function (nor would I want it to).

perhaps if you post some code i would understand better sorry

i treat Corona objects as “black boxes” – i let them handle the display but don’t rely upon them to keep track of MY stuff the way I need it kept track of.

that said, it’s trivially easy to extend Corona objects to keep track of stuff the way YOU want to.  given what you’ve coded so far, all you need is to use BOTH of your code snippets:

group:insert(newCard) – this actually puts it in the display

group.card = newCard – this extends the group object with a reference to your object

so that at some later point you’d be able to do (for example, et al):

group:remove(group.card) – use that reference to remove from display

group.card = nil – and don’t forget to release that lingering reference

another approach would be to add a “list” for things added to the group, rather than individual properties, for example:

group.myHandyListOfStuff = {}

group.myHandyListOfStuff[“newCard”] = newCard

– now you have “named” access to those object references

(and 500 other developers would give you 500 alternate ways of doing similar, take your pick)

You can actually just combine your two snippets like this:

[lua]

local newCard = display.newImage( imageDirectory )  – Create the card

group:insert( newCard )  – Make the card a part of the group

group.card = newCard    – Create a custom property of the group to enable us to access the card by a string key

[/lua]

Hope this helps.

  • Andrew

@jstrahan

No need to apologize - I was just trying to clarify what I meant. I appreciate that you tried to help but I think we misunderstood each other. I would put bigger code snippets of my own in but there’s so much that’s interconnected in my program now I thought it would be easier to describe a generic circumstance.

@davebollinger and aukStudios

These approaches seem to work so far based on some quick testing I did. Thank you very much! I will report back here if things go pear-shaped. Hopefully this will be useful to others wondering the same thing.

Thanks again!