【问题标题】:In Cocos2d-x 3.2(Lua), how to detect if point is in a rect?在 Cocos2d-x 3.2(Lua) 中,如何检测点是否在矩形中?
【发布时间】:2014-08-26 04:05:21
【问题描述】:

这里是事件句柄函数,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 功能,有没有其他办法处理?

【问题讨论】:

    标签: lua cocos2d-x


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      相关资源
      最近更新 更多