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
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
相关文章推荐
- cocos2dx-Action-圆周运动
- Cocos2d 内存优化
- cocos-js,Widget的addTouchEventListener改善
- cocos2dx - Chipmunk 物理引擎 -个人笔记
- Cocos2d 屏幕适配方案
- iOS配置cocos2d-x
- Cocos2d-js 对象缓冲池cc.pool
- Cocos2d-JS schedule用法
- Cocos2d3.x之C3130: 编译器错误: 未能将插入的代码块写入PDB
- Cocos2d-3.x Android环境搭建
- cocos2dx 3.x粒子系统
- Cocos2d-x中使用音频CocosDenshion引擎介绍与音频文件的预处理
- Cocos2dx3.10在Windows平台搭建Android命令行编译apk打包
- cocosPods 常见使用步骤
- Cocos2dx 世界、屏幕、openGL、本地等坐标系
- VS2012 Cocos2D-X 环境搭建
- Cocos2d-x教程第(8)讲-遮罩层实现捕鱼达人滚动数字表盘
- 【cocos2d-x 手游研发----博彩大转盘】
- 使用 CocoStudio 创建 Cocos2d-x 序列帧和骨骼动画
- Cocos2d-x 之 CCGUI设计与实现