前端之家收集整理的这篇文章主要介绍了
cocos2d-x-3.16实现橡皮擦除遮挡层(类似刮刮乐效果),
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
经历几次修改,总算实现了感觉还行的效果。下面是我的实现代码,高手勿喷!!!!
1. Erasure.hpp
#ifndef Erasure_hpp
#define Erasure_hpp
#include <stdio.h>
#include <cocos2d.h>
USING_NS_CC;
class Erasure : public Layer
{
public:
Erasure();
~Erasure();
bool init();
static Scene *scene();
virtual bool onTouchBegan(Touch *touch,Event *unused_event);
virtual void onTouchMoved(Touch *touch,Event *unused_event);
virtual void onTouchEnded(Touch *touch,Event *unused_event);
virtual void onTouchCancelled(Touch *touch,Event *unused_event);
CREATE_FUNC(Erasure);
public:
void initializeElement();
void initializeDrawNode();
private:
Sprite *m_pBottom;
Sprite *m_pImage;
RenderTexture *m_pRenderTexture;
};
#endif /* Tailor_hpp */
3.Erasure.cpp
#include "Erasure.hpp"
Erasure::Erasure()
{
m_pImage = NULL;
m_pBottom = NULL;
}
Erasure::~Erasure()
{
m_pImage = NULL;
m_pBottom = NULL;
}
Scene *Erasure::scene()
{
Erasure *pLayer = Erasure::create();
Scene *scene = Scene::create();
scene->addChild(pLayer);
return scene;
}
bool Erasure::init()
{
if (!Layer::init())
{
return false;
}
initializeElement();
initializeDrawNode();
EventListenerTouchOneByOne *listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2(Erasure::onTouchBegan,this);
listener->onTouchMoved = CC_CALLBACK_2(Erasure::onTouchMoved,this);
listener->onTouchEnded = CC_CALLBACK_2(Erasure::onTouchEnded,this);
listener->onTouchCancelled = CC_CALLBACK_2(Erasure::onTouchCancelled,this);
_eventDispatcher->addEventListenerWithSceneGraPHPriority(listener,this);
return true;
}
void Erasure::initializeElement()
{
Size winSize = Director::getInstance()->getWinSize();
m_pBottom = Sprite::create("white.jpeg");
m_pBottom->setPosition(Vec2(winSize.width/2,winSize.height/2));
m_pBottom->setScale(5.0f);
addChild(m_pBottom,0);
m_pImage = Sprite::create("image.jpg");
m_pImage->setScale(2.0f);
m_pImage->setAnchorPoint(Vec2(0.5f,0.5f));
m_pImage->setPosition(Vec2(winSize.width/2,winSize.height/2));
}
void Erasure::initializeDrawNode()
{
Size size = Director::getInstance()->getWinSize();
m_pRenderTexture = RenderTexture::create(size.width,size.height);
m_pRenderTexture->setPosition(Vec2(size.width/2,size.height/2));
this->addChild(m_pRenderTexture);
m_pRenderTexture->begin();
m_pImage->visit();
m_pRenderTexture->end();
}
bool Erasure::onTouchBegan(cocos2d::Touch *touch,cocos2d::Event *unused_event)
{
return true;
}
void Erasure::onTouchMoved(cocos2d::Touch *touch,cocos2d::Event *unused_event)
{
Point pt = touch->getLocation();
float distance = pt.distance(m_ptDragStart);
if (m_pRenderTexture && m_pRenderTexture->isVisible())
{
if (distance > 1)
{
m_pRenderTexture->begin();
int d = (int)distance;
for (int i = 0; i < d; i++)
{
blendFunc.src = GL_ZERO;
BlendFunc blendFunc;
blendFunc.src = GL_ZERO;
blendFunc.dst = GL_ONE_MINUS_DST_ALPHA;
Sprite *brush = Sprite::create("brush.png");
brush->setBlendFunc(blendFunc);
float difx = pt.x - m_ptDragStart.x;
float dify = pt.y - m_ptDragStart.y;
float delta = (float)i / distance;
Point pos = Point(m_ptDragStart.x + (difx * delta),m_ptDragStart.y + (dify * delta));
brush->setPosition(pos);
brush->visit();
}
m_pRenderTexture->end();
}
}
m_ptDragStart = touch->getLocation();
}
void Erasure::onTouchEnded(cocos2d::Touch *touch,cocos2d::Event *unused_event)
{
}
void Erasure::onTouchCancelled(cocos2d::Touch *touch,cocos2d::Event *unused_event)
{
}
原文链接:https://www.f2er.com/cocos2dx/338225.html