Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Best way to remove a sprite that contains other sprites and bitmaps? — Gideros Forum

Best way to remove a sprite that contains other sprites and bitmaps?

pm1891pm1891 Member
edited October 2019 in Bugs and issues
Like the title says, I'm trying to come up with the most efficient way to remove a sprite that contains other sprites and bitmaps.

Say for example I've this following sprite. What should be the best way to remove it?
local group = Sprite.new()
local g1 = Sprite.new()
local g2 = Sprite.new()
group:addChild(g1)
group:addChild(g2)
 
local g3 = Sprite.new()
g2:addChild(g3)
 
local b1 = Bitmap.new(...)
g1:addChild(b1)
 
local b2 = Bitmap.new(...)
g2:addChild(b2)
 
local b3 = Bitmap.new(...)
g3:addChild(b3)
I'm trying to finetune this following function to remove the parent sprite-
local function clearSprite(group)
 
	local n = group:getNumChildren()
	print("N "..n)
	if n == 0 then
 
		if group.pool then
 
			group.pool:free(group) ---I'm pooling bitmaps. So if group is a bitmap, it gets freed and returned to its pool
			group.pool = nil
 
		end
 
		print("removeFromParent()")
 
		group:removeFromParent()
		group = nil
 
 
	else
 
		for i = n, 1, -1 do
 
			local child = group:getChildAt(i)
			clearSprite(child)
 
		end
 
		group:removeFromParent()
		group = nil
 
	end
 
 
end
I'm not sure it's working perfectly because I'm seeing a rise in the lua memory every time I use clearsprite(group).

So how do you guys remove sprites? Any and all ideas to tackle this thing are deeply appreciated. Thanks.

Comments

  • olegoleg Member
    edited October 2019
    Just remove the master sprite, no children need to be deleted
    g1,g2,g3,b1,b2,b3=nil
    group:removeFromParent()
    group=nil
     collectgarbage()
    I give this function as an example if you want to remove children without removing the master sprite
    function clear(sprite)
        while sprite:getNumChildren() > 0 do
            sprite:removeChildAt(1)
        end
    end
    clear(group)
     
     collectgarbage()
    my games:
    https://play.google.com/store/apps/developer?id=razorback456
    мій блог по гідерос https://simartinfo.blogspot.com
    Слава Україні!
    +1 -1 (+3 / -0 )Share on Facebook
  • @oleg - Thank you so much for your input. It'd so convenient if I can simply remove the parent sprite. But how does this affect the bitmap pooling though? Also, I'm seeing an increase in lua memory when I'm only removing the parent sprite.
  • Apollo14Apollo14 Member
    edited October 2019
    pm1891 said:

    @oleg - Thank you so much for your input. It'd so convenient if I can simply remove the parent sprite. But how does this affect the bitmap pooling though? Also, I'm seeing an increase in lua memory when I'm only removing the parent sprite.

    Do you collectgarbage() ?

    I've noticed that sometimes Lua doesn't auto-collect garbage at all (even after a long period of time). But after manual command 'collectgarbage()' everything is collected perfectly.

    Also you should 'removeAllListeners()' (if there were any)

    Likes: oleg, plicatibu

    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
    +1 -1 (+2 / -0 )Share on Facebook
  • pm1891 said:

    @oleg - Thank you so much for your input. It'd so convenient if I can simply remove the parent sprite. But how does this affect the bitmap pooling though? Also, I'm seeing an increase in lua memory when I'm only removing the parent sprite.


    image.png
    767 x 413 - 47K

    Likes: antix, plicatibu

    my games:
    https://play.google.com/store/apps/developer?id=razorback456
    мій блог по гідерос https://simartinfo.blogspot.com
    Слава Україні!
    +1 -1 (+2 / -0 )Share on Facebook
  • pm1891pm1891 Member
    edited October 2019
    @oleg , @Apollo14 - You guys are right. Simple removal of the parent group works fine. It's just not working for me. I guess I screwed up somewhere in my code. :|
    Thank you so much for taking the time to answer my question.

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • hgy29hgy29 Maintainer
    @pm1891, I think your code was almost correct, but it wasn't clearing up the parent sprite. I would have written it this way instead:
    local function clearSprite(group)
    	--Recursively clear all children
     	local n = group:getNumChildren()
    	print("N "..n)
    	for i = n, 1, -1 do
    		local child = group:getChildAt(i)
    		clearSprite(child)
    	end
    	-- Now clear this sprite
    	if group.pool then
    		group.pool:free(group) ---I'm pooling bitmaps. So if group is a bitmap, it gets freed and returned to its pool
    		group.pool = nil
    	end
    	group:removeFromParent()
    end

    Likes: plicatibu

    +1 -1 (+1 / -0 )Share on Facebook
  • pm1891 said:

    @oleg , @Apollo14 - You guys are right. Simple removal of the parent group works fine. It's just not working for me. I guess I screwed up somewhere in my code. :|
    Thank you so much for taking the time to answer my question.

    You may still have links to sprite children somewhere
    To prevent this, try this:
    local group = Sprite.new()
    group.g1 = Sprite.new()
    group.g2 = Sprite.new()
    group:addChild(group.g1)
    group:addChild(group.g2)
     
    group.g3 = Sprite.new()
    group.g2:addChild(group.g3)
     
    group.b1 = Bitmap.new(...)
    group.g1:addChild(group.b1)
     
    group.b2 = Bitmap.new(...)
    group.g2:addChild(group.b2)
     
    group.b3 = Bitmap.new(...)
    group.g3:addChild(group.b3)
    my games:
    https://play.google.com/store/apps/developer?id=razorback456
    мій блог по гідерос https://simartinfo.blogspot.com
    Слава Україні!
Sign In or Register to comment.