as mentioned in many others threads we have to remove all objects we created manually.
if the DisplayObject has eventlisteners you have to remove them manually, too.
for example:
var circle:Sprite = new Sprite();
circle.addEventListener(Event.ENTER_FRAME, doSomething);
circle.addEventListener(Event.REMOVED_FROM_STAGE, doCleanup);
addChild(circle);
stage.addEventListener(MouseEvent.CLICK, doAction);
function doSomething(event:Event):void {
trace('enterFrame event running');
}
function doAction(event:MouseEvent):void {
trace('removing DisplayObject');
removeChild(circle);
circle = null;
}
function doCleanup(event:Event):void {
trace('removing eventlisteners');
circle.removeEventListener(Event.ENTER_FRAME, doSomething);
}
so i am still a bit confused about removing objects from the stage.
what happens to a child object, if its parent has been removed from the stage?
var circle1:Sprite = new Sprite();
var circle2:Sprite = new Sprite();
circle1.addEventListener(Event.ENTER_FRAME, doSomething);
circle1.addEventListener(Event.REMOVED_FROM_STAGE, doCleanup);
circle2.addEventListener(Event.REMOVED_FROM_STAGE, stillOnStage);
addChild(circle1);
circle1.addChild(circle2);
stage.addEventListener(MouseEvent.CLICK, doAction);
function doAction(event:MouseEvent):void {
trace('removing DisplayObject');
removeChild(circle1);
circle1 = null;
}
function doSomething(event:Event):void {
trace('enterFrame event running');
}
function stillOnStage(event:Event):void {
trace('seems i was removed?');
}
function doCleanup(event:Event):void {
trace('removing eventListener');
circle1.removeEventListener(Event.ENTER_FRAME, doSomething);
}
so does circle2 still remain on the stage? is it still in the memory? will it get garbage collected? the listener tells me, that it was removed.
regards
michael
Edit:
similar article
Edit:
guess the listeners for the removement should also be removed?
function doCleanup(event:Event):void {
trace('removing eventListener');
circle1.removeEventListener(Event.ENTER_FRAME, doSomething);
circle1.removeEventListener(Event.REMOVED_FROM_STAGE, doCleanup);
circle2.removeEventListener(Event.REMOVED_FROM_STAGE, doCleanup);
}