Search code examples
actionscript-3flash-cs6

stage.swapChildren(MClip1, Mclip2); not working


i have made a drag and match game where i have a rectangle circle movieclip ..when rectangle/or circle hit a specific box rectangle will always be behind circle and circle will always be over retangle,if i drag rectangle first then circle its ok..but if i drag circle first then rectangle ..circle always takes its position behind ractengle i used stage.swapChildren(ccircle, crect) but its not working ...here is my code

 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});

    }
}

Solution

  • You can't change ccircle index to -1. Only 0, 1, 2, ...

    stage.setChildIndex(ccircle, -1);

    You should write:

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

    It means: If ccircle is under crect then put ccircle on top.