cocos 2dx Lua 点击区域判断

onTouch见 上篇 


self:onTouch(function (event)

    print("----", event.x, event.y, event.name)
    local point = cc.p(event.x, event.y)
    if cc.rectContainsPoint(sp:getBoundingBox(), cc.p(event.x, event.y)) then
    print("点击了sp")
    else
    print("没有点中sp")
    end
    return true

    end, nil, true)

此为在同一坐标系中的方法, 在不同坐标系中就是错误的了


if cc.rectContainsPoint(sp1:getBoundingBox(), sp1:getParent():convertToNodeSpace(cc.p(event.x, event.y))) then
    print("点击了sp1")
else
    print("没有点中sp1")
end

在不同坐标系中 我们 可以先将 点的位置坐标 转换为相对 精灵父节点 的位置

猜你喜欢

转载自blog.csdn.net/ooomyself/article/details/78136294