Corona物理引擎碰撞事件对象
我正在为一个简单的游戏尝试 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能已经弄清楚了这一点,或者现在已经放弃了 Corona,但这里有一个答案,我将添加我的两分钱:
您可以向球对象添加任何您想要的属性。您不必显式声明属性——当您为它们分配值时,它们会自动创建。例如,要添加“半径”属性,只需在创建“球”对象后执行此操作:
完成此操作后,假设您已将碰撞事件处理程序附加到球本身,则球将被传递作为“self”参数,您可以使用 self.radius 获取半径:
如果您已将事件处理程序附加到其他某个对象,则球将作为“event”参数的“other”属性传递。因此,使用您的井示例,如果您将事件处理程序附加到井上,那么您将获得球的半径:
当然,如果您有其他(非球)对象也可以击中井,并且那些对象没有“radius”属性,那么您必须首先确保“event.other”实际上是一个“ball”对象。如果不这样做,那么当您尝试获取半径时,您将得到“nil”。事实上,您可以使用此行为来检测另一个对象实际上是一个球:
希望这会有所帮助。我自己刚接触电晕一个月,所以...
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:
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:
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:
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:
Hope this helps. I'm only a month into Corona myself, so...