不知道3.3中关于这个碰撞检测的接口回调如何注册,搜索了好多,都是以前的例子,和现在不一样
看了samples里面的例子,压根就没有注册碰撞回调,这个如何处理,求教啊~
local contactListener = cc.EventListenerPhysicsContact:create();
contactListener:registerScriptHandler(onContactBegin, cc.Handler.EVENT_PHYSICS_CONTACT_BEGIN);
local eventDispatcher = layer:getEventDispatcher()
eventDispatcher:addEventListenerWithSceneGraphPriority(contactListener, layer);
3Q,试用你的方法,确实可行
现在讲sample里面的physics的粒子修改了一下,用以下代码替换掉其中的MainScene的内容即可
:867::867::867:
local MainScene = class("MainScene", function()
return display.newPhysicsScene("MainScene")
end)
local GRAVITY = -200
local COIN_MASS = 100
local COIN_RADIUS = 46
local COIN_FRICTION = 0.8
local COIN_ELASTICITY = 0.8
local WALL_THICKNESS = 64
local WALL_FRICTION = 1.0
local WALL_ELASTICITY = 0.5
function MainScene:ctor()
-----------------------------------------------------------------
----------------------注册碰撞监听-------------------------------
-----------------------------------------------------------------
local contactListener = cc.EventListenerPhysicsContact:create()
contactListener:registerScriptHandler(function(contact)
print("contactBegin", "-----------", type(contact))
return true
end, cc.Handler.EVENT_PHYSICS_CONTACT_BEGIN)
contactListener:registerScriptHandler(function(contact, solve)
print("contactPresolve", "-----------", type(contact), type(solve))
return true
end, cc.Handler.EVENT_PHYSICS_CONTACT_PRESOLVE)
contactListener:registerScriptHandler(function(contact, solve)
print("contactPostsolve", "-----------", type(contact), type(solve))
end, cc.Handler.EVENT_PHYSICS_CONTACT_POSTSOLVE)
contactListener:registerScriptHandler(function(contact)
print("contactSeperate", "-----------", type(contact))
end, cc.Handler.EVENT_PHYSICS_CONTACT_SEPERATE)
self:getEventDispatcher():addEventListenerWithFixedPriority(contactListener, 1)
-----------------------------------------------------------------
-- create touch layer
self.layer = display.newLayer()
self.layer:setTouchEnabled(true)
self.layer:addNodeEventListener(cc.NODE_TOUCH_EVENT, function(event)
return self:onTouch(event.name, event.x, event.y)
end)
self:addChild(self.layer)
-- create label
cc.ui.UILabel.new({text = "TAP SCREEN", size = 32, color = display.COLOR_WHITE})
:align(display.CENTER, display.cx, display.cy)
:addTo(self)
local leftWallSprite = display.newSprite("#Wall.png")
leftWallSprite:setScaleY(display.height / WALL_THICKNESS)
leftWallSprite:setPosition(display.left + WALL_THICKNESS / 2, display.cy + WALL_THICKNESS)
self:addChild(leftWallSprite)
local rightWallSprite = display.newSprite("#Wall.png")
rightWallSprite:setScaleY(display.height / WALL_THICKNESS)
rightWallSprite:setPosition(display.right - WALL_THICKNESS / 2, display.cy + WALL_THICKNESS)
self:addChild(rightWallSprite)
local bottomWallSprite = display.newSprite("#Wall.png")
bottomWallSprite:setScaleX(display.width / WALL_THICKNESS)
bottomWallSprite:setPosition(display.cx, display.bottom + WALL_THICKNESS / 2)
self:addChild(bottomWallSprite)
local wallBox = display.newNode()
wallBox:setAnchorPoint(cc.p(0.5, 0.5))
wallBox:setPhysicsBody(cc.PhysicsBody:createEdgeBox(cc.size(display.width - WALL_THICKNESS*2, display.height - WALL_THICKNESS)))
wallBox:setPosition(display.cx, display.height/2 + WALL_THICKNESS/2)
self:addChild(wallBox)
end
function MainScene:createCoin(x, y)
-- add sprite to scene
local coinSprite = display.newSprite("#Coin.png")
self:addChild(coinSprite)
local coinBody = cc.PhysicsBody:createCircle(COIN_RADIUS, cc.PhysicsMaterial(COIN_RADIUS, COIN_FRICTION, COIN_ELASTICITY))
-----------------------------------------------------------------
-----设置分类掩码和碰撞掩码,这里设置的是硬币和硬币碰撞----------
-----------------------------------------------------------------
coinBody:setCategoryBitmask(1)
coinBody:setContactTestBitmask(1)
-----------------------------------------------------------------
coinBody:setMass(COIN_MASS)
coinSprite:setPhysicsBody(coinBody)
coinSprite:setPosition(x, y)
end
function MainScene:onTouch(event, x, y)
if event == "began" then
self:createCoin(x, y)
end
end
return MainScene
```