2007-09-12

Flash/AS3 z-index 處理 part 2

承上一篇
3)欲以物件的某個參考值當作 Depth
將 Display Object 和 Depth (參考值) 做成 Object
所有 Object 做成 Object Array, 就可以用 sortOn 幫你排
FLA 原始檔
var numObj:int = 12;
var dang:Number = Math.PI*2/numObj;
var xr:int = 200;
var yr:int = 20;
var shift:Number = 0;
var arObj:Array = [];
for (var i:int = 0; i < numObj; i++) {
arObj[i] = new mc_star;
this.addChild( arObj[i] );
}
this.addEventListener(Event.ENTER_FRAME, mEnterFrame);
function mEnterFrame(e:Event):void {
var _mc:MovieClip;
var ang:Number;
var depth:int;
var depthsRela:Array = [];
for (var i:int = 0; i < numObj; i++) {
ang = i*dang +shift;
_mc = arObj[i] as MovieClip;
_mc.x = xr*Math.cos(ang);
_mc.y = yr*Math.sin(ang);
_mc.scaleX = 0.8 + 0.2*Math.sin(ang);
_mc.scaleY = _mc.scaleX;
var blurN:Number = 4*(1- Math.sin(ang) );
_mc.filters = [new BlurFilter(blurN, blurN, BitmapFilterQuality.HIGH)];
// z-index 的參考值
depth = Math.sin(ang)*1000;
depthsRela.push({d:depth, mc:_mc});

}
// 排序
depthsRela.sortOn("d", Array.NUMERIC);
for (i=0; i < numObj; i++) {
this.setChildIndex(depthsRela[i].mc, i);
}
shift += 0.01;
}

沒有留言:

FB 留言