我正在用Flash CS5 / AS 3.0编写一个游戏,它通过基于升序的y位置绘制所有相关的电影剪辑来模拟字段深度,即舞台上较低的东西在舞台上重叠的东西重叠。因此,与具有20的y位置的MovieClip相比,具有y位置的MovieClip将需要具有较低的指数,因此第二个将在第一个的顶部绘制。

我写了一个快速而肮脏的函数只是为了测试它。在痕迹期间,我注意到卡车的指数在临时靠近舞台顶部时击中0,但如果我走得太远,它会从舞台上完全消失。然后跟踪开始生成此错误:

ArgumentError:错误#2025:提供的displayObject必须是呼叫者的子项。
flash.display :: displayObjectContainer / GetChildIndex()

在EICT :: Game / ReorganizedisplayIndexes()
在EICT :: Game / Loop()

thetruck是播放器控制的车辆的MovieClip 敌人,锥体,岩石是包含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!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top