Corona physics engine Collision event object
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 "se开发者_如何转开发lf" and "event" parameters. Is there a simple way to check the radius of the "Ball" that hits the well?
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...
精彩评论