经历几次修改,总算实现了感觉还行的效果。下面是我的实现代码,高手勿喷!!!!
1. Erasure.hpp
//
// Tailor.hpp
// Hello-mobile
#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;
// DrawNode *m_pDotNode;
RenderTexture *m_pRenderTexture;
};
#endif /* Tailor_hpp */
3.Erasure.cpp
//
// Tailor.cpp
// Hello-mobile
#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));
// addChild(m_pImage); //注意这里不要把遮挡精灵加入父节点,当时在这个地方卡了很久,
//实现不了效果
}
void Erasure::initializeDrawNode()
{
Size size = Director::getInstance()->getWinSize();
//m_pDotNode = DrawNode::create();
//m_pDotNode->retain();
//m_pDotNode->drawDot(Point(0,0),3.0f,Color4F(255,255));
m_pRenderTexture = RenderTexture::create(size.width,size.height);
m_pRenderTexture->setPosition(Vec2(size.width/2,size.height/2));
this->addChild(m_pRenderTexture);
//通过RenderTexture将m_pImage渲染到节点上
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;
// m_pDotNode->setBlendFunc(blendFunc);
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));
// m_pDotNode->setPosition(pos);
// m_pDotNode->visit();
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)
{
}
参考文章:
1.http://www.mamicode.com/info-detail-193798.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。