Flash:将影片剪辑从一个容器移动到另一个容器,同时保持相同的位置和旋转

发布于 2025-01-08 00:24:29 字数 166 浏览 0 评论 0原文

如标题所示,我有 2 个容器 MovieClip(mc1、mc2),每个容器都有单独的 x、y 旋转。我想从 mc1 中获取一个子项,然后将其 addChild() 到 mc2,同时在屏幕上保持相同的 x、y 和旋转,这样您就可以看到添加的子 mc,就好像它没有更改位置或旋转,只是移动到另一个 MC 。知道怎么做吗?

As the title shows, I have 2 container MovieClips (mc1, mc2), each of them has a separate x,y, rotation. I want to take a child from mc1 and addChild() it to mc2 while keeping the same x, y, and rotation on the screen so you see the added child mc as if it didnt change position or rotation, just moved to the other MC. Any idea how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绿光 2025-01-15 00:24:29

也许这段代码可以帮助你:
您可以指定要转换为另一个影片剪辑的坐标系的点。

public static function localToLocal(from:Sprite, to:Sprite, origin:Point):Object
{
     var point:Point = new Point(origin.x,origin.y);
     point = from.localToGlobal(point);
     point = to.globalToLocal(point);
     return point;
}

如果有多个单独旋转的子影片剪辑,问题仍然存在。否则,您可以仅使用已应用于影片剪辑的一次旋转,并将其应用于新的包装剪辑。

Maybe this piece of code might help you out:
You can specify a point which you want to translate into the coordinate system of another movie clip.

public static function localToLocal(from:Sprite, to:Sprite, origin:Point):Object
{
     var point:Point = new Point(origin.x,origin.y);
     point = from.localToGlobal(point);
     point = to.globalToLocal(point);
     return point;
}

The problem remains, if there are multiple sub-movieclips, that are rotated individually. Otherwise you can use just the one rotation you have applied to the movieclip, and apply it to a new wrapper-clip.

听不够的曲调 2025-01-15 00:24:29

您可以使用 localToGlobal()globalToLocal() 来实现这一点。

var positionInMc1:Point = new Point(child.x, child.y);
var positionInStage:Point = mc1.localToGlobal(positionInMc1);
var positionInMc2:Point = mc2.globalToLocal(positionInStage);

mc2.addChild(child);
mc2.x = positionInMc2.x;
mc2.y = positionInMc2.y;

编辑:

但是,这将无法正确处理旋转。因此,您可能需要在此之后旋转 child 以纠正 mc1mc2 的旋转。由于它们处于同一级别,您可能应该根据它们各自旋转之间的差异来旋转child

编辑:

从您在下面发布的代码:

child.rotation -= mc2.rotation;

You can use localToGlobal() and globalToLocal() to achieve that.

var positionInMc1:Point = new Point(child.x, child.y);
var positionInStage:Point = mc1.localToGlobal(positionInMc1);
var positionInMc2:Point = mc2.globalToLocal(positionInStage);

mc2.addChild(child);
mc2.x = positionInMc2.x;
mc2.y = positionInMc2.y;

Edit:

However, this will not handle the rotation correctly. So you will probably have to rotate child after that to correct for the rotation of mc1 and mc2. As they are in the same level, you should probably rotate child by the difference between their respective rotations.

Edit:

From the code you posted below:

child.rotation -= mc2.rotation;
生来就爱笑 2025-01-15 00:24:29

如果你有深层嵌套的转换,我会使用这样的东西。例如,您有嵌套精灵 mc1.mc2.mc3,并且想要将 mc3 重新附加到 mc4。这个看起来一个坐标系中的右向量 (1, 0) 与另一坐标系中的相同向量有多少不同。

function reattach(target:DisplayObject, to:DisplayObjectContainer):void {
    var center:Point = target.localToGlobal(new Point(0,0));
    // vector (1, 0) from old local coordinate system
    var oldVector:Point = target.localToGlobal(new Point(1,0)).subtract(center);
    var newCenter:Point = to.globalToLocal(center);
    to.addChild(target);
    target.x = newCenter.x;
    target.y = newCenter.y;
    // vector (1, 0) from new local coordinate system
    var newVector:Point = target.localToGlobal(new Point(1,0)).subtract(center);
    // angle between these two vectors
    var angle:Number = Math.acos((oldVector.x*newVector.x + oldVector.y*newVector.y)/(oldVector.length*newVector.length)) * 180 / Math.PI;
    if (oldVector.x*newVector.y - oldVector.y*newVector.x > 0) angle = -angle;
    // rotating for delta angle
    target.rotation += angle;
}

reattach(mc1.mc2.mc3, mc4);

如果任何精灵被缩放,则必须进行修改

If you got deeply nested transforms I'd use something like this. For example you have nested sprites mc1.mc2.mc3 and you want to reattach mc3 to mc4. This one looks how much the right vector (1, 0) in one coordinate system differs from same vector in another one.

function reattach(target:DisplayObject, to:DisplayObjectContainer):void {
    var center:Point = target.localToGlobal(new Point(0,0));
    // vector (1, 0) from old local coordinate system
    var oldVector:Point = target.localToGlobal(new Point(1,0)).subtract(center);
    var newCenter:Point = to.globalToLocal(center);
    to.addChild(target);
    target.x = newCenter.x;
    target.y = newCenter.y;
    // vector (1, 0) from new local coordinate system
    var newVector:Point = target.localToGlobal(new Point(1,0)).subtract(center);
    // angle between these two vectors
    var angle:Number = Math.acos((oldVector.x*newVector.x + oldVector.y*newVector.y)/(oldVector.length*newVector.length)) * 180 / Math.PI;
    if (oldVector.x*newVector.y - oldVector.y*newVector.x > 0) angle = -angle;
    // rotating for delta angle
    target.rotation += angle;
}

reattach(mc1.mc2.mc3, mc4);

Have to be modified if any of the sprites are scaled

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文