これを説明するのは少し難しいです。
オブジェクトをy位置に基づいて配置することにより、被写界深度をシミュレートするゲームに取り組んでいます。
静的な背景ステージと、スポーン、デスポーン、動き回るキャラクターがあります。
これらの文字は、互いに前にあるときに適切に並べる必要があります。
また、条件付きチェックを使用して、キャラクターが特定の静的オブジェクトの後ろまたは前にある必要があるかどうかを判断できるようにする必要もあります。
私の問題を説明するゲームとは別のファイルを作成することで、問題を骨の折れる部分まで分解しようとしました。
この例では、ステージ上に bgBox と fgBox の 2 つのムービークリップがあり、それぞれ黒と黄色です。
私のコードでは、それぞれ異なる y 位置にさらに 4 つのボックスを作成しています。そのうちの 3 つは、bgBox と fgBox の間にきちんと重ねて配置することを目的としていますが、4 つ目の紫は他のすべての上に配置することを目的としています。
これをエクスポートするとどうなりますか: http://www.fileize.com/files/32d824d3/992/sorting_test.swf
これが私のコードです:
var boxList:Array = new Array();
var unsortedBoxList:Array = new Array();
var initialSort:Boolean = true;
var blue_box:Shape = new Shape();
var red_box:Shape = new Shape();
var green_box:Shape = new Shape();
var purple_box:Shape = new Shape();
blue_box.graphics.lineStyle(1);
blue_box.graphics.beginFill(0x0000FF, 1);
blue_box.graphics.drawRect(200,150,100,100);
red_box.graphics.lineStyle(1);
red_box.graphics.beginFill(0xFF0000, 1);
red_box.graphics.drawRect(220,170,100,100);
green_box.graphics.lineStyle(1);
green_box.graphics.beginFill(0x00FF00, 1);
green_box.graphics.drawRect(240,190,100,100);
purple_box.graphics.lineStyle(1);
purple_box.graphics.beginFill(0xFF00FF, 1);
purple_box.graphics.drawRect(220,230,100,100);
trace("adding boxes----");
addChild(green_box);
trace("green: "+green_box.name);
boxList.push(green_box);
addChild(blue_box);
trace("blue: "+blue_box.name);
boxList.push(blue_box);
addChild(purple_box);
trace("purple: "+purple_box.name);
boxList.push(purple_box);
addChild(red_box);
trace("red: "+red_box.name);
boxList.push(red_box);
trace("-----------------");
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
function loop(e:Event):void
{
sortObjects();
}
function sortObjects():void {
if(initialSort == true){
setChildIndex(bgBox,0);
setChildIndex(fgBox,1);
trace("initial sort----");
trace(" - bgBox index: "+getChildIndex(bgBox));
trace(" - fgBox index: "+getChildIndex(fgBox));
trace("----------------");
initialSort = false;
}
boxList.sortOn(y, Array.NUMERIC);
trace("----------");
trace("boxList sorted: ");
for each(var j:Shape in boxList){
trace(j.name);
}
trace("-----");
trace(" - bgBox index: "+getChildIndex(bgBox));
for each(var i:Shape in boxList){
if(i.y > 220){
trace("y > 220; purple box"); //never triggers? the purple box should be on top of everything.
setChildIndex(i,(getChildIndex(fgBox)+1));
}
else
{
setChildIndex(i,getChildIndex(fgBox));
//all other boxes should be arranged neatly inbetween the black and yellow boxes.
}
trace(i.name+" index: "+getChildIndex(i));
}
trace(" - fgBox index: "+getChildIndex(fgBox));
}
これは非常に奇妙な出力です。
adding boxes----
green: instance5
blue: instance3
purple: instance6
red: instance4
-----------------
initial sort----
- bgBox index: 0
- fgBox index: 1
----------------
----------
boxList sorted:
instance6
instance3
instance5
instance4
-----
- bgBox index: 0
instance6 index: 1
instance3 index: 2
instance5 index: 3
instance4 index: 4
- fgBox index: 5
----------
boxList sorted:
instance5
instance3
instance6
instance4
-----
- bgBox index: 0
instance5 index: 5
instance3 index: 4
instance6 index: 3
instance4 index: 2
- fgBox index: 1
----------
boxList sorted:
instance6
instance3
instance5
instance4
-----
- bgBox index: 0
instance6 index: 1
instance3 index: 2
instance5 index: 3
instance4 index: 4
- fgBox index: 5
ご覧のとおり (ユーザー Marty Wallace のアイデアのおかげで)、オブジェクトは Y 値で正しくソートされていません。
これが役立つ場合に備えて、ソース ファイルへのリンクを次に示します 。
なぜこれが起こっているのか誰にも説明できますか?誰でもこの問題の解決策を提供できますか?
前もって感謝します。これは私を数日間困惑させました。