Corona物理引擎碰撞事件对象

发布于 2024-10-05 18:30:56 字数 295 浏览 6 评论 0原文

我正在为一个简单的游戏尝试 Corona 物理引擎。我创建了几个“球”,只是圆形物体和“好吧”静态传感器物体。

physics.addBody(ball,{density=1-dens, friction=0.2, bounce=boun, radius=imp})
physics.addBody( well,"static", { radius=sensorRadius, isSensor = true} )

碰撞事件有“self”和“event”参数。有没有一种简单的方法来检查击中井的“球”的半径?

I'm trying out the Corona physics engine for a simple game. I have created several "balls", just circular object and "well" a static sensor object.

physics.addBody(ball,{density=1-dens, friction=0.2, bounce=boun, radius=imp})
physics.addBody( well,"static", { radius=sensorRadius, isSensor = true} )

The collision event has "self" and "event" parameters. Is there a simple way to check the radius of the "Ball" that hits the well?

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

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

发布评论

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

评论(1

人生百味 2024-10-12 18:30:56

您可能已经弄清楚了这一点,或者现在已经放弃了 Corona,但这里有一个答案,我将添加我的两分钱:

您可以向球对象添加任何您想要的属性。您不必显式声明属性——当您为它们分配值时,它们会自动创建。例如,要添加“半径”属性,只需在创建“球”对象后执行此操作:

ball.radius = 20

完成此操作后,假设您已将碰撞事件处理程序附加到球本身,则球将被传递作为“self”参数,您可以使用 self.radius 获取半径:

local radius = self.radius

如果您已将事件处理程序附加到其他某个对象,则球将作为“event”参数的“other”属性传递。因此,使用您的井示例,如果您将事件处理程序附加到井上,那么您将获得球的半径:

local radius = event.other.radius

当然,如果您有其他(非球)对象也可以击中井,并且那些对象没有“radius”属性,那么您必须首先确保“event.other”实际上是一个“ball”对象。如果不这样做,那么当您尝试获取半径时,您将得到“nil”。事实上,您可以使用此行为来检测另一个对象实际上是一个球:

local radius = event.other.radius
if radius then
    -- may be safe to assume event.other is a ball
else
    -- something else hit the well
end

希望这会有所帮助。我自己刚接触电晕一个月,所以...

You've probably either figured this out already, or given up on Corona by now, but just so there's an answer here I'll add my two cents:

You can add any property you want to your ball object. You don't have to explicitly declare properties--they are created automatically when you assign a value to them. For example, to add a "radius" property, just do this once you've created your "ball" object:

ball.radius = 20

Once you've done that, assuming you've attached the collision event handler to the ball itself, the ball is passed as the "self" parameter, and you can get the radius with self.radius:

local radius = self.radius

If you've attached the event handler to some other object, the ball will be passed as the "other" property of the "event" parameter. So using your example of a well, if you attached the event handler to the well, then you'd get the ball's radius with:

local radius = event.other.radius

Of course, if you have other (non-ball) objects that can hit the well also, and those objects don't have "radius" property, then you'll have to make sure that "event.other" is actually a "ball" object first. If you don't then you'll get "nil" when you attempt to get the radius. In fact, you could use this behavior to detect that the other object is actually a ball:

local radius = event.other.radius
if radius then
    -- may be safe to assume event.other is a ball
else
    -- something else hit the well
end

Hope this helps. I'm only a month into Corona myself, so...

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