Copy link to clipboard
Copied
This is my function that creates a display object with multiple children, one of which has an event listener.
function makeIt():void {
var spriteA:Sprite = new Sprite();
var spriteB:Sprite = new Sprite();
spriteB.addEventListener(MouseEvent.CLICK, myCallback);
var spriteC:Sprite = new Sprite();
spriteA.addChild(spriteB);
spriteB.addChild(spriteC);
stage.addChild(spriteA);
}
Let's say I'm done with it. What's the proper way to dispose of it so that it no longer uses up memory? Do I have to do all this?
spriteB.removeChild(spriteC);
spriteB.removeEventListener(MouseEvent.CLICK, myCallback);
spriteA.removeChild(spriteB);
stage.removeChild(spriteA);
I've read some people say that you should also assign things to null, so maybe like this?:
spriteB.removeChild(spriteC);
spriteC = null;
spriteB.removeEventListener(MouseEvent.CLICK, myCallback);
spriteA.removeChild(spriteB);
spriteB = null;
stage.removeChild(spriteA);
spriteA = null;
And I've got a few more questions:
1. What happens if I just removeChild(spriteA) when I'm done with it and call makeIt() again later when I need it? Are the same variables being reassigned, so there's no need to clean up after them?
2. If this display object is going to be frequently used and removed, would it be better to just toggle spriteA.visible?
Also, I'm familiar with weak event listeners, but I'd prefer not to use them.
removeChild is not disposing of anything, it is merely removing it from the display list... it could be deemed very similar to setting an object's visible property to be false as far as visuals go... the object is still around and can be targeted with code in both cases. If you want things to go away and be ready for garbage collection then you need to remove all references to them and null the objects. If you know that an object will be reused frequently then you probably want to go the visib
...Copy link to clipboard
Copied
removeChild is not disposing of anything, it is merely removing it from the display list... it could be deemed very similar to setting an object's visible property to be false as far as visuals go... the object is still around and can be targeted with code in both cases. If you want things to go away and be ready for garbage collection then you need to remove all references to them and null the objects. If you know that an object will be reused frequently then you probably want to go the visible true/false route, though using removeChild/addChild can work as well.
Copy link to clipboard
Copied
So in my example, would this be the bare minimum to remove all the references?
spriteB.removeEventListener(MouseEvent.CLICK, myCallback);
stage.removeChild(spriteA);
spriteA = spriteB = spriteC = null;
And would delete() be better so you don't have variables that do nothing but hold null?
spriteB.removeEventListener(MouseEvent.CLICK, myCallback);
stage.removeChild(spriteA);
delete(spriteA);
delete(spriteB);
delete(spriteC);
Copy link to clipboard
Copied
Do a quick search of Google using "AS3 delete vs null" and read thru the first few results... it'll save me from repeating it to you.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now