类型 事件 修订版 发行 2024.3703 关键词 碰撞,物理 另请参见 碰撞检测 (指南) preCollision postCollision physics.setAverageCollisionPositions() physics.setReportCollisionsInContentCoordinates()
结合使用 physics 库。将事件名称为 "collision"
的物理引擎 collision 事件通过 Corona 事件监听器模型公开。除了标准的 collision 事件外,Corona 还提供 preCollision 和 postCollision 事件。
根据是局部检测碰撞还是全局检测,对于所涉对象的引用方式也会有所不同。要了解更多有关这两种碰撞处理方法的信息,请参阅 碰撞检测 指南。
局部碰撞事件 | 全局碰撞事件 | |
---|---|---|
event.target 或 self |
event.object1 |
|
event.other |
event.object2 |
|
event.selfElement |
event.element1 |
|
event.otherElement |
event.element2 |
请比较这两个示例函数,以了解引用方式的变化
-- Local collision handling local function onLocalCollision( self, event ) print( event.target ) --the first object in the collision print( event.other ) --the second object in the collision print( event.selfElement ) --the element (number) of the first object which was hit in the collision print( event.otherElement ) --the element (number) of the second object which was hit in the collision end object.collision = onLocalCollision object:addEventListener( "collision" ) -- Global collision handling local function onGlobalCollision( event ) print( event.object1 ) --the first object in the collision print( event.object2 ) --the second object in the collision print( event.element1 ) --the element (number) of the first object which was hit in the collision print( event.element2 ) --the element (number) of the second object which was hit in the collision end Runtime:addEventListener( "collision", onGlobalCollision )
当碰撞涉及一个圆,并且碰撞结果以局部空间返回(请参阅 physics.setReportCollisionsInContentCoordinates())时,碰撞的局部空间位置始终为 0,0
。这是 Box2D 的一项限制。
目前,如果 Corona 代码尝试修改仍然涉及碰撞的对象,那么 Box2D 物理引擎在碰撞期间很容易崩溃。这是因为 Box2D 仍在对这些对象执行迭代的数学运算。请参阅 碰撞检测 指南,了解受此规则约束的函数和属性列表。