cocos2dx 3.0之判断点击精灵透明区域

本站文章转载务必在明显处注明:原文链接 http://blog.csdn.net/cjsen/article/details/17241027

前言

在Layer层中 如何判断触摸事件 在图片精灵中,触摸点是否在图片的透明区域

实现

                if(p1.containsPoint(p)){
                    int8_t data[4];
                    Point touchPoint = node -> convertTouchToNodeSpace(touch);
                    Point location = Point((touchPoint.x) * CC_CONTENT_SCALE_FACTOR(), (touchPoint.y) * CC_CONTENT_SCALE_FACTOR());
                    RenderTexture* renderTexture = RenderTexture::create(1* CC_CONTENT_SCALE_FACTOR(),1 * CC_CONTENT_SCALE_FACTOR(), Texture2D::PixelFormat::RGBA8888);
                    renderTexture->beginWithClear(0,0,0,0);//只保存渲染一个像素的数据
                    Point oldPos = node->getPosition();
                    Point oldAnchor = node->getAnchorPoint();
                    node->setAnchorPoint(Point(0,0));
                    node->setPosition(Point(-location.x, -location.y));
                    node->visit();
                    node->setAnchorPoint(oldAnchor);
                    node->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());
                    if(data[0] || data[1] || data[2] || data[3])
                    {
                        CCLOG("非透明");
                    }else{
                        CCLOG("透明");
                    }
                }

其中当触摸事件为已在精灵内部,进而再判断是否在透明区域,其中node为要判断的精灵

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值