How to remove a child (Movieclip) and add a new parent (Movieclip)

I need to remove a child (movieClip) from a parent (movieClip) when dragging and adding the same child (movieClip) to another movie clip when deleting.

this method is used to drag and drop

function pickUp(event:MouseEvent):void 
{
    event.target.startDrag();
}

when i throw it away

function dropIt(event:MouseEvent):void
{
    event.target.parent.removeChild(event.target); //for removing from previous parent clip

    event.target.dropTarget.parent.addChild(event.target); // add to new Moviclip
}

But the clip is not displayed or is not available when dropped ...

Help me overcome this problem.

0
source share
1 answer

I had a quick game, here is what I came up with:

In my example, there is a MovieClip on the stage called "ball", as well as several other MovieClip that are scattered around, for use as dropTargets.

ball.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
stage.addEventListener(MouseEvent.MOUSE_UP, dropIt);

function pickUp(event:MouseEvent):void 
{
    var ballPoint:Point = ball.parent.localToGlobal( new Point(ball.x, ball.y) );

    ball.parent.removeChild(ball);
    addChild(ball);
    ball.x = ballPoint.x;
    ball.y = ballPoint.y;
    ball.startDrag();
}

function dropIt(event:MouseEvent):void 
{
    ball.stopDrag();
    if(!event.target.dropTarget) { return };

    var dropT:MovieClip = event.target.dropTarget.parent;
    var ballPoint:Point = dropT.globalToLocal( new Point(ball.x, ball.y) );

    ball.parent.removeChild(ball);
    dropT.addChild(ball);
    ball.x = ballPoint.x;
    ball.y = ballPoint.y;
}

, PickUp "" MovieClip DisplayObject. , , dropTarget (dropTarget ). x/y ( localToGlobal) . .

dropIt stopDrag, , dropTarget. x/y globalToLocal , -. (dropTarget). .

, .

... , , , dropShadow dropTargets . , , , .

+1

All Articles