zoukankan      html  css  js  c++  java
  • Cocos2d-x 3.0 Lua编程 之 响应物理引擎的Contact事件回调不运行的问题

    在较早的版本号如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)


    注冊给事件 cc.Handler.EVENT_PHYSICS_CONTACT_BEGIN 的回调函数是会被调用的,然后到了3.0版本号后,以上的代码便无法正常工作,cc.Handler.EVENT_PHYSICS_CONTACT_BEGIN事件并未被触发,须要额外为以上代码中的2个刚体对象groudNode:getPhysicsBody()和spriteBird:getPhysicsBody()分别调用setContactTestBitmask(mask),当中mask取同一个值从而使得2个刚体对象属于同一个Contact測试组,这样才干保证当2个刚体接触时Contact事件能被触发,进而调用回调函数。

    正确的演示样例代码例如以下:

        -- add ground
        local groudNode = cc.Node:create()
        groudNode:setPhysicsBody(cc.PhysicsBody:createEdgeSegment(cc.p(0, landHeight), cc.p(288, landHeight)))
        groudNode:getPhysicsBody():setContactTestBitmask(1);
        layer:addChild(groudNode)
    
        -- add bird
        local spriteBird = createBird()
        spriteBird:setPhysicsBody(cc.PhysicsBody:createCircle(birdSize))
        spriteBird:getPhysicsBody():setContactTestBitmask(1);
        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)


  • 相关阅读:
    Eq Eqv Equal
    list append 总是复制前面的参数,而不复制最后一个参数
    Teach Yourself Scheme in Fixnum Days 13 Jump跳转
    python 操作 office
    python ImportError: DLL load failed: %1 不是有效的 Win32 应用程序
    pywin32 安装错误 ImportError: DLL load failed: 不是有效的 Win32 应用程序
    Python version 2.7 required, which was not found in the registry
    scheme 解释器Guile 使用
    Teach Yourself Scheme in Fixnum Days 6 recursion递归
    求一个正则表达式,字母不能重复
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4035703.html
Copyright © 2011-2022 走看看