2007-06-18

Z-order Index (part 2)

移除 DisplayObject 時, 其原本所佔用的 index, 會被後面的物件所遞補
參考 設定連結的元件會變成類別 原始檔
Frame actions :
var ar:Array = new Array();
var s:Symbol1;
for (var i:int=0; i<6; i++) {
s = new Symbol1();
s.x = s.width*i;
s.y = 100;
s._txt.text = String(i+1);
s.name = 's'+(i+1);
s.addEventListener(MouseEvent.CLICK, cOnClick);
this.addChild(s);
ar.push(s);
}

function cOnClick(e:MouseEvent):void {
var t_mc:MovieClip = e.currentTarget as MovieClip;
t_mc.parent.removeChild(t_mc);
trace('子物件數目:', this.numChildren);
for (var i:int=0; i < ar.length; i++) {
if (this.contains(ar[i])) {
trace(ar[i].name, ':', this.getChildIndex(ar[i]) );
}
}
}

點按1號物件後的輸出結果:
子物件數目: 5
s2 : 0
s3 : 1
s4 : 2
s5 : 3
s6 : 4

若欲取得某 DisplayObject 的所有子物件, 可以搭配使用:
for(var k:int=0; k < this.numChildren; k++){
var d:DisplayObject = this.getChildAt(k);
trace(d.name);
}

沒有留言:

FB 留言