AS3 Trace path back to root
by admin on Jul.15, 2008, under as3, code, snippet
This function will trace the path DisplayObject for a DisplayObject back to root in Actionscript 3. Useful for seeing witch containers it’s belonging to.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | private function tracePath (dispObj : DisplayObject, s : String = "") : void { if(dispObj.parent != null) { tracePath(dispObj.parent, s + "." + dispObj.name); } else { var a : Array = s.split(".").reverse(); var output : String = ""; for (var i : Number = 0;i < a.length; i++) { output = output + "." + a[i]; } trace("PATH: " + output.substr(1, output.length - 2)); } } |
Usage example:
1 2 | objName.name = "myDisplayObjectTest" tracePath(objName); |
Output example:
1 | PATH: root1.movPreloaderHolder.loaderHolder.instance3.myDisplayObjectTest |
1 comment for this entry:
August 7th, 2008 on 5:19 am
Or even
/**
* @author richardhulbert
*/
public class DisplayUtilities {
public static function createPathNameString(DObj : DisplayObjectContainer,Arr : Array = null) : String {
if(Arr == null) {
Arr = new Array();
}
if(DObj.parent == DObj.root) {
// we have arraived at the stage return the path;
Arr.reverse();
return Arr.join(”.”);
}else {
//save for later
Arr.push(DObj.parent.name);
//lets see if the parent is on the stage
return createPathNameString(DObj.parent, Arr);
}
}
}