Cocos2dx Widget button透明区域过滤
小伟哥 遇到一个命题:
button透明区域过滤。当点击一个建筑button、花的时候不得不想一些方法把点击透明区域过滤掉。
让点击也没有效果滴啦。
開始搜索了半天才有所思路。
在网络上非常多贴代码的。
http://blog.csdn.net/lwuit/article/details/40658347
整理后代码例如以下:
bool CCMenu::CheckAlphaPoint(CCMenuItem* pChild, const CCPoint& point)
{
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCNode* selectSprite = ((CCMenuItemSprite*)pChild)->getSelectedImage(); CCRenderTexture *renderer = CCRenderTexture::create(winSize.width, winSize.height);
renderer->begin(); bool visible = selectSprite->isVisible();
if (visible) {
selectSprite->visit();
}
else
{
selectSprite->setVisible(true);
selectSprite->visit();
selectSprite->setVisible(false);
} GLubyte pixelColors[4]; #if ( CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
glReadPixels(point.x, point.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixelColors[0]);
#else
glReadPixels(point.x, point.y, 1, 1, GL_ALPHA, GL_UNSIGNED_BYTE, &pixelColors[0]);
#endif int alpha = pixelColors[0];
CCLOG("----alpha %d", alpha); renderer->end(); if (alpha <= 30)
{
return true;
}
else
{
return false;
} }
上面代码的确在測试project上面直接简历个ccsprite 活着 menuitem 是能够运行的。
随着UI工具的进步。我们选择了CocoStudio 的 Widget 。方便了你我啊。
可是可可是,把上面的代码贴过来,试了试真心不能用啊。
有些同志,到此放弃了对知识原理的探究。
程序就是苦啊。遇到这种问题必须往下研究不是?
经过了多重推敲与图纸猜測。
后来发现了出现故障的根本原因:
CCRenderTexture *renderer 渲染后不能得到位置上面的颜色值 为0 00000为什么为0
visit()好不好使?各种疑惑
bool Widget::onTouchBegan(CCTouch *touch, CCEvent *unused_event)
{
_touchStartPos = touch->getLocation();
_hitted = isEnabled()
& isTouchEnabled()
& hitTest(_touchStartPos)
& clippingParentAreaContainPoint(_touchStartPos); if (!_hitted)
{
return false;
} // add yww alpha check
if (!AlphaTouchCheck(_touchStartPos))
{
return false;
} setFocused(true);
Widget* widgetParent = getWidgetParent();
if (widgetParent)
{
widgetParent->checkChildInfo(0,this,_touchStartPos);
}
pushDownEvent();
return !_touchPassedEnabled;
}
上面是按键检測的逻辑。
以下是改动过的代码。原理非常easy 在widget 里面ccnode节点 节点位置 相对父节点是0. 所以在visit的时候 位置就从0。0 開始了。
我们矫正下改渲染节点的位置。转成屏幕坐标 然后在依据touch 坐标获取当前点击像素的 透明值。
// yww get alpha touch event check
bool Button::AlphaTouchCheck(const CCPoint &point)
{
bool isTouchClaimed = false; if (getAlphaTouchEnable())
{
// check claimed touch arena
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCSprite* selectSprite = (CCSprite*)getVirtualRenderer();
CCPoint cutPos = selectSprite->getPosition();
// CCLOG("getAlphaTouchEnable selectSprite X %f, Y %f", cutPos.x, cutPos.y); // get screen point
CCPoint wordpx = selectSprite->getParent()->convertToWorldSpace(cutPos);
// CCLOG("getAlphaTouchEnable convertToWorldSpace X %f, Y %f", wordpx.x, wordpx.y); selectSprite->setPosition(wordpx); CCRenderTexture *renderer = CCRenderTexture::create(winSize.width, winSize.height);
//selectSprite->addChild(renderer); renderer->begin(); bool visible = selectSprite->isVisible();
if (visible)
{
selectSprite->visit();
}
else
{
selectSprite->setVisible(true);
selectSprite->visit();
selectSprite->setVisible(false);
} GLubyte pixelColors[4]; #if ( CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
glReadPixels(point.x, point.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixelColors[0]);
#else
glReadPixels(point.x, point.y, 1, 1, GL_ALPHA, GL_UNSIGNED_BYTE, &pixelColors[0]);
#endif int alpha = pixelColors[0];
CCLOG("----alpha %d", alpha); renderer->end(); selectSprite->setPosition(cutPos); if (alpha <= 20)
{
isTouchClaimed = false;
}
else
{
isTouchClaimed = true;
}
// check claimed touch arena
}
else
{
isTouchClaimed = true;
}
return isTouchClaimed;
}
上面逻辑是 重写了widget 的自己定义函数
AlphaTouchCheck
这个依据自己的需求构建结构了。
在lua里面能够提供检測开关 是否对透明纸进行检測咯。
不多往下说了。浪费网络内存咯。
Cocos2dx Widget button透明区域过滤的更多相关文章
- 解决Android中No resource found that matches android:TextAppearance.Material.Widget.Button.Inverse问题
解决Android中No resource found that matches android:TextAppearance.Material.Widget.Button.Inverse问题http ...
- 加载程序到android虚拟机报错: android.widget.RelativeLayout cannot be cast to android.widget.Button
05-23 02:53:48.416: E/Trace(875): error opening trace file: No such file or directory (2) 05-23 02:5 ...
- android:TextAppearance.Material.Widget.Button.Inverse找不到或者报错问题
前两天将android sdk升到android6.0后出现Error retrieving parent for Item - AppCompact-v7 23 或者无法解析 android:Tex ...
- android:TextAppearance.Material.Widget.Button.Inverse问题
如果在刚够构建Android Studio项目的时候,运行发现,出现没找到资源的错误!找不到com.android.support/appcompat-v7/23.0.1/res/values-v23 ...
- TextAppearance.Material.Widget.Button.Inverse,Widget.Material.Button.Colored
编译xamarin android项目报错: android:TextAppearance.Material.Widget.Button.Inverse android:Widget.Material ...
- lua 中处理cocos2dx 的button 事件
lua 中处理cocos2dx 的button 事件 2014-05-08 09:44:32| 分类: lua |举报 |字号 订阅 1.引入这个类:require "GuiConst ...
- java.lang.ClassCastException:android.widget.Button cannot be cast to android.widget.ImageView
今天遇到一个错误也不知道怎么回事,上网搜了一下: 出现的问题是:java.lang.ClassCastException:android.widget.Button cannot be cast to ...
- cocos2dx CCControlButton button大事
=================================.cpp文件 <pre name="code" class="cpp">bool ...
- cocos2dx --- Widget 载入中 CCNode
如果说. Widget 有addChild() 与 addNode() 两个方法. 如今我要载入一个粒子特效进去,下图: Widget* layout = dynamic_cast<Wid ...
随机推荐
- IDEA14 Ultimate Edition注册码
分享几个license: (1) key:IDEA value:61156-YRN2M-5MNCN-NZ8D2-7B4EW-U12L4 (2) key:huangweivalue:97493-G3A4 ...
- 项目管理及自动构建工具Maven
项目管理及自动构建工具Maven 一.Maven安装.目录结构.cmd命令1.下载安装apache-maven-3.2.3-bin.zip下载:http://maven.apache.org/down ...
- 华丽的bootstrap3碰到土鳖IE6
之前由于看好很容易上手的bootstrap,然后用这个框架写了个网站,对于不会美工和细致设计的攻城师来说,bootstrap是个界面设计的瑞士军刀,三下五除二就能搞定个不算太丑的页面. 吭哧吭哧工作了 ...
- python的元组和列表使用之一
Python的列表和元组 1. 概述 列表是用方括号[]包围的数据集合,不同的成员之间用逗号进行分隔,列表可以通过序号来进行访问其中的成员,可以对列表进行排序.添加.删除操作,改变列表中某 ...
- 新手须知设计的法则 Mark
经常看到一些讲如何学习设计的文章,坦白讲感觉有些千篇一律.且不痛不痒,都说要看点书.学点画.练软件.多观察……唉,练软件这事还要说么,难道你还需要告诉一个人学开发是需要学习编程语言的? 学习是基于过往 ...
- kali2 vmtools
root@kali:~# cat /etc/apt/sources.list# Regular Repositoriesdeb http://http.kali.org/kali sana main ...
- IIS启动出错解决方法
IIS出现server application error,最终解决办法2007年10月30日 星期二 20:38Server Application Error The server has enc ...
- Percona Xtrabackup备份mysql(转)
add by zhj:另外,参考了Xtrabackup之innobackupex备份恢复详解,我用的是Xtrabackup2.2.6版本, 可以成功备份和恢复指定的数据库. 原文:http://www ...
- cocos2dx使用了第三方库照样移植android平台-解决iconv库的移植问题
当我写这篇文章的时候我是怀着激动的心情的,因为我又解决了一个技术问题.你可能对题目还一知半解,这是什么意思,我之所以要写这篇文章就是要解决当我们在cocos2dx中使用了第三方库的时候,移植到andr ...
- [转]模拟HttpContext 实现ASP.NET MVC 的单元测试
众所周知 ASP.NET MVC 的一个显著优势即可以很方便的实现单元测试,但在我们测试过程中经常要用到HttpContext,而默认情况下单元测试框架是不提供HttpContext的模拟的,本文通过 ...