在较早的版本如3.0beta使用如下代码的话:
-- add ground
local groudNode = cc.Node:create()
groudNode:setPhysicsBody(cc.PhysicsBody:createEdgeSegment(cc.p(0, landHeight), cc.p(288, landHeight)))
layer:addChild(groudNode)
-- add bird
local spriteBird = createBird()
spriteBird:setPhysicsBody(cc.PhysicsBody:createCircle(birdSize))
layerBg:addChild(spriteBird)
local function onContactBegin(contact)
print("onContactBegin")
return true;
end
-- bind contact listenser
local contactListener = cc.EventListenerPhysicsContactWithBodies:create(groudNode:getPhysicsBody(), spriteBird:getPhysicsBody())
contactListener:registerScriptHandler(onContactBegin, cc.Handler.EVENT_PHYSICS_CONTACT_BEGIN)
layer:getEventDispatcher():addEventListenerWithSceneGraphPriority(contactListener, layer)
注册给事件