关于不规则按钮点击问题

请问各位大神,怎样让不规则按钮的透明区域不响应鼠标事件啊

搜到了,还没验证过,楼主可以试下

//获得点击的OpenGL的世界坐标值
CCPoint touchPoint = convertTouchToNodeSpace(touch);

CCPoint location = ccp((touchPoint.x) * CC_CONTENT_SCALE_FACTOR(), (touchPoint.y) * CC_CONTENT_SCALE_FACTOR());

UInt8 data;
CCRenderTexture* renderTexture = CCRenderTexture::create(1* CC_CONTENT_SCALE_FACTOR(),1 * CC_CONTENT_SCALE_FACTOR(), kCCTexture2DPixelFormat_RGBA8888);
renderTexture->beginWithClear(0,0,0,0);
//只保存渲染一个像素的数据
CCPoint oldPos = this->getPosition();
CCPoint oldAnchor = this->getAnchorPoint();
this->setAnchorPoint(ccp(0,0));
this->setPosition(ccp(-location.x, -location.y));
this->visit();
this->setAnchorPoint(oldAnchor);
this->setPosition(oldPos);
glReadPixels(0,0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, data);
renderTexture->end();
renderTexture->release();
//检测alpha值
if(data || data || data || data)
{
//非透明
return true;
}
//透明
return false;

Quick中自带一个取sprite某点颜色的接口

function Sprite:getColor(__point, __convertToNodeSpace, __isFloat)

楼主可直接用这个

用了这个,但是不知道会不会影响性能之内的,每次点击都要new一个sprite,虽然不添加到界面
button:addTouchEventListener(handler(self, self.test))
function TestClass:test(sender,eventType)
local size=sender:getContentSize()
local point= sender:convertToNodeSpace(CCPoint(sender:getTouchStartPos().x,sender:getTouchStartPos().y))
local rect=cc.RectMake(-size.width/2,-size.height/2,size.width,size.height)
if(self:getAlpha(size,point)<1)then return end
if eventType == ccs.TouchEventType.began then

 elseif eventType == ccs.TouchEventType.ended then
            
 end

end

function TestClass:getAlpha(size,point)
local img=display.newSprite(“button图片路径”)
return img:getColorAlpha(CCPoint(point.x,point.y))
end

1赞

3.0以上没有合理的解决办法啊

我也在找3.x上获取像素的方法。

一楼方法可用!

能用吗?
为啥我按照他的改写了。但是有问题呀。
基于quick 3.3。

可以正常获取rgb 但是a永远是255
我去除掉了sprite:visit 不影响任何结果。怎么使用或者不使用sprite:visit后,对readPixels没有什么影响。。

对gl.readPixels的机制不是很懂。
下面是是我的部分代码:

function ColorLumpManager:alphaCheck(sprite, touch)
    local location = touch

    local data = 4
    local renderTexture = cc.RenderTexture:create(1, 1, cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A8888)
    renderTexture:begin()
    --只保存渲染一个像素的数据
    sprite:visit()
    renderTexture:endToLua()
    local vt = gl.readPixels(location.x, location.y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, data)
    dump(vt)
end

=========================望论坛大神们指点答疑

好像懂了什么。。

那现在的3.3版本怎么解决这个需求呢?有人做过吗?

你找到了么 解决办法

感觉3楼的方法可行.以前是在按钮上加了一个同样大小的renderTexture,点击时,每次都是遍历像素信息,来确实透明值