cocos2dx 检测是否点击到了图片的透明区域

直接上代码

bool FurnitureBase::clickMe(CCPoint p)
{

        bool k = false;
        for (int i = 0; i<rightAppearanceArray->count(); ++i)
        {
            Q5Sprite* qp =(Q5Sprite*) (rightAppearanceArray->objectAtIndex(i));
            if (qp->boundingBox().containsPoint(p))
            {
                int8_t data[4];
                CCPoint touchPoint = qp->convertToNodeSpace(baseLayer->convertToWorldSpace(p));
                
                CCPoint location = CCPoint((touchPoint.x), (touchPoint.y) );
                CCRenderTexture* renderTexture = CCRenderTexture::create(1,1,kCCTexture2DPixelFormat_RGBA8888);
                // CCLOG("%f",renderTexture->getAnchorPoint().x);
                renderTexture->beginWithClear(0,0,0,0);//只保存渲染一个像素的数据
                CCPoint oldPos = qp->getPosition();
                CCPoint oldAnchor = qp->getAnchorPoint();
                qp->setAnchorPoint(CCPoint(0,0));
                qp->setPosition(ccp(-location.x, -location.y));
                qp->visit();
                qp->setAnchorPoint(oldAnchor);
                qp->setPosition(oldPos);
                glReadPixels(0,0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, data);
                renderTexture->end();
                renderTexture->release();
                //检测alpha值
                // CCLOG("X:%.0f y:%.0f R: %d, G: %d, B: %d, A: %d tag:%d",location.x ,location.y, data[0], data[1], data[2], data[3],//this->getTag());
                //CCLOG("%f____________",-location.x);
                //CCLOG("%f____________",-location.y);
                //CCLOG("%d____________",data[2]);
                //CCLOG("%d____________",data[3]);
                if(data[3])
                {
                    //CCLOG("非透明");
                    k = ture;
                    break;
                }else{
                    // CCLOG("透明");
                }
            }
        }
    
    
    
    return k;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值