如何删除子(影片剪辑),并添加到新的父(影片剪辑) [英] How to remove child(Movieclip) and add to new parent (Movieclip)

查看:165
本文介绍了如何删除子(影片剪辑),并添加到新的父(影片剪辑)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要删除从父(影片剪辑)的同时拖动子(影片剪辑)和相同的子(影片剪辑)添加到另一个影片剪辑时删除。

I need to remove the child(movieClip) from the parent(movieClip) while dragging and add the same child(movieClip) to another movieclip when dropped.

此方法用于拖动

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

当我把它

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 visible or not available while dropping...

帮我从这个问题的解决。

Help me to overcome from this Problem.

推荐答案

我有一个快速的打法,这里是我想出了:

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

我的例子,对所谓的球舞台上的影片剪辑,和其他一些影片剪辑的周围散落用作dropTargets。

My example has a MovieClip on the stage called "ball", and a number of other MovieClip 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;
}

基本上,代答的处理程序会删除其母公司的球影片剪辑,并将其添加到根的DisplayObject。这可以确保球在上面,这样的DropTarget将工作(DropTarget的将只与在堆叠顺序较低的对象的工作)。球的X / Y位置被计算(使用localToGlobal)为其新的父,和应用。然后开始拖动时调用。

Basically, the PickUp handler removes the "ball" MovieClip from its parent, and adds it to the root DisplayObject. This makes sure the ball is on top, so the dropTarget will work (dropTarget will only work with Objects lower in the stack order). The ball's x/y position is calculated (using localToGlobal) for its new parent, and applied. Then start drag is called.

在dropIt处理程序首先调用调用stopDrag,然后返回,如果它不能找到的DropTarget。使用的球一个新的X / Y轴计算的 globalToLocal 这个时候,取得了DropTarget的新位置。球然后除去并添加到它的新的父(所述的DropTarget)。新的位置的应用。

The dropIt handler first calls stopDrag, then returns if it can't find a dropTarget. A new x/y position for the ball is calculated using globalToLocal this time, to get its new position in its droptarget. The ball is then removed and added to its new parent (the dropTarget). The new position is applied.

看起来工作非常好我的情况。

Seems to work really well in my case.

此外...我发现,进行测试,如果它的工作的好办法,就是暂时采用不同的模糊或阴影的过滤器,以在舞台上的dropTargets。当你把球到他们的方式,它将继承过滤器,你可以看到一些视觉反馈。

Also... I found that a good way to test if it's working, is to temporarily apply different blur or dropShadow filters to the dropTargets on the stage. That way when you drop the ball onto them, it will inherit the filter, and you can see some visual feedback.

这篇关于如何删除子(影片剪辑),并添加到新的父(影片剪辑)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆