碰撞

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

概述

结合使用 physics 库。将事件名称为 "collision" 的物理引擎 collision 事件通过 Corona 事件监听器模型公开。除了标准的 collision 事件外,Corona 还提供 preCollisionpostCollision 事件。

对象参考

根据是局部检测碰撞还是全局检测,对于所涉对象的引用方式也会有所不同。要了解更多有关这两种碰撞处理方法的信息,请参阅 碰撞检测 指南。

局部碰撞事件   全局碰撞事件
event.targetself   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 )

疑难杂症

属性

event.contact

event.element1

event.element2

event.name

event.object1

event.object2

event.other

event.otherElement

event.phase

event.selfElement

event.target

event.x

event.y