以下是事件处理函数,self
是cc.Node
,self.bg
是cc.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
如何比较location
和rect
以检查此精灵是否点击,我已阅读此文档,
http://www.cocos2d-x.org/reference/native-cpp/V3.2/d4/d8a/classcocos2d_1_1_rect.html#a011e04551ca371f8a99d2a3f47cd499e
似乎cc.rect
没有containsPoint
函数,还有其他方法可以解决这个问题吗?
答案 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