如何在 IOS 中平滑地缩放图层以将对象保留在屏幕上?

发布于 2024-12-01 03:45:48 字数 114 浏览 0 评论 0原文

我正在开发一款游戏,球被击中后可以离开图层的可见区域。我在每一帧中设置球的位置,因为我从 Box2D 获得有关其位置的反馈。当球接近屏幕边缘时,我想缩小适量以保持球可见。有人可以帮助我了解执行此操作的逻辑吗?谢谢。

I'm working on a game where a ball is hit, and can leave the visible area of the layer. I'm setting the position of the ball each frame, as I get feedback from Box2D about its location. When the ball nears the edge of the screen, I want to zoom out just the right amount to keep the ball visible. Can someone give me a hand with the logic to do this? Thanks.

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

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

发布评论

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

评论(1

云巢 2024-12-08 03:45:48

假设屏幕大小为 480(像素),球的原始直径为 10 像素。

Original size of ball = bOriginal = 10
Distance represented by screen = s = 480
Distance ball has travelled = x
Diameter of the ball = b = bOriginal

当球到达距屏幕边缘一定距离时,您会看到一个标志。之后你就得到了你已经知道的速度;这也可以被认为是球向屏幕边缘移动的速率,因此屏幕必须相对于球的尺寸扩展的速率,以确保球行进的总距离包含在屏幕尺寸。

If x >= 475
  ratio of screen size to distance = r = 480 / (x+5)
  b = bOriginal *  r
end

这将演示“缩小”,即球将不断变小,以确保行进的总距离适合屏幕的大小。

Let's have the size of the screen size of 480 (pixels), with the original diameter of the ball being 10 pixels.

Original size of ball = bOriginal = 10
Distance represented by screen = s = 480
Distance ball has travelled = x
Diameter of the ball = b = bOriginal

You would have a flag for when the ball reaches a certain distance from the edge of the screen. After which you have your velocity, which you already know; this can also be thought of as the rate the ball is moving towards the edge of the screen, therefore the rate at which the screen must expand with respect to the size of the ball to ensure the total distance the ball has travelled is included within the size of the screen.

If x >= 475
  ratio of screen size to distance = r = 480 / (x+5)
  b = bOriginal *  r
end

This will demonstrate a "zoom-out" in that the ball will get continually smaller to ensure that the total distance travelled fits into the size of the screen.

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