1.a sprite,has no parent, is any function(method) can keep it not destroyed?
2.a sprite,it parent is stage ,when "stage:removeChild(sprite)" , is any function(method) can destroy it frome memory?
3.a sprites has no parent,how long destroy from memory? and what is the process?
Comments
Now I will try to answer your questions
1- when your sprite has no parent (not added to stage) then it is not displayed on the screen but is kept in memory
2- when you do stage:removeChild(sprite) lua and its garbage collector will at some point automatically remove it from memory, to be sure you can set sprite = nil
3- same as above
Note if you want to hide (not destroy) your sprite you can call sprite:setVisible(false), ...
Destroy from memory is called garbageCollector. From the wiki:
https://wiki.gideros.rocks/index.php/Introduction_to_Lua#Assignments_and_variables
There are quite a few posts on the forum like these ones:
https://forum.gideros.rocks/discussion/comment/61504/#Comment_61504
https://forum.gideros.rocks/discussion/8019/doubts-about-texture-bitmaps-and-memory-consumption
https://forum.gideros.rocks/discussion/7845/best-way-to-remove-a-sprite-that-contains-other-sprites-and-bitmaps
https://forum.gideros.rocks/discussion/comment/56787/#Comment_56787
...
giderians, please correct me if I am wrong
emm, Where is the problem?
local container = Sprite.new()
local ball1 = Bitmap.new(Texture.new("ball.png", true))
ball1:setAnchorPoint(0.5, 0.5)
ball1:setX(-50)
container:addChild(ball1)
local ball2 = Bitmap.new(Texture.new("ball.png", true))
ball2:setAnchorPoint(0.5,0.5)
ball2:setX(50)
container:setPosition(150, 150)
stage:addChild(container) -- add
stage:removeChild(container) -- remove
collectgarbage('collect')
local timer = Timer.new(20000, 1)
timer:addEventListener(Event.TIMER,
function()
print("111")
stage:addChild(container) --- why still can addchild?
end)
timer:start()
so I guess you want to destroy container from memory?
you need to set it to nil before collectgarbage.
so I guess you want to destroy container from memory? --- yes you are right
1.if not set it to "nil",it will never be destroyed? even though it is local variable?
2.if "container" has not parent and not set "nil", even though auto-collect garbage or manual 'collectgarbage()' ,it still exist?
2- yes it still exists because "gideros" thinks that you will use this object later in your game/app so it doesn't destroy it for you.
Likes: MoKaLux
So, to be freeable, an object must not be reachable through any reference, either those you set yourself in lua (a global, a local, an upvalue, a parameter of a still running function, etc), or those Gideros keeps under the hood (Sprites attached to the stage, a running MovieClip, etc).
Likes: MoKaLux, SinisterSoft
Likes: MoKaLux