How do you make a list of display objects?

I have been using Corona / LUA for 6-8 weeks and have a nice game in development. However I’m stuck on the best way to create lists of game / display objects…

I have a function that creates a new enemy using sprite.newSprite( spriteSet1 ) and then adds extra records like type, directions, behaviours etc.

enemy = spawnNewEnemy (type, xpos etc…)

this works, but what if I have 5 enemies and I want to access the 3rd enemy, I have tried the code below but it doesn’t work

enemyList = {}

for i = 1,5 do

enemy = spawnNewEnemy( data in here…)

enemList[i] = enemy – using enemyList{} to store each enemy —

end

when I try to access the 3rd enemy e.g. enemy = enemyList[3] it returns nil…

is this because I’m trying to make a table of a table?

Thanks for help, the LUA manual doesn’t seem to shed much light on this.

PS The only work around I can see is to create a record that contains a unique identifer and then cycle through all the objects looking for that…

[import]uid: 3093 topic_id: 10776 reply_id: 310776[/import]

I’m not an expert, but I do something similar and it works
make sure to include ‘return enemy’ in the spawnNewEnemy function
and I would change
enemy = spawnNewEnemy( data in here…)
to
local enemy = spawnNewEnemy( data in here…) [import]uid: 49842 topic_id: 10776 reply_id: 39154[/import]

I don’t know if that typo is just here or in your actual code, but you wrote:

enemList[i] = enemy [import]uid: 12108 topic_id: 10776 reply_id: 39172[/import]

thanks for the input guys,

using a local variable got this to work

local enemy = spawnNewEnemy( data in here…)

I can’t see why using a global makes a difference in this case but I guess there’s a reason :slight_smile: the typo was just my writing up the post not from the source code

[import]uid: 3093 topic_id: 10776 reply_id: 39180[/import]