How can i remove multiple sprite sheets from a table

5 replies [Last post]
JASIL
User offline. Last seen 1 year 17 weeks ago. Offline
Joined: 16 Sep 2011

Hi ,

I have used several sprite sheets in my scene. i would like to remove all of them from a loop before go loop

i have tried below way but no luck
local spriteSheettbl={}

on story board createScene
local layOutassets=loqsprite.newFactory("spritesheet1")
spriteSheettbl[#spriteSheettbl+1]=layOutassets
lcoal motherstart=loqsprite.newFactory("spritesheet2")
spriteSheettbl[#spriteSheettbl+1]=motherstart
local motherend=loqsprite.newFactory("spritesheet3")
spriteSheettbl[#spriteSheettbl+1]=motherend

on story board exitScene

for i=1, #spriteSheettbl do
if spriteSheettbl[i]~= nil then
print("print tb :" .. tostring(spriteSheettbl[i]))
spriteSheettbl[i]:dispose()
end
end

it listing all sprite sheet table but texture memory not clearing.

is there any other way to do this please help me

Thanks
Jasil

Replies

Brent Sorrentino
User offline. Last seen 13 hours 44 min ago. Offline
Staff
Joined: 28 Nov 2012

Hello Jasil,

First thing: the tool you're using (SpriteLoq?) shouldn't be implementing the old, depreciated sprite sheets method. I see that you're calling :dispose() but this call was used in the old sprite method. The proper way to clean up sprite sheets in the current imageSheet-based method is to simply clear objects (sprites) that are using that sheet, then set the sheet itself to nil.

Second, you can loop through the table, but in this case you must loop through in reverse direction. This is a common misunderstanding in Lua.

Basically, let's say you have a table like this (each letter represents a sheet in your case):
{ A, B, C, D, E, F }

If you start at index 1 and remove it, the table becomes this:
{ B, C, D, E, F }

But now, the loop iterator (i) has moved to 2. So, it removes the object in position 2, which is "C", not "B", and you have this:
{ B, D, E, F }

Now the iterator moves to 3, and you get this:
{ B, D, F }

And finally, it considers the table limit reached, and the loop ends. You can see that you're left with 3 unremoved objects in the table, and thus, the cleaning process has only done half its task!

The solution is the loop through in a reverse direction, as such:

1
2
3
for i=#spriteSheettbl, 1, -1 do
  --remove process
end

As you can see, we've just told Lua to start at the END of the table and work backward (the -1 parameter) to the beginning of the table. Using our sample, we have this:
{ A, B, C, D, E, F }

The iterator starts at 6 and removes object "F", leaving this:
{ A, B, C, D, E }

Now the iterator is at 5, so it removes object "E"... and so forth, until the entire table is cleared out.

Hope this helps!
Brent

JASIL
User offline. Last seen 1 year 17 weeks ago. Offline
Joined: 16 Sep 2011

Hi Brent Sorrentino,

Thanks for your Quick Replay.

Can you please post a sample code for remove Objects from image based sprite sheet.

before dispose i used to clear the objects from the group for example

function clearSpriteGroupAndFreeMemory(ObjGroup)
for i=ObjGroup.numChildren,1,-1 do
local child = ObjGroup[i]
child.parent:remove( child )
end
end

All the sprite based objects should insert in to the Objects Group.

Thanks
Jasil

Brent Sorrentino
User offline. Last seen 13 hours 44 min ago. Offline
Staff
Joined: 28 Nov 2012

Hi Jasil,
Happy to help. Your loop to remove the sprites from the group(s) is fine, but do you want to only remove them from the group without clearing them from the display (screen)? If you want to clear them from the screen, you should just use:

1
2
3
4
for i=ObjGroup.numChildren,1,-1 do
  local child = ObjGroup[i]
  display.remove( child ) ; child = nil
end

This will also remove them from the group at the same time, because obviously they can't belong to a group if they're not on the screen. :)

After you do that, you can clear the table of sprite sheets using this:

1
2
3
for i=#spriteSheettbl,1,-1 do
  spriteSheettbl[i] = nil
end

That should clear out your texture memory... if it's not, I'll take a closer look at your code (if needed) and suggest what might be going wrong.

Brent

JASIL
User offline. Last seen 1 year 17 weeks ago. Offline
Joined: 16 Sep 2011

Hi Brent Sorrentino ,

Thank you so much. I will Check and get back you if i need any help required.
Could you please suggest any advanced Corona Learning Book Available on market

Thanks
Jasil

Brent Sorrentino
User offline. Last seen 13 hours 44 min ago. Offline
Staff
Joined: 28 Nov 2012

Certainly! Dr. Brian Burton has written 2 books for learning Corona. I'm not sure if you'd consider the following book "advanced", but it covers a wide variety of Corona/Lua topics.

http://www.burtonsmediagroup.com/books/mobile-app-development-with-corona-getting-started/

Brent

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.