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



--创建碰撞框-- 用于多个部位检测
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值