Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Do I have to remove children in this case? — Gideros Forum

Do I have to remove children in this case?

eezingeezing Member
edited October 2013 in General questions
So do I always need to remove children from a sprite?

If sprites are indexed inside a parent, can we just set self = nil, and not bother removing them? Or is this information stored elsewhere and removing should be done?

Use
Parent = Core.class( Sprite )
 
function Parent:init( myLayer )
 
	self.child1 = Sprite.new()
	self.child2 = Sprite.new()
 
	self:addChild(child1)
	self:addChild(child2)
 
	myLayer:addChild(self)
end
 
 
function Parent:destroy()	
 
	self.child1:removeFromParent()
	self.child2:removeFromParent()
 
	--[[ Are the two lines above required in this case ??? ]]--
 
	self:removeFromParent()
	self = nil
end

Comments

  • hgvyas123hgvyas123 Guru
    Accepted Answer
    self:removeFromParent()
    	self = nil
    above code will remove self as well as all the child of it

    :)
  • To answer your question, then yes, adding some child to a stage hierarchy adds one more reference to it, so self = nil is not enough and you should explicitly remove it from parent.
    The way @hgvyas123 demonstrates is the easiest way to accomplish what you want. :)
    Even more, you don't have to nil out self, but rather the variable where you stored the instance if you had one, if not then only remove from parent :)
  • Great! Thanks for the clarification.
Sign In or Register to comment.