PhysicsContact.isEnabled

类型 布尔值
对象 PhysicsContact
physics.*
修订 版本 2024.3703
关键词 物理接触, isEnabled
另请参阅 preCollision(碰撞前)

概述

启用或禁用与此接触相关的碰撞。这通常在 preCollision(碰撞前) 事件侦听器中使用,可用于构建单侧平台,角色可以垂直跳过这些平台,但只能从下方跳过。

注意事项

该接触仅在当前时间步长内启用/禁用。未来的接触将照常注册。

示例

local platform = display.newRect( 0, 0, 280, 30 )
platform.x, platform.y = display.contentCenterX, display.contentCenterY+80
platform.collType = "passthru"
physics.addBody( platform, "static", { bounce=0.0, friction=0.3 } )

local ball = display.newCircle( 0, 0, 15 )
ball.x, ball.y = display.contentCenterX, display.contentCenterY-40
physics.addBody( ball, "dynamic", { bounce=0.0, radius=20 } )

local function preCollisionEvent( self, event )

   local collideObject = event.other
   if ( collideObject.collType == "passthru" ) then
      event.contact.isEnabled = false  --disable this specific collision
   end
end

ball.preCollision = preCollisionEvent
ball:addEventListener( "preCollision" )