如何使用defineCollisionRectangle?

发布于 2024-12-29 09:38:02 字数 604 浏览 0 评论 0原文

对于初学者,我如何使用 defineCollisionRectangle API

我对此进行了一些研究,但我认为它根本没有任何用处。

确实,我可以使用 CollidesWith() 来检查 2 个精灵是否发生碰撞。

但我想使用一个具有参数的精灵,例如周围有后院围栏的房子。

我尝试在条件中使用 defineCollisionRectangle() ,在构造函数中设置它,但我不知道如何使用它。

我可以使用 object.getX/getyobject.getWidth/getHeight 在精灵周围创建一个参数。

defineCollisionRectangle 的真正作用是什么以及如何使用它?

For starters, how can I use defineCollisionRectangle API?

I've done some research about it and I think it doesn't have any use at all.

True that I can just use collidesWith() to check if 2 sprites collide.

But what I want to use a sprite that has parameters like house with a backyard fence around it.

I tried using the defineCollisionRectangle() in a condition, set it in the constructor but I don't know how to use it.

I can just use object.getX/gety and object.getWidth/getHeight, to make a Parameter around the sprite.

What does defineCollisionRectangle really do and how do I use it?

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

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

发布评论

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

评论(1

糖果控 2025-01-05 09:38:02

为了检测像house这样的物体与周围有fence的碰撞,我首先定义两个Sprite对象 - 一个用于围栏,另一个用于房屋 -每个都有其自己的碰撞矩形。

要渲染周围有栅栏的房子,我会在 fenceSprite 上绘制 houseSprite,如下图所示:

https://i.sstatic.net/4Wn5B.jpg

通过这种方法,可以很容易地判断碰撞是与栅栏还是房屋发生的 - 仅仅是因为每个碰撞都定义了自己的碰撞矩形。


一般来说,当您发现单个碰撞矩形不能满足您的需要时,您会发明一种将事物分解为更多矩形子元素的方法,以便在组合时,这些元素模拟/近似所需的行为。

  • 最终用户只是不关心场景后面有多少 Sprite 对象。只要最终结果感觉像周围有后院栅栏的房子,他们就会很高兴。

To detect collision for objects like house with a fence around it I would start with defining two Sprite objects - one for fence, another for house - each with its own collision rectangle.

To render a house with fence around it, I'd draw houseSprite over fenceSprite like at the sketch below:

https://i.sstatic.net/4Wn5B.jpg

With this approach it would be really easy to tell whether collision happened with fence or with house - simply because each defines its own collision rectangle.


Generally, when you find out that single collision rectangle doesn't do what you need, you invent a way to decompose things to more rectangular sub-elements so that when combined, these elements simulate / approximate desired behavior.

  • End users just don't care how many Sprite objects are there behind the scene. They're happy as long as end result feels about like house with a backyard fence around it.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文