今天做点和矩形的碰撞检测,发现Quick自己封的检测函数好像有点问题,把代码贴出来:
quick自己封的:
function cc.rectContainsPoint( rect, point )
local ret = false
if (point.x >= rect.x) and (point.x <= rect.x + rect.width) and
(point.y >= rect.y) and (point.y <= rect.y + rect.height) then
ret = true
end
return ret
end
我自己用:rect = getBoundingBox()获取到了精灵的矩形大小以及位置
分析rect的数据:
rect.x = 精灵的当前X坐标
rect.y = 精灵的当前Y坐标
rect.width = 精灵矩形宽度
rect.height = 精灵矩形高度
得到的x,y坐标是精灵的中点位置坐标,而不是矩形左下角坐标
那么用quick自己封的cc.rectContainsPoint就有问题了,他检测碰撞点在矩形的右边半部分,而左边半部分就没检测,所以就出问题了
不知道是我自己函数没用对还是什么问题,请大家帮忙看一下
下面是我自己的修改后的检测函数:
function Game:rectContainsPoint( rect, point )
local ret = false
if (point.x >= rect.x-rect.width/2) and (point.x <= rect.x + rect.width/2) and
(point.y >= rect.y-rect.height/2) and (point.y <= rect.y + rect.height/2) then
ret = true
end
return ret
end