2011年6月7日 星期二

利用遞迴函式巡訪容器中所有子物件,並過濾掉特定類型物件

1000606_recursion_1.swf


/*利用遞迴函式巡訪容器中所有子物件,並過濾掉特定類型物件*/

this.setInfoTF();
var indentSpace:String = "    ";
this.callOn(this, indentSpace);

function callOn(_doc:DisplayObjectContainer,_indentSpace:String):void {
if (_doc is DisplayObjectContainer) {
for (var i:uint; i<_doc.numChildren; i++) {
/*過濾掉Shape和TextField*/
if(_doc.getChildAt(i) is Shape != true && _doc.getChildAt(i) is TextField != true){
trace(_indentSpace + _doc.getChildAt(i).name + " : " + _doc.getChildAt(i));
this.addInfo(_indentSpace + _doc.getChildAt(i).name + " : " + _doc.getChildAt(i));
}
/*子物件中若還有容器,則調用本身函式callOn() - 遞迴*/
if (_doc.getChildAt(i) is DisplayObjectContainer) {
var tmpdoc:DisplayObjectContainer = _doc.getChildAt(i) as DisplayObjectContainer;
callOn(tmpdoc,_indentSpace + "    ");
}
}
}
return;
}

function setInfoTF():void {
var format:TextFormat = new TextFormat();
format.color = 0x222299;
this.info.selectable = false;
this.info.wordWrap = true;
this.info.autoSize = TextFieldAutoSize.LEFT;
this.info.setTextFormat(format);
this.info.defaultTextFormat = format;
return;
}

function addInfo(_obj:*):void {
this.info.appendText(_obj.toString() + "\n");
return;
}

沒有留言: