cocos2d-x-3.16实现橡皮擦除遮挡层(类似刮刮乐效果)(转)

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,0,0,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)
{

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值