postCollision

类型 事件
修订 版本 2024.3703
关键词 postCollision, 物理引擎
另请参阅 碰撞检测 (指南)
collision
preCollision
physics.setAverageCollisionPositions()
physics.setReportCollisionsInContentCoordinates()

概述

physics 库结合使用。物理引擎碰撞后事件通过 Corona 事件监听器模型公开,事件名称为 "postCollision"。除了这些事件和标准的 collision 事件之外,Corona 还提供了 preCollision 事件。

对象引用

根据是在本地还是全局检测到碰撞后事件,对相关对象的引用会有所不同。有关这两种碰撞处理方法的更多信息,请参阅 碰撞检测 指南。

本地碰撞事件   全局碰撞事件
event.targetself   event.object1
event.other   event.object2
event.selfElement   event.element1
event.otherElement   event.element2

请比较这两个示例函数以了解引用变量

-- Local postCollision handling
local function onLocalPostCollision( 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.postCollision = onLocalPostCollision
object:addEventListener( "postCollision" )

-- Global postCollision handling
local function onGlobalPostCollision( 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( "postCollision", onGlobalPostCollision )

陷阱

物理引擎碰撞后事件非常“嘈杂”,每次接触可能会报告多次,从而可能影响性能。因此,只有在真正需要碰撞后检测时才应该监听这些事件。我们还建议您在感兴趣的对象中使用本地侦听器,而不是全局侦听物理世界中的所有 "postCollision" 事件。

属性

event.contact

event.element1

event.element2

event.force

event.friction

event.name

event.object1

event.object2

event.other

event.otherElement

event.selfElement

event.target

event.x

event.y