cocos2d-x像素级触摸处理

来源:互联网 发布:javascript有map吗 编辑:程序博客网 时间:2024/06/13 13:03
cocos2d-x像素CCImage触摸透明

最近研究了一下像素级的触摸处理,有时候我们用一个不规则的图形作为一个按钮,这个不规则的图形是一张矩形的png图片,很可能图片的实际有效的显示内容只占整个png图片的很小一部分,剩下的大部分都是png图片的透明区域,我们想把这部分透明区域过滤掉,实现一个触摸到真实的内容才会有按钮响应的效果。

刚开始试图通过CCSprite直接获取到纹理的像素信息,但是cocos2d-x并没有给我们提供直接通过CCSprite获取像素信息的接口,研究了几个网上的Demo,发现通过使用RenderTexture重绘可以实现这一效果,下面把代码贴出来。

#include "HelloWorldScene.h"#include "SimpleAudioEngine.h"using namespace cocos2d;using namespace CocosDenshion;CCScene* HelloWorld::scene(){    // 'scene' is an autorelease object    CCScene *scene = CCScene::create();        // 'layer' is an autorelease object    HelloWorld *layer = HelloWorld::create();    // add layer as a child to scene    scene->addChild(layer);    // return the scene    return scene;}bool HelloWorld::init(){    if (!CCLayer::init()){        return false;    }        this->setTouchEnabled(true);    this->m_imgMan = CCSprite::create("man.png");    this->m_imgMan->setPosition(ccp(400, 200));    this->addChild(this->m_imgMan, 1);        this->m_pRenderTexture = CCRenderTexture::create(this->m_imgMan->getContentSize().width, this->m_imgMan->getContentSize().height, kCCTexture2DPixelFormat_RGBA8888);    this->m_pRenderTexture->ignoreAnchorPointForPosition(true);    this->m_pRenderTexture->setPosition(ccp(400, 200));    this->m_pRenderTexture->setAnchorPoint(CCPointZero);    this->addChild(this->m_pRenderTexture, 0, 1);        return true;}bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) {        bool isTouched = false;        CCPoint touchPoint = pTouch->getLocationInView();    CCPoint glPoint = CCDirector::sharedDirector()->convertToGL(touchPoint);    if (this->m_imgMan->boundingBox().containsPoint(glPoint)) {            ccColor4B color4B = {0, 0, 0, 0};                CCPoint nodePos = this->m_imgMan->convertTouchToNodeSpace(pTouch);        unsigned int x = nodePos.x;        unsigned int y = this->m_imgMan->getContentSize().height - nodePos.y;                CCPoint point = this->m_imgMan->getPosition();        //开始准备绘制        this->m_pRenderTexture->begin();        //绘制使用的临时精灵,与原图是同一图片        CCSprite* pTempSpr = CCSprite::createWithSpriteFrame(this->m_imgMan->displayFrame());        pTempSpr->setPosition(ccp(pTempSpr->getContentSize().width / 2, pTempSpr->getContentSize().height / 2));        //绘制        pTempSpr->visit();        //结束绘制        this->m_pRenderTexture->end();        //通过画布拿到这张画布上每个像素点的信息,封装到CCImage中        CCImage* pImage = this->m_pRenderTexture->newCCImage();        //获取像素数据        unsigned char* data_ = pImage->getData();        unsigned int *pixel = (unsigned int *)data_;        pixel = pixel + (y * (int)pTempSpr->getContentSize().width) * 1 + x * 1;        //R通道        color4B.r = *pixel & 0xff;        //G通道        color4B.g = (*pixel >> 8) & 0xff;        //B通过        color4B.b = (*pixel >> 16) & 0xff;        //Alpha通道,我们有用的就是Alpha        color4B.a = (*pixel >> 24) & 0xff;                CCLOG("当前点击的点的: alpha = %d", color4B.a);                if (color4B.a > 50) {            isTouched = true;        } else {            isTouched = false;        }                //绘制完成后清理画布的内容        this->m_pRenderTexture->clear(0, 0, 0, 0);    }            if (this->m_pLabTips) {        this->m_pLabTips->removeFromParentAndCleanup(true);        this->m_pLabTips = NULL;    }        return isTouched;}void HelloWorld::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) {        if (this->m_pLabTips) {        this->m_pLabTips->removeFromParentAndCleanup(true);        this->m_pLabTips = NULL;    }        this->m_pLabTips = CCLabelTTF::create("点击到非透明的像素点", "Courier", 30);    this->m_pLabTips->setAnchorPoint(CCPointZero);    this->m_pLabTips->setPosition(ccp(300.0f, 100.0f));    this->m_pLabTips->setColor(ccYELLOW);    this->addChild(this->m_pLabTips, 1);}void HelloWorld::registerWithTouchDispatcher() {    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, CCLayer::getTouchPriority(), false);}

这里我们把alpha通道的值小于50的像素点都视为同名的点,当点击到了透明的黑色区域时,屏幕上不显示任何文字,点击到有色区域时,观察打印日志信息:


实现的原理:我通过点击的时候把图片进行重绘,重绘的过程中,可以通过RenderTexture也就是画布,把整个画布上的像素点信息全部拿到,我让绘制的内容和画布的大小是一样的,所以就能保证画布上的每一个像素点就是我想要绘制的图片的像素点,然后通过判断像素点的alpha通道值,来确定这个点是否是透明色的,如果是透明色则不做触摸响应。


本文由CC原创总结,如需转载请注明出处:http://blog.csdn.net/oktears/article/details/37993871


转自:http://blog.csdn.net/oktears/article/details/37993871
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小孩子不爱读书不听话怎么办 小孩不爱读书写字怎么办 一年级学生不爱学习怎么办 孩子练字怕累怎么办 孩子不愿意学英语怎么办 生了儿子 不喜欢 怎么办 养两个儿子的怎么办 看诗词记不住怎么办 经常读书记不住怎么办 孩子不爱记数字怎么办 考研学不进去怎么办 读了职高后悔怎么办 不喜欢看书的人怎么办 生的儿子不喜欢怎么办 孩子上网不回家怎么办 儿子不想读书了怎么办 中考体育考不好怎么办 小孩突然没礼貌怎么办 小孩读书记忆差怎么办 这几天不爱吃饭怎么办 读书读到不懂的怎么办 读书很多词不懂怎么办 看书看不进去怎么办 一年级小孩不喜欢读书怎么办 考研还不想学习怎么办 怎么吃还是瘦怎么办 要想读的书该怎么办 大人瘦不爱吃饭怎么办 大人不爱吃青菜怎么办 如果不想写作业怎么办 一年级不爱写作业怎么办 生气总打孩子怎么办 人爱喝酒怎么办才不改 俩岁半宝宝不吃饭怎么办 孩子高中不爱学习怎么办 10岁不愿意吃饭怎么办 儿童老是不睡觉怎么办 二岁宝宝厌食怎么办 吃着了发烧怎么办 小孩记不住东西怎么办 婴儿口水多厌食怎么办