grey
grey

Reputation: 1150

AS3: Determining when a DisplayObject has been removed from the stage?

The REMOVED_FROM_STAGE event is fired before the object is actually removed from the stage. I'm looking for a performant way to know when the object is finally removed. Suggestions?

Upvotes: 0

Views: 1177

Answers (3)

noponies
noponies

Reputation: 2726

How are you removing your object?

The last event before the screen is redrawn and you would see that your object has been removed is the render event.

Could you not listen out for that? As if an object is removed from the stage, the stage will have to redraw itself.

stage.invalidate();
stage.addEventListener(Event.RENDER, doAfter, false, 0, true);

Upvotes: 1

crooksy88
crooksy88

Reputation: 3851

You could check the objects stage value.

if (!object.stage) {
  //not added to display list
} 

Possibly set up an enterframe listener when REMOVED_FROM_STAGE is triggered.

object.addEventListener(Event.REMOVED_FROM_STAGE, removed, false, 0, true);

private function removed(e:Event):void {
    e.target.removeEventListener(Event.REMOVED_FROM_STAGE, removed);    
    e.target.addEventListener(Event.ENTER_FRAME, checkStage, false, 0, true);
}

private function checkStage(e:Event):void {
    if (!e.target.stage) {
        //object has been removed
        e.target.removeEventListener(Event.ENTER_FRAME, checkStage);
        //do something now it has been removed
    }
}

Upvotes: 2

mschloesser
mschloesser

Reputation: 71

You can always check if the target object's parent property is null. When and how you check this depends on your application structure etc.

if (targetObject !== null && targetObject.parent === null) {
    // target object has been removed from display list
} else {
    // target object is still in display list
}

Upvotes: 0

Related Questions