
最近研究了一下像素级的触摸处理,有时候我们用一个不规则的图形作为一个按钮,这个不规则的图形是一张矩形的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,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}; 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); } 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://www.jb51.cc/article/p-hanpyxyd-g.html 转自:http://blog.csdn.net/oktears/article/details/37993871 总结
以上是内存溢出为你收集整理的cocos2d-x像素级触摸处理全部内容,希望文章能够帮你解决cocos2d-x像素级触摸处理所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)