位置: 编程技术 - 正文

cocos2dx 不规则按钮的实现

编辑:rootadmin

推荐整理分享cocos2dx 不规则按钮的实现,希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:,内容如对您有帮助,希望把文章链接给更多的朋友!

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

cocos2dx 不规则按钮的实现

刚开始试图通过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(, )); this->addChild(this->m_imgMan, 1); this->m_pRenderTexture = CCRenderTexture::create(this->m_imgMan->getContentSize().width, this->m_imgMan->getContentSize().height, kCCTexture2DPixelFormat_RGBA); this->m_pRenderTexture->ignoreAnchorPointForPosition(true); this->m_pRenderTexture->setPosition(ccp(, )); 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 >> ) & 0xff; //Alpha通道,我们有用的就是Alpha color4B.a = (*pixel >> ) & 0xff; CCLOG("当前点击的点的: alpha = %d", color4B.a); if (color4B.a > ) { 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", ); this->m_pLabTips->setAnchorPoint(CCPointZero); this->m_pLabTips->setPosition(ccp(.0f, .0f)); this->m_pLabTips->setColor(ccYELLOW); this->addChild(this->m_pLabTips, 1); } void HelloWorld::registerWithTouchDispatcher() { CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, CCLayer::getTouchPriority(), false); }

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

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

本文由CC原创总结,如需转载请注明出处:

【cocos2dx】如何实现场景的跳转 前面定义了一个SpalshScene.h,并声明了如下的函数,在SplashScene.cpp中对这些函数实现,跳转用到了CCNode类的定时器有关的函数,详细参考书,这里采用的

cocos2dx ActionManager播放动画回调问题 ActionManager播放动画回掉问题问题描述:我在studio编辑器里编辑了一个界面,里面有一个动画,我想当动画执行完毕后,再执行一些其他操作,然后就使

RapidJSON的一些方便操作的宏定义 以前是一直使用CCJsonConventer去把JSON字符串转化为CCDictionary对象的,现在使用cocos2d-x3.x后,不推荐使用CCDictionary了,而且,JSON库也换成了rapidjson,不过我

标签: cocos2dx 不规则按钮的实现

本文链接地址:https://www.jiuchutong.com/biancheng/372603.html 转载请保留说明!

上一篇:Cocos2dx -lua QuickXDev拓展

下一篇:【cocos2dx】如何实现场景的跳转(cocos2d怎么用)

  • 个人独资企业需要报税吗
  • 增值税发票是什么纸张型号
  • 报销为什么不能多家赔付
  • 运输企业内账
  • 发票能减多少税
  • 营业收入净额是营业总收入吗
  • 文件柜材质
  • 知道税负率和收入怎么算进项
  • 计提坏账又收回
  • 报销招待费事由有哪些
  • 非盈利组织是两套账吗?
  • 小规模纳税人第一次网上报税
  • 新个税聘用退休后怎么算
  • 债券投资的风险主要有
  • 典当行借贷属于民间借贷吗
  • 公司自建厂房需要缴纳哪些税
  • 非金融机构借款计入什么科目
  • 金碟系统操作
  • 进口产品增值税的组成计税价格
  • 三证合一后新办企业多久去税务登记
  • 发票上折扣怎么记账
  • 对外投资的风险及对策
  • 子公司代发母公司人员工资
  • 政府奖励金额是否要交二次税呢
  • 公司税务风险怎么解决
  • 产品配件属于什么会计科目
  • 用人单位发放工资时跨行手续费应由谁支付
  • ajax调用php函数
  • 月末结转营业税金及附加分录
  • 事业单位财产清查怎么进行
  • php生成html
  • 出口商品怎么做分录
  • 简述php中常用魔术方法及其各自的作用
  • framework for
  • AttributeError: ‘bytes‘ object has no attribute ‘encode‘异常解决方案
  • php数组的类型有哪些
  • tune a video:one-shot tuning of image diffusion models for text-to-video generation
  • cv计算机视觉定义
  • 跟日期有关的名字
  • un删除
  • 小规模纳税人增值税月末处理
  • 财务费用汇兑损益在借方还是贷方
  • 补缴以前年度企业所得税如何做账
  • 现金盘盈盘亏怎么处理
  • 取得了水电费发票怎么做
  • 土石方工程开票范围
  • 帝国cms使用手册
  • python 定制类
  • Price Comparison Script
  • 怎么计算多个表格的某一数据
  • phpcms添加内容
  • 房产税中出租房产原值怎么算
  • sql2008用户sa登录失败
  • 金税四期有很多不实的传言
  • 废弃土地怎么认定
  • 工程结算审核资料清单
  • 余利宝和余额宝哪一个安全
  • 筹建期的开办费开业后如何处理
  • 预付货款用什么会计科目
  • 付款成功的钱怎么返还
  • 汽车三产件
  • sqlserver r服务
  • qpso算法
  • win10预览版最新
  • xp系统本地连接启用不了
  • win7怎么随便放桌面图标
  • 在linux系统中
  • 关机你的电脑遇到问题,需要重新启动,我们只收集
  • windows alt r
  • win10天气系统怎么弄掉
  • 安卓接入点
  • 置顶在线
  • Android游戏开发教程
  • 转换目录的命令
  • 简单的jquery插件实例
  • jquery html函数
  • unity 3d 介绍
  • 用js自动判断浏览记录
  • pip install clashroyale
  • 四川省税务局发票查询
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设