cocos2d-x lua physics

前端之家收集整理的这篇文章主要介绍了cocos2d-x lua physics前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在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)

	
	
end



function LayerPhysics:createLayer()
	local layer=LayerPhysics.new()
	
	layer:createSprite()
	
	return layer
end


function 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():getBody():getNode())
     
        local node1=contact:getShapeA():getBody():getNode()
        print("tag:",node1:getTag())
        
        local node2=contact:getShapeB():getBody():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)


	
end

return 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 scene
end

return PhysicsScene



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

原文链接:https://www.f2er.com/cocos2dx/345827.html

猜你在找的Cocos2d-x相关文章