y位置に基づいてMovieclipsの子インデックスを変える?
-
14-11-2019 - |
質問
私はフラッシュCS5 / AS 3.0を使ってゲームを書いています。 。したがって、10のY位置を有するMovieClipは、20のY位置を有するMovieClipと比較して低いインデックスを有する必要があるので、第2のものは最初の上に描かれている。
これをテストするためだけに迅速で汚れた機能を書きました。トレースの間に、私がステージの上部に近くなるとトラックの指数が0にぶつかることに気付きましたが、私が遠すぎるとそれはステージから完全に消えます。その後、トレースはこのエラーの生成を開始します。
ArgumentError:エラー#2025:指定されたDisplayObjectは発信者の子である必要があります。
Flash.Display :: DisplayObjectContainer / getChildIndex()
EICT :: Game / ReganizedIsplayIndexes()
EICT ::ゲーム/ループ()テトックは選手管理車のムービーリップです 敵、円錐、岩はすべてMovieclipsを含むアレイ
それらのどれもイベントリスナーを持っていません。
.private function ReorganizeDisplayIndexes(): void { var drawableObjects:Array = new Array(); drawableObjects.push(theTruck); drawableObjects = drawableObjects.concat(Enemies, Rocks, Bushes); drawableObjects.sortOn("y", Array.DESCENDING | Array.NUMERIC); drawableObjects.reverse(); trace(collisionLayer.getChildIndex(theTruck)); for (var a:int = collisionLayer.numChildren - 1; a >= 0; a--) { collisionLayer.removeChildAt(a); } for (var i:int = 0; i < drawableObjects.length; i++) { collisionLayer.addChild(drawableObjects[i]); } }
解決
Hint: You don't need to remove the child first. When you use addChild()
on an object, it is automatically re-added to the next highest depth.
That said, you'll just need to do something like this:
drawableObjects.sortOn("y");
for each(var i:DisplayObject in drawableObjects)
{
if(i.parent)
i.parent.addChild(i);
}
他のヒント
Use setChildIndex instead of removing and re-adding:
for (var a:int = collisionLayer.numChildren - 1; i >= 0; i--)
{
collisionLayer.setChildIndex(drawableObjects[i], i);
}
Also, it's a bit wasteful to first order the sort in descending and then reversing the array. Sort it ascending to begin with!