原文请猛戳:
http://galoisplusplus.coding....
cocos2d-x的按钮默认是以长方形作为点击区域的,实际使用时这确实很蛋疼。之前有大牛研究了如何获取图片的透明度实现不规则点击区域的方法,例如:
cocos2d-x开发笔记:获取Sprite上某一个点的透明度,制作不规则按钮
在 cocos2d-x 中获取纹理的像素值
可惜在cocos2d-x v3.0以上的版本,他们的方法都失效了,因此本渣也不得不自行琢磨这一块的实现。
之前方法的核心在于:图片每个像素点的RGBA信息被保存在Image类中,这个底层类并不会随着图片Node的创建而留在内存中(否则其内存占用量可想而知)。然而,在create出一个Sprite后,可以在内存中拿到它的Frame,再用RenderTextue重画一次拿到原始的Image,这样就可以判断图片上任意点的像素了。如果某点像素的Alpha值为0,则说明它是透明的,点击在该点是无效的。
这种方法是所以会失效,是因为cocos2d-x v3.0的RenderTexture是异步渲染的,导致拿到的Image并不是最后渲染的结果。本渣用了一种简单粗暴的方法,用Image类的initWithImagefile()方法去初始化Image对象,这样就确保了对象的数据是正确的。但是这种方法会有文件IO,不宜频繁调用,因此我只在一开始时创建一次,用一个bool数组保存每个像素点是否透明度为0的信息。每次触发点击事件时,就根据这个数组的值来判断点击是否有效。我把这部分逻辑封装到cocos2d::ui::button
的子类Irregularbutton
中,具体代码详见:
cocos2d-x-irregular-button
/* * ===================================================================================== * * filename: Irregularbutton.h * * Description: * * Version: 1.0 * Created: 2014/10/29 * Revision: none * Compiler: gcc * * Author: Shuai Yuan (),* Organization: * * ===================================================================================== */#ifndef __IRREGulAR_button_H__#define __IRREGulAR_button_H__ #include "cocos2d.h"#include "ui/CocosGUI.h"/* * ===================================================================================== * Class: Irregularbutton * Description: * ===================================================================================== */class Irregularbutton : public cocos2d::ui::button{ public: /* ==================== liFECYCLE ======================================= */ Irregularbutton (); /* constructor */ virtual ~Irregularbutton (); /* destructor */ static Irregularbutton* create(); static Irregularbutton* create(const std::string& normalimage,const std::string& selectedImage = "",const std::string& disableImage = "",cocos2d::ui::Widget::TextureResType texType = cocos2d::ui::Widget::TextureResType::LOCAL); /* ==================== ACCESSORS ======================================= */ /* ==================== MUTATORS ======================================= */ /* ==================== OPERATORS ======================================= */ virtual bool hitTest(const cocos2d::Vec2 &pt) overrIDe; protected: virtual bool init() overrIDe; virtual bool init(const std::string& normalimage,cocos2d::ui::Widget::TextureResType texType = cocos2d::ui::Widget::TextureResType::LOCAL) overrIDe; voID loadnormaltransparentInfo(); bool getIstransparentAtPoint(cocos2d::Vec2 point); /* ==================== DATA MEMBERS ======================================= */ private: /* ==================== DATA MEMBERS ======================================= */ int normalimageWIDth_; int normalimageHeight_; bool* normaltransparent_;}; /* ----- end of class Irregularbutton ----- */#endif /* __IRREGulAR_button_H__ */
/* * ===================================================================================== * * filename: Irregularbutton.cpp * * Description: * * Version: 1.0 * Created: 2014/10/30 * Revision: none * Compiler: gcc * * Author: Shuai Yuan (),* Organization: * * ===================================================================================== */#include "Irregularbutton.h"USING_NS_CC;using namespace ui;Irregularbutton::Irregularbutton(): button(),normaltransparent_(nullptr){}Irregularbutton::~Irregularbutton(){ delete[] normaltransparent_;}Irregularbutton* Irregularbutton::create(){ Irregularbutton* Widget = new Irregularbutton(); if (Widget && Widget->init()) { Widget->autorelease(); return Widget; } CC_SAFE_DELETE(Widget); return nullptr;}Irregularbutton* Irregularbutton::create(const std::string& normalimage,const std::string& selectedImage,const std::string& disableImage,TextureResType texType){ Irregularbutton* btn = new Irregularbutton; if (btn && btn->init(normalimage,selectedImage,disableImage,texType)) { btn->autorelease(); return btn; } CC_SAFE_DELETE(btn); return nullptr;}bool Irregularbutton::init(const std::string &normalimage,TextureResType texType){ bool ret = true; do { if (!button::init(normalimage,texType)) { ret = false; break; } } while (0); loadnormaltransparentInfo(); return ret;}bool Irregularbutton::init(){ if (button::init()) { return true; } return false;}voID Irregularbutton::loadnormaltransparentInfo(){ Image* normalimage = new Image(); normalimage->initWithImagefile(_normalfilename); normalimageWIDth_ = normalimage->getWIDth(); normalimageHeight_ = normalimage->getHeight(); auto dataLen = normalimage->getDataLen(); if (normaltransparent_ != nullptr) { delete[] normaltransparent_; } auto normalPixels = normalimage->getData(); normaltransparent_ = new bool[dataLen / (sizeof(unsigned char) * 4)]; for (auto i = 0; i < normalimageHeight_; i++) { for (auto j = 0; j < normalimageWIDth_; j++) { normaltransparent_[i * normalimageWIDth_ + j] = (normalPixels[(i * normalimageWIDth_ + j) * 4 + 3] == 0); } } delete normalimage;}bool Irregularbutton::getIstransparentAtPoint(cocos2d::Vec2 point){ point.y = _buttonnormalRenderer->getContentSize().height - point.y; int x = (int) point.x - 1; if (x < 0) { x = 0; } else if (x >= normalimageWIDth_) { x = normalimageWIDth_ - 1; } int y = (int) point.y - 1; if (y < 0) { y = 0; } else if (y >= normalimageHeight_) { y = normalimageHeight_ - 1; } return normaltransparent_[normalimageWIDth_ * y + x];}bool Irregularbutton::hitTest(const Vec2 &pt){ Vec2 localLocation = _buttonnormalRenderer->convertToNodeSpace(pt); Rect valIDtouchedRect; valIDtouchedRect.size = _buttonnormalRenderer->getContentSize(); if (valIDtouchedRect.containsPoint(localLocation) && getIstransparentAtPoint(localLocation) == false) { return true; } return false;}总结
以上是内存溢出为你收集整理的cocos2d-x V3.x不规则按钮全部内容,希望文章能够帮你解决cocos2d-x V3.x不规则按钮所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)