#ifndef __ERASABLELAYER_H__#define __ERASABLELAYER_H__#include "cocos2d.h"USING_NS_CC;// http://blog.csdn.net/linchaolong// 可擦除的Layerclass ErasableLayer :public Layer{public: // 创建ErasableLayer //1.遮罩层 //2.橡皮擦(可以是Sprite或者纹理的路径) static ErasableLayer* create(const char* layerPath,const char* erasaPath); static ErasableLayer* create(Node *layer,Sprite* erasa); static ErasableLayer* create(Node *layer,const char* erasaPath); static ErasableLayer* create(const char* layerPath,Sprite* erasa); // 还原 voID clear(); // 是否可擦除 voID seterasable(bool); bool ontouchBegan(touch* touch,Event *event); voID ontouchesMoved(touch* touch,Event *event); voID ontouchended(touch* touch,Event *event);protected: ErasableLayer(); ~ErasableLayer(); bool init(Node *layer,Sprite* erasa); voID tick(float);private: Node* layer_; Sprite* erasa_; bool isErasable_; RenderTexture* rt_;};#endif
#include "ErasableLayer.h"ErasableLayer::ErasableLayer() :isErasable_(true){}ErasableLayer::~ErasableLayer(){ CC_SAFE_RELEASE(layer_); CC_SAFE_RELEASE(erasa_);} ErasableLayer* ErasableLayer::create(const char* layerPath,const char* erasaPath){ CCSprite* layer = CCSprite::create(layerPath); CCSprite* erasa = CCSprite::create(erasaPath); ErasableLayer* ret = new ErasableLayer; if (ret->init(layer,erasa)) { ret->autorelease(); return ret; } delete ret; return nullptr;}ErasableLayer* ErasableLayer::create(Node* layer,Sprite* erasa){ ErasableLayer* ret = new ErasableLayer; if (ret->init(layer,const char* erasaPath){ CCSprite* erasa = CCSprite::create(erasaPath); ErasableLayer* ret = new ErasableLayer; if (ret->init(layer,erasa)) { ret->autorelease(); return ret; } delete ret; return nullptr;}ErasableLayer* ErasableLayer::create(const char* layerPath,Sprite* erasa){ CCSprite* layer = CCSprite::create(layerPath); ErasableLayer* ret = new ErasableLayer; if (ret->init(layer,erasa)) { ret->autorelease(); return ret; } delete ret; return nullptr;}bool ErasableLayer::init(Node *layer,Sprite* erasa){ if (!Layer::init()) { return false; } layer_ = layer; erasa_ = erasa; layer_->retain(); erasa_->retain(); this->setContentSize(layer_->getContentSize()); // 设置颜色混合模式 BlendFunc erasaBf = { GL_ZERO,GL_ONE_MINUS_SRC_Alpha }; //源因子:值为0,橡皮擦颜色为透明;目标因子:目标颜色透明度减去源颜色的透明度 erasa_->setBlendFunc(erasaBf); auto size = layer_->getContentSize(); rt_ = RenderTexture::create(size.wIDth,size.height); rt_->setAnchorPoint(Vec2(0,0)); rt_->setposition(Vec2(size.wIDth/2,size.height/2)); this->addChild(rt_); clear(); schedule(CC_SCHEDulE_SELECTOR(ErasableLayer::tick)); // 触摸事件 auto Listener = EventListenertouchOneByOne::create(); Listener->ontouchBegan = CC_CALLBACK_2(ErasableLayer::ontouchBegan,this); Listener->ontouchmoved = CC_CALLBACK_2(ErasableLayer::ontouchesMoved,this); Listener->ontouchended = CC_CALLBACK_2(ErasableLayer::ontouchended,this); _eventdispatcher->addEventListenerWithSceneGraPHPriority(Listener,this); Listener->setSwallowtouches(false); return true;}voID ErasableLayer::tick(float){ if (!isErasable_) { return; } // 设置源颜色Alpha值为最大值,目标颜色Alpha值减去源颜色Alpha值后就为0了,混合出来的效果就变透明了,这样就实现了橡皮擦效果了。 erasa_->setopacity(255); // 更新RenderTexture rt_->begin(); // 绘制 erasa_->visit(); rt_->end();}voID ErasableLayer::clear(){ layer_->setAnchorPoint(Vec2(0,0)); layer_->setposition(Vec2(0,0)); rt_->begin(); layer_->visit(); rt_->end();}voID ErasableLayer::seterasable(bool flag){ isErasable_ = flag;}bool ErasableLayer::ontouchBegan(touch* touch,Event *event){ erasa_->setposition(this->convertToNodeSpace(touch->getLocation())); return true;}voID ErasableLayer::ontouchesMoved(touch* touch,Event *event){ erasa_->setposition(this->convertToNodeSpace(touch->getLocation()));}voID ErasableLayer::ontouchended(touch* touch,Event *event){ erasa_->setposition(this->convertToNodeSpace(touch->getLocation()));}总结
以上是内存溢出为你收集整理的Cocos2dx_可擦除Layer全部内容,希望文章能够帮你解决Cocos2dx_可擦除Layer所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)