preCollision

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

概述

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

对象引用

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

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

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

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

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

陷阱

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

属性

event.contact

event.element1

event.element2

event.name

event.object1

event.object2

event.other

event.otherElement

event.selfElement

event.target

event.x

event.y