在Cocos2d-x 3.2(Lua)中,如何检测点是否在矩形中?

时间:2014-08-26 04:05:21

标签: lua cocos2d-x

以下是事件处理函数,selfcc.Nodeself.bgcc.Sprite

local function onTouchEnded(touch, event)
        local location = touch:getLocation()
        local size = self.bg:getContentSize() 
        local rect = cc.rect(self:getPositionX(),self:getPositionY(),size.width,size.height)

end

如何比较locationrect以检查此精灵是否点击,我已阅读此文档, http://www.cocos2d-x.org/reference/native-cpp/V3.2/d4/d8a/classcocos2d_1_1_rect.html#a011e04551ca371f8a99d2a3f47cd499e

似乎cc.rect没有containsPoint函数,还有其他方法可以解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

if location.x > rect.x and location.x < rect.x + rect.width and
   location.y > rect.y and location.y < rect.y + rect.height then
   -- location is in rect, do your stuff
end