您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x 自定义N个碰撞矩形框。

2016-05-30 15:00 555 查看
--创建碰撞框-- 用于多个部位检测

function MathTools.createCollisionRect(sp, sp_custom)

local sp_t = {
 x = not sp_custom and sp:getPositionX()  or sp_custom.x or sp:getPositionX(),
 y = not sp_custom and sp:getPositionY()  or sp_custom.y or sp:getPositionY() , 
 w = not sp_custom and sp:getContentSize().width or sp_custom.w or sp:getContentSize().width , 
 h = not sp_custom and sp:getContentSize().height  or sp_custom.h or sp:getContentSize().height,
 ap = cc.p(sp:getAnchorPoint())

}
--忽略锚点  等价于 getBoundBox()

  sp_t.x = sp_t.x -  sp_t.w * sp_t.ap.x

  sp_t.y =  sp_t.y -  sp_t.h * sp_t.ap.y

 return sp_t

end

--碰撞检测 矩形   1 个部位 矩形框的大小

function MathTools.isCollisionToRect(sp_t,sp2,is_open_line,layer)
local is_rect = false
local collisionCfg = {}
local collision_id = {}
for __,v in pairs(sp2) do
local sp2 = v

for _,v2 in pairs(sp_t) do
local sp = v2
if is_open_line then
local  function DrawRect()
local rect_1 = cc.DrawNode:create()
rect_1:setName("drect"..rect_1:getTag())
rect_1:drawRect(cc.p(sp.x  , sp.y ),cc.p(sp.w + sp.x ,sp.h + sp.y),cc.c4b(255,100,0,100)) --后期记得加 颜色枚举
layer:addChild(rect_1,999)

local rect_2 = cc.DrawNode:create()
rect_2:setName("drect"..rect_2:getTag())
rect_2:drawRect(cc.p(sp2.x  , sp2.y ),cc.p(sp2.w + sp2.x ,sp2.h + sp2.y),cc.c4b(100,255,0,100)) --后期记得加 颜色枚举
layer:addChild(rect_2,999)
end 
DrawRect()--画框
end

    local is_rect = not ( sp.x > sp2.x + sp2.w or
                   sp.x + sp.w < sp2.x         or
                   sp.y > sp2.y + sp2.h        or
                   sp.y + sp.h < sp2.y )
   
    if is_rect == true then 
    --插入碰撞信息
    table.insert(collision_id ,1,{Language.Common.Collision[_].."->"..Language.Common.Collision[__]})

    table.insert(collisionCfg, 1 , { [1] = true ,[2] = collision_id }) 
    end

end

end

return collisionCfg

end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: