根据比例重新定位对象

发布于 2024-08-18 03:53:46 字数 272 浏览 2 评论 0原文

在我的游戏中,我有飞机从自上而下的角度飞过屏幕。当飞机坠毁时,我会按比例缩小它们,使其看起来好像它们坠落时离地面更近,离屏幕更远。我在这些工艺品上也有炮塔。它们是与空中计划不同的对象。我也缩小了它们的比例。唯一的问题是它们没有正确地重新定位。即使它们被缩放,它们仍保持在 x 和 y 位置,看起来就像被从飞机上拉开一样。是否可以根据它们所在的物体(即飞机)的大小来正确地重新定位它们

我刚刚从另一篇文章中得到了一个很好的答案,将它们放置到影片剪辑本身中。这很好,但郑重声明,如果有人知道这个问题的答案,那就太好了。

In my game, I have airplanes that are flying past the screen from a top down view. when the air planes crash, I scale them down to make it appear as if they are falling closer to the ground and farther away from the screen. I have turrets on these crafts as well. they are seperate objects from the air plan. I scale them down as well. The only problem is they dont reposition correctly. they stay in their x and y positions even though they are being scaled it looks as if they are being pulled away from the air plane. is it possible to reposition them correctly based on the size on the object they sit on (i.e. the air planes)

I just got a good answer from another post to place them into the movieclip itself. which is great but for the record, if anyone knows the answer to this, that would be great.

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

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

发布评论

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

评论(1

度的依靠╰つ 2024-08-25 03:53:46

您应该将每架飞机的所有内容都保留在其自己的 MovieClip 中。但了解如何在没有内置scaleX/scaleY 的情况下执行此操作仍然很有用。使用您当前的设置,可以像这样完成(其中plane是您的飞机,scale是您要将飞机缩放到的因子(0-1之间),turret是一个应该相对于plane):

function scalePlane(plane, turret, scale:Number):void {

    //scaling coordinates:
    var relativeX:Number = turret.x - plane.x;
    var relativeY:Number = turret.y - plane.y;
    var newRelativeX:Number = relativeX*scale;
    var newRelativeY:Number = relativeY*scale;
    turret.x = plane.x + newRelativeX;
    turret.y = plane.y + newRelativeY;
    //scaling size:
    turret.scaleX *= scale;
    turret.scaleY *= scale;
    plane.scaleX *= scale;
    plane.scaleY *= scale;
}

scalePlane(plane1, turret1, 0.9);
//overall scale is 0.9;
//next frame:
scalePlane(plane1, turret1, 0.9);
//now the overall scale is 0.81 because you are multiplying the scales

我还没有测试过,但只要飞机和炮塔有相同的父母,它就应该可以工作。请注意,这种情况下的比例是相对的,因此您可能需要稍微更改计算。 (如果您使用比例 0.5 调用此代码两次,则平面的比例将为 0.5*0.5 = 0.25)

You should be keeping everything for each plane inside it's own MovieClip. But it's still useful to know how to do this without the builtin scaleX/scaleY. With your current setup, it can be done like so(where plane is your plane, scale is the factor (between 0-1) that you are going to scale the plane to, and turret is a turret that should be scale relative to the plane):

function scalePlane(plane, turret, scale:Number):void {

    //scaling coordinates:
    var relativeX:Number = turret.x - plane.x;
    var relativeY:Number = turret.y - plane.y;
    var newRelativeX:Number = relativeX*scale;
    var newRelativeY:Number = relativeY*scale;
    turret.x = plane.x + newRelativeX;
    turret.y = plane.y + newRelativeY;
    //scaling size:
    turret.scaleX *= scale;
    turret.scaleY *= scale;
    plane.scaleX *= scale;
    plane.scaleY *= scale;
}

scalePlane(plane1, turret1, 0.9);
//overall scale is 0.9;
//next frame:
scalePlane(plane1, turret1, 0.9);
//now the overall scale is 0.81 because you are multiplying the scales

I haven't tested that, but it should work as long as the plane and turret have the same parents. Note that scale in this case is relative, so you might have to change your calculations a bit. (if you were to call this code with scale 0.5 twice, the plane would then have a scale of 0.5*0.5 = 0.25)

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