stage.swapChildren(MClip1, Mclip2);不工作

stage.swapChildren(MClip1, Mclip2); not working

我制作了一个拖动匹配游戏,其中我有一个矩形圆圈动画片段..当 rectangle/or 圆圈击中一个特定的框时,如果我拖动,矩形将始终在圆圈后面,并且圆圈将始终过度缠绕先画矩形然后画圆圈 ok..但是如果我先画圆圈然后画矩形 ..circle 总是在我使用 stage.swapChildren(ccircle, crect) 的 ractengle 后面占据它的位置但是它不工作...这是我的代码

 crect.addEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown1);
function item_onMouseDown1(event:MouseEvent):void 
    {
        crect = MovieClip(event.target);
        startX1 = crect.x;
        startY1 = crect.y;
        crect.startDrag();
        setChildIndex(crect, this.numChildren-1);
        stage.addEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp1);

    }
function stage_onMouseUp1(event:MouseEvent):void {

stage.removeEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp1);
crect.stopDrag();

    if(crect.hitTestObject(box3))
    {
        TweenMax.to(crect, 0.5, {x:hit2X, y:hit2Y,height:height2Y, width:weight2X, ease:Cubic.easeOut});
         //crect.mouseEnabled=false;


    }
    else
    {
        TweenMax.to(crect, 0.5, {x:startX1, y:startY1, ease:Bounce.easeOut});

    }
}
ccircle.buttonMode=true;
ccircle.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown1);

function onMouseDown1(event:MouseEvent):void 
    {
        ccircle = MovieClip(event.target);
        startX1 = ccircle.x;
        startY1 = ccircle.y;
        ccircle.startDrag();
        setChildIndex(ccircle, this.numChildren-1);
        stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp1);

    }
function onMouseUp1(event:MouseEvent):void {

stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp1);
ccircle.stopDrag();

    if(ccircle.hitTestObject(box3))
    {
         TweenMax.to(ccircle, 0.5, {x:hit1X, y:hit1Y,height:height1Y, width:weight1X, ease:Cubic.easeOut});
        //stage.swapChildren(ccircle, crect);

        //setChildIndex(ccircle, this.numChildren-1);
         //ccircle.mouseEnabled=false;


    }
    else
    {
        TweenMax.to(ccircle, 0.5, {x:startX1, y:startY1, ease:Bounce.easeOut});

    }
}

您不能将 ccircle 索引更改为 -1。只有 0, 1, 2, ...

stage.setChildIndex(ccircle, -1);

你应该写:

if (getChildIndex(ccircle) < getChildIndex(crect))
    swapChildren(ccircle, crect);

意思是:如果ccirclecrect下面,那么把ccircle放在上面。

你可以直接使用

removeChild(crect);
addChild(crect);

这将始终把 crect 放在最前面。当然,如果你需要ccircle在上面,也可以这样做。

我简单地设置 setChildIndex(crect, this.numChildren-2); 和 setChildIndex(crect, this.numChildren-2);并且它的工作完美,因为它不是用于在顶部设置圆圈 only.while 拖动我必须将拖动的对象放在其他对象的顶部..

我还找到了一些您可以阅读的信息
Gary Rosenzweig 网站,我一直在研究自己在 flash 上的显示列表。

该站点是 http://flashgameu.com/ 并向下滚动查找 "understanding the display list."

它解释了按名称和显示列表中的位置交换 children。

观看他的播客将帮助您了解使用显示列表交换 children、addChild、removeChild 和 childIndex 的选项。

它也是免费的,是一个很好的网站,提供了一些有关如何使用 Flash 和 as3 编程的信息。