Physics 在经过replaceScene更新当前界面后, 会出现重复碰撞的问题

已经解决

在onExit 时,
c.Director:getInstance():getEventDispatcher():removeEventListener(self.contactListener)

  1. 可以把下面代码建立一个Scene , 然后观察print(“log…”)

当球第一次与方块接触时, 每次接触, 打印一次 log …

  1. 然后点击Restart

这时可以明显看出来, 球与方块接触时, 每次打印两次 log …

这个情况怎么解决??

补充: quick-cocos2d-x 3.2rc1 ,3.3rc1 都有这个问题

local c = cc
local roleCtrl = require("app.controller.roleCtrl")

local RoleTestScene = class("RoleTestScene", function()
    return display.newPhysicsScene("GameScene")
end)



function RoleTestScene:ctor()
    self.world= self:getPhysicsWorld()
    self.world:setGravity(c.p(0,-100))
    self.world:setDebugDrawMask(cc.PhysicsWorld.DEBUGDRAW_ALL)




        -- 碰撞监听处理...... 
    self.contactListener = c.EventListenerPhysicsContact:create()
    self.contactListener:registerScriptHandler(handler(self,self.onContactBegin), c.Handler.EVENT_PHYSICS_CONTACT_BEGIN)
    local eventDispathcher = c.Director:getInstance():getEventDispatcher()
    eventDispathcher:addEventListenerWithFixedPriority(self.contactListener, 1)


end

function RoleTestScene:onEnter()


    -- 盒子.... 
    local box = display.newRect(cc.rect(display.cx , display.cy, display.width , display.height) , 
        {fillColor = cc.c4f(0,0,0,1), borderColor = cc.c4f(0,1,0,1), borderWidth = 1} )
    :addTo(self)


    :align(display.CENTER , display.cx , display.cy )
    local boxBody = c.PhysicsBody:createEdgeBox(c.size(display.width-100 , display.height -100 ))
    boxBody:setCategoryBitmask(0x01)
    boxBody:setContactTestBitmask(0x05)
    box:setPhysicsBody(boxBody)



    -- 自由下落物体
    local  fallBox= cc.PhysicsBody:createCircle(100)
    fallBox:setCategoryBitmask(0x05)
    fallBox:setContactTestBitmask(0x01)
    fallBox:getShape(0):setRestitution(0)
    local fallRect = display.newRect(cc.rect(display.cx , display.cy, 100, 100))
        :addTo(self)
        :align(display.CENTER , display.cx , display.cy )

    fallRect:setPhysicsBody(fallBox)

    --
    local role = roleCtrl.new("M5" , 2,1,40 ,2,true , self.world )

    :addTo(self)
    :align(display.CENTER , display.cx , display.cy)
    role:setDefaultRun()
    role:Start()
    ]]

    cc.ui.UIPushButton.new()
    :addTo(self)
    :align(display.LEFT_BOTTOM , 100 , 100)
    :setButtonLabel("normal", cc.ui.UILabel.new({text="Restart" , size=40}))
    :onButtonClicked(function() 
        display.replaceScene(require("app.scenes.RoleTestScene").new())
    end)
end

function RoleTestScene:onExit()

end


function RoleTestScene:onContactBegin(event)
    print("log........ ")
    return true
end

return RoleTestScene


```