cocos2d-x lua physics

cocos2d-x lua physics,第1张

概述在cocos2d-x 3.x封装了物理引擎 在layer中添加几个精灵 require("Cocos2d")require("Cocos2dConstants")require("bitExtend")LayerPhysics=class("LayerPhysics",function () return cc.Layer:create()end)function LayerPh

在cocos2d-x 3.x封装了物理引擎


在layer中添加几个精灵

require("Cocos2d")require("Cocos2dConstants")require("bitExtend")LayerPhysics=class("LayerPhysics",function ()	return cc.Layer:create()end)function LayerPhysics:ctor()--  碰撞监听   local conListener=cc.EventListenerPhysicsContact:create();    conListener:registerScriptHandler(function(contact)            print("---contact-碰撞了--")return true    end,cc.Handler.EVENT_PHYSICS_CONTACT_BEGIN)        conListener:registerScriptHandler(function()        print("seperate")        end,cc.Handler.EVENT_PHYSICS_CONTACT_SEPERATE)        cc.Director:getInstance():getEventdispatcher():addEventListenerWithSceneGraPHPriority(conListener,self)		endfunction LayerPhysics:createLayer()	local layer=LayerPhysics.new()		layer:createSprite()		return layerendfunction LayerPhysics:createSprite()local size=cc.Director:getInstance():getWinSize()--创建一个围绕屏幕四周的物理边界local node=cc.Node:create()    node:setPhysicsBody(cc.PhysicsBody:createEdgeBox(size,cc.PHYSICSBODY_MATERIAL_DEFAulT,5))    node:setposition(size.wIDth/2,size.height/2)    self:addChild(node)            --一个body的categoryBitmask和另一个body的ContactTestBitmask的逻辑与的结果不等于0时,接触事件将被发出,否则不发送。--一个body的categoryBitmask和另一个body的CollisionBitmask的逻辑与结果不等于0时,他们将碰撞,否则不碰撞--需要两个body相互位与运算的值都是大于0时才会发生碰撞检测和发送接触事件通知       local sp1=cc.Sprite:create("test/logo.png")    local b1=cc.PhysicsBody:createBox(sp1:getContentSize())    b1:setDynamic(false)    --    b1:getShape(0):setFriction(0)--    b1:getShape(0):setRestitution(1)    --    b1:setcategoryBitmask(0x01) --类别掩码 默认值为0xFFFFFFFF--    b1:setContactTestBitmask(0x01) --接触掩码 默认值为 0x00000000--    b1:setCollisionBitmask(0x01) --碰撞掩码 默认值为0xFFFFFFFF    b1:setcategoryBitmask(0x0d)    b1:setContactTestBitmask(0x0d)    b1:setCollisionBitmask(0x0d)    sp1:setPhysicsBody(b1)    sp1:setposition(60,sp1:getContentSize().height/2)    sp1:setTag(1)    self:addChild(sp1)    print("b1:",b1:isDynamic())        print("bit:"..bit._and(0x0d,0x03))        local sp2=cc.Sprite:create("test/logo.png")    local b2=cc.PhysicsBody:createCircle(sp2:getContentSize().wIDth/2);    b2:setDynamic(true)    --    b2:getShape(0):setFriction(0)--    b2:getShape(0):setRestitution(1)    --    b2:setcategoryBitmask(0x03)--    b2:setContactTestBitmask(0x01)--    b2:setCollisionBitmask(0x01)    b2:setcategoryBitmask(0x03)    b2:setContactTestBitmask(0x03)    b2:setCollisionBitmask(0x03)    sp2:setPhysicsBody(b2)    sp2:setposition(200,sp2:getContentSize().height/2)    sp2:setTag(2)    self:addChild(sp2)    print("b2:",b2:isDynamic())               local sp3=cc.Sprite:create("test/logo.png")    local b3=cc.PhysicsBody:createBox(sp3:getContentSize())    b3:setDynamic(false)    b3:setcategoryBitmask(0x03)    b3:setContactTestBitmask(0x02)    b3:setCollisionBitmask(0x02)    sp3:setPhysicsBody(b3)    sp3:setTag(3)    sp3:setposition(250,320)    self:addChild(sp3)                --    cc.Director:getInstance():getRunningScene():getPhysicsWorld():addJoint(--        cc.PhysicsJointdistance:construct(b1,b2,cc.vertex2F(0,0),sp2:getContentSize().wIDth/2)))     --    只对两个body监听     local l=cc.EventListenerPhysicsContactWithBodIEs:create(b1,b2)    ---    --@param  cc.PhysicsContact contact    l:registerScriptHandler(function(contact)    --    处理游戏中精灵碰撞逻辑            print("getShapeA:",contact:getShapeA():ge@R_502_5998@():getNode())             local node1=contact:getShapeA():ge@R_502_5998@():getNode()        print("tag:",node1:getTag())                local node2=contact:getShapeB():ge@R_502_5998@():getNode()                  print("pp")       end,cc.Handler.EVENT_PHYSICS_CONTACT_BEGIN)    cc.Director:getInstance():getEventdispatcher():addEventListenerWithSceneGraPHPriority(l,self)	--触摸监听    local Listener=cc.EventListenertouchOneByOne:create()    Listener:setSwallowtouches(true)    ---    --@param touch cc.touch    --@param event cc.Event    Listener:registerScriptHandler(function(touch,event)            sp2:getPhysicsBody():setVeLocity(cc.p(0,100))            local target = event:getCurrentTarget()            local locationInNode = target:convertToNodeSpace(touch:getLocation())            local s = target:getContentSize()            local rect = cc.rect(0,s.wIDth,s.height)            --触摸点是否在精灵上            if cc.rectContainsPoint(rect,locationInNode) then                return true            end            return false    end,cc.Handler.EVENT_touch_BEGAN)    Listener:registerScriptHandler(function(touch,event)            local target = event:getCurrentTarget()            local posX,posY = target:getposition()            local delta = touch:getDelta()            target:setposition(cc.p(posX + delta.x,posY + delta.y))    end,cc.Handler.EVENT_touch_MOVED)    local eventdispatcher = self:getEventdispatcher()    --    精灵2添加触摸事件    eventdispatcher:addEventListenerWithSceneGraPHPriority(Listener,sp2)	--	精灵3添加触摸事件    eventdispatcher:addEventListenerWithSceneGraPHPriority(Listener:clone(),sp3)	endreturn LayerPhysics


创建一个带物理世界的场景

require("Cocos2d")require("Cocos2dConstants")require("LayerPhysics")PhysiCSScene=class("PhysiCSScene",function ()	return cc.Scene:createWithPhysics()end)function PhysiCSScene:create()	local scene=PhysiCSScene.new();	    --	设置PhysicsWorld调试(有红色的框框)    scene:getPhysicsWorld():setDeBUGDrawMask(cc.PhysicsWorld.DEBUGDRAW_ALL)--    scene:getPhysicsWorld():setSpeed(20)        local layer=LayerPhysics:createLayer()    scene:addChild(layer)        return sceneendreturn PhysiCSScene



cocos2d-x还提供了计算十六进制的值,使用时需要
require("bitExtend")
print("bit:"..bit._and(0x0d,0x03))

总结

以上是内存溢出为你收集整理的cocos2d-x lua physics全部内容,希望文章能够帮你解决cocos2d-x lua physics所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1017882.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-23
下一篇2022-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存