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
在不同坐标系中 我们 可以先将 点的位置坐标 转换为相对 精灵父节点 的位置

本文介绍了在Cocos2d-x Lua中进行点击区域判断的方法,特别是处理不同坐标系的情况。通过使用`onTouch`监听触摸事件,并结合`getBoundingBox`、`convertToNodeSpace`等函数,将触摸坐标转换为精灵父节点的坐标,实现精确的点击判断。
最低0.47元/天 解锁文章
2408

被折叠的 条评论
为什么被折叠?



