I have huge legacy project. And sometimes one of my windows is removed from stage. I've created test project to simulate situation with chain of children root.mc1.mc2.mc3.mc4
mc4 is MovieClipWithEvents custom class
After that I put line removeChild(mc1);
In mc4 I also receive REMOVED_FROM_STAGE, but I can't figure which one of parent is removed.
globalVisibleList method in trace shows:
// mc4 added into mc3
[trace] onAddedToStage mc4 mc3 mc4 mc4
// mc1 is removed from root, BUT event.target shows that mc4 is removed from mc3
[trace] onRemovedFromStage mc4 mc3 mc4 mc4
//All parents still have their parents and are visible despite we know that mc1 is removed from root and received REMOVED_FROM_STAGE
[trace] [object MovieClipWithEvents2], mc4, true
[trace] [object MovieClip], mc3, true
[trace] [object MovieClip], mc2, true
[trace] [object MovieClip], mc1, true
[trace] [object Test_VisibleEvent2], root1, true
[trace] [object Stage], null, true
//The next line trace("*************\n" + mc4.globalVisibleList(mc4));
all is ok, mc1 is not child of root now
[trace] *************
[trace] [object MovieClipWithEvents2], mc4, true
[trace] [object MovieClip], mc3, true
[trace] [object MovieClip], mc2, true
[trace] [object MovieClip], mc1, true
Test_VisibleEvent2
package {
import display.MovieClipWithEvents2;
import flash.display.MovieClip;
import flash.display.Sprite;
public class Test_VisibleEvent2 extends Sprite {
private var mc1:MovieClip;
private var mc2:MovieClip;
private var mc3:MovieClip;
private var mc4:MovieClipWithEvents2;
public function Test_VisibleEvent2() {
mc1 = new MovieClip();
mc2 = new MovieClip();
mc3 = new MovieClip();
mc4 = new MovieClipWithEvents2();
mc1.name = "mc1";
mc2.name = "mc2";
mc3.name = "mc3";
mc4.name = "mc4";
addChild(mc1);
mc1.addChild(mc2);
mc2.addChild(mc3);
mc3.addChild(mc4);
removeChild(mc1);
trace("*************\n" + mc4.globalVisibleList(mc4));
}
}
}
MovieClipWithEvents2
package display {
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.events.Event;
public class MovieClipWithEvents2 extends MovieClip {
public function MovieClipWithEvents2() {
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
}
protected function onAddedToStage(event:Event):void {
trace("onAddedToStage", name, parent.name, event.target.name, event.currentTarget.name);
}
protected function onRemovedFromStage(event:Event):void {
trace("onRemovedFromStage", name, parent.name, event.target.name, event.currentTarget.name);
trace(globalVisibleList(this));
}
public function globalVisibleList(displayObject:DisplayObject):String {
var result:String = "";
var checking:DisplayObject = displayObject;
while(checking){
result += getNameAndVisible(checking);
if(checking.parent){
checking = checking.parent;
}else{
return result;
}
}
return result;// to make Idea happy
}
public function getNameAndVisible(displayObject:DisplayObject):String {
return displayObject + ", " + displayObject.name + ", " + displayObject.visible + "\n";
}
}
}
So I have two questions:
Why mc4 receive REMOVED_FROM_STAGE, but all chain of parents is unbroken till Stage?
How to figure which parent is removed?