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
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
if yes, how can i do such thin ^^
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:
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