Declare sprites in a middle of a game

Hi guys,

I have following problem.
I have a 100 sprites in my level.lua file.
They are consuming a lot of memory.
They are declared in sprite.lua like this:

local Sprites = {
{ image_sheet = myImageSoliderSheet, sequence_data = sequenceSoliderData },
{ image_sheet = myImageEnemiesSheet, sequence_data = sequenceEnemiesData },
– etc…
}

Then later called in level.lua like this:

local sprite_info = Sprites[math.random(#Sprites)]

sprite = display.newSprite( sprite_info.image_sheet, sprite_info.sequence_data)

My question:
Is there a way I can declare 10 of them at a time, and destroy previous 10 of them, and so on in a circle?
I do not want to destroy them permanetly, just free memory and call them later on.
Sorry for not highlighting my code (I post this by cellphone) :slight_smile:

Thank you.
Ivan

Yes, To be honest i have never dealt with sprites but what i would do is add a table and in that table there are ten sprites and when you need to remove the sprites you could remove the table and add a new one automatically with 10 new sprites.

Good Luck!

Thank you Sonic. But when I remove (nil) Table A and add Table B I assume I can never ever call Table A again during game play?

You can remove the table and then add it again right away like this

Lets say the table is 

local player = {}

and then 

timer.performWithDelay(1, function() for k,v in pairs( player ) do display.remove( v ) end player = {} end )

That removes the table and anything in the table and then adds a new one.

Credits for the code go to RoamingGamer

Thank you.
But, can I recall removed table ever again during gameplay?

Or it is lost forever (until new game)?

can you explain “recall”? 

Let’s say I start my game with Table A holding sprite 1 to 10, and nil Table A after a period of time (or score), and after that call/declare Table B holding sprite 11 to 20.

Can I ever again use (i.e. call) sprites 1 to 10, i.e. use Table A?

Well if you remove it then i dont think so but after you remove it you could make the same table again…

Yes, To be honest i have never dealt with sprites but what i would do is add a table and in that table there are ten sprites and when you need to remove the sprites you could remove the table and add a new one automatically with 10 new sprites.

Good Luck!

Thank you Sonic. But when I remove (nil) Table A and add Table B I assume I can never ever call Table A again during game play?

You can remove the table and then add it again right away like this

Lets say the table is 

local player = {}

and then 

timer.performWithDelay(1, function() for k,v in pairs( player ) do display.remove( v ) end player = {} end )

That removes the table and anything in the table and then adds a new one.

Credits for the code go to RoamingGamer

Thank you.
But, can I recall removed table ever again during gameplay?

Or it is lost forever (until new game)?

can you explain “recall”? 

Let’s say I start my game with Table A holding sprite 1 to 10, and nil Table A after a period of time (or score), and after that call/declare Table B holding sprite 11 to 20.

Can I ever again use (i.e. call) sprites 1 to 10, i.e. use Table A?

Well if you remove it then i dont think so but after you remove it you could make the same table again…