如何使用defineCollisionRectangle?
对于初学者,我如何使用 defineCollisionRectangle API?
我对此进行了一些研究,但我认为它根本没有任何用处。
确实,我可以使用 CollidesWith() 来检查 2 个精灵是否发生碰撞。
但我想使用一个具有参数的精灵,例如周围有后院围栏的房子。
我尝试在条件中使用 defineCollisionRectangle()
,在构造函数中设置它,但我不知道如何使用它。
我可以使用 object.getX/gety
和 object.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了检测像
house
这样的物体与周围有fence
的碰撞,我首先定义两个Sprite对象 - 一个用于围栏,另一个用于房屋 -每个都有其自己的碰撞矩形。要渲染周围有栅栏的房子,我会在 fenceSprite 上绘制 houseSprite,如下图所示:
通过这种方法,可以很容易地判断碰撞是与栅栏还是房屋发生的 - 仅仅是因为每个碰撞都定义了自己的碰撞矩形。
一般来说,当您发现单个碰撞矩形不能满足您的需要时,您会发明一种将事物分解为更多矩形子元素的方法,以便在组合时,这些元素模拟/近似所需的行为。
To detect collision for objects like
house
with afence
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:
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.