加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2dx 3.x widget 透明过滤 UIbutton实现

发布时间:2020-12-14 21:31:57 所属栏目:百科 来源:网络整理
导读:参考了很多资料,理都懂,然并卵啊! 第一个想到的是用 RenderTexture来实现判断点击的像素点的alpha值,参考的资料http://blog.csdn.net/lwuit/article/details/40658347 发现不成功,貌似渲染机制改了有点蛋疼。 第二个参考的就是http://blog.csdn.net/sup

参考了很多资料,理都懂,然并卵啊!

第一个想到的是用RenderTexture来实现判断点击的像素点的alpha值,参考的资料http://blog.csdn.net/lwuit/article/details/40658347 发现不成功,貌似渲染机制改了有点蛋疼。


第二个参考的就是http://blog.csdn.net/super_level/article/details/41708563的方法,然后在修改了下成功了,上代码。


首先是在widget中添加:

	virtual bool AlphaTouchCheck(const Vec2 &point);

	virtual bool getAlphaTouchEnable();

	virtual void setAlphaTouchEnable(bool isAlphaTouch);
bool Widget::AlphaTouchCheck(const Vec2 &point)
{
	return true;
}

bool Widget::getAlphaTouchEnable()
{
	return _isAlphaTouchEnable;
}

void Widget::setAlphaTouchEnable(bool isAlphaTouch)
{
	_isAlphaTouchEnable = isAlphaTouch;
}
widget中的判断先默认返回true

把开启的开关放在widget中,方便以后layout 和 imageview的不规则点击。开关默认关闭

接下来就是uibutton中重载AlphaTouchCheck 函数

bool Button::AlphaTouchCheck(const Vec2& point)
{
	if (getAlphaTouchEnable())
	{
		Image* normalImage = new Image();
		normalImage->initWithImageFile(_normalImageString);
		auto data = normalImage->getData();
		if (data == NULL)
		{
			return true;
		}
		auto locationInNode = this->convertToNodeSpace(point);
        //图片的0,0 是左上角 所以要和触摸点的Y转换一下 也就是“(normalImage->getHeight() - (int)(locationInNode.y) - 1)” 
		//该data值是把二维数组展开成一个一维数组,因为每个像素值由RGBA组成,所以每隔4个char为一个RGBA,并且像素以横向排列
		int pa = 4 * ((normalImage->getHeight() - (int)(locationInNode.y) - 1) * normalImage->getWidth() + (int)(locationInNode.x)) + 3;
		unsigned int ap = data[pa];
		if (ap < 20)
		{
			CC_SAFE_DELETE(normalImage);
			return false;
		}
		else
		{
			CC_SAFE_DELETE(normalImage);
			return true;
		}
	}
	return true;
}
_normalImageString 是记录创建时候的图片名。

然后就ok了。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读