经历几次修改,总算实现了感觉还行的效果。下面是我的实现代码,高手勿喷!!!!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 总结
以上是内存溢出为你收集整理的cocos2d-x-3.16实现橡皮擦除遮挡层(类似刮刮乐效果)全部内容,希望文章能够帮你解决cocos2d-x-3.16实现橡皮擦除遮挡层(类似刮刮乐效果)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)