Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Newb. Question about memory and tables. — Gideros Forum

Newb. Question about memory and tables.

kontinyukontinyu Member
edited March 2013 in General questions
Hi,
I'm creating some sprites and putting them in a table like this;
grass[#grass]:setScale(math.random(0.5,1),math.random(3,8))
grass[#grass].wetness = math.random(15,25)
grass[#grass]:setPosition(wx,h-144)
grass[#grass]:setAnchorPoint(1,1)
self:addChild(grass[#grass])
And i'm looping through all of them within a timer function like that;
for body,_ in pairs(grass) do
				if grass[body].wetness > 0 then
				grass[body].wetness = grass[body].wetness - 1
					if grass[body].wetness < 1 then
						self:removeChild(grass[body])
						collectgarbage()
					end
				end
			end
But my memory usage is always going up. AFAIK i should nill other values like this;
table.remove (grass ,body)
grass[body].wetness = nil
But i can't because when i do this i get this error;
""main.lua:186: attempt to compare number with nil""
In number 186 i got; ""if grass[body].wetness > 0"" then this line.

So what should i do to get rid of this sprite. I'm creating and removing hundreds of them. and they are consuming a lot of lua memory.

Comments

  • mertocanmertocan Member
    Accepted Answer
    I think you should use sprite class instead of using table. For example ,
    grass=Sprite.new()

    when you want to add something , grass:addChild(anything)

    when you want to remove anything , grass:removeChildAt(number)

    and if you want to get number of Childrens grass:getNumChildren()

    You can do everything that you can do with tables except some sorting or special functions.

    Likes: kontinyu

    +1 -1 (+1 / -0 )Share on Facebook
  • Ok that worked great for sprites. can i apply this same idea to physic bodies too?
    if yes, how can i do such thin ^^
  • Actually , i never used physic bodies but i think you just need to add bodies as a child to the sprite. Nothing changes.
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    Well not exactly, physics are just mathematical calculations, and you need to represent the physics through Sprite like objects (like Bitmap for example).

    To do this there are two main steps:
    you need to somehow assign specific physics body to specific Sprite object, usually it is done by setting sprite body property, as in:
    local sprite = Sprite.new()
    sprite.body = physicsBody
    Second step is updating the sprite based on physics body's position and rotation. This could be done in every enterFrame event.

    Here is a quick tutorial on how it works, you can also find more advanced tutorials on that website ;)

    http://appcodingeasy.com/Gideros-Mobile/Gideros-Box2D-basics

    Likes: mertocan, kontinyu

    +1 -1 (+2 / -0 )Share on Facebook
Sign In or Register to comment.