cocos2dx实现橡皮擦效果以及判断是否擦除完毕

本文实例为大家分享了cocos2dx实现橡皮擦效果,以及判断是否擦除完毕,供大家参考,具体内容如下

首先修改HelloWorld.h文件

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC_EXT;
USING_NS_CC;
class HelloWorld : public cocos2d::Layer
{
public:
 // there's no 'id' in cpp, so we recommend returning the class instance pointer
 static cocos2d::Scene* createScene();

 // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
 virtual bool init();

 // a selector callback
 void menuCloseCallback(cocos2d::Ref* pSender);

 // implement the "static create()" method manually
 CREATE_FUNC(HelloWorld);
 void myUpdate(float dt);//不断判断是否全部擦除
 void onTouchesMoved(const std::vector<Touch*>& touches, Event* event);
 bool myIsDataClear(RenderTexture *pRenderTexture);//是否完全擦除
 bool myIsDataClearInRect(RenderTexture *pRenderTexture,int x,int y,int width ,int height);//某个区域是否完全擦除
 Sprite *sprFore;
 RenderTexture *renderTexture;
 Vector<Sprite*> _brushs;
};

#endif // __HELLOWORLD_SCENE_H__

然后修改HelloWorld.cpp文件

#include "HelloWorldScene.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
 // 'scene' is an autorelease object
 auto scene = Scene::create();

 // 'layer' is an autorelease object
 auto layer = HelloWorld::create();

 // add layer as a child to scene
 scene->addChild(layer);

 // return the scene
 return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{

 if ( !Layer::init() )
 {
  return false;
 }

 Size visibleSize = Director::getInstance()->getVisibleSize();
 Vec2 origin = Director::getInstance()->getVisibleOrigin();

 auto closeItem = MenuItemImage::create(
           "CloseNormal.png",
           "CloseSelected.png",
           CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

 closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
        origin.y + closeItem->getContentSize().height/2));

 auto menu = Menu::create(closeItem, NULL);
 menu->setPosition(Vec2::ZERO);
 this->addChild(menu, 1);

 auto label = Label::createWithTTF("Test Eraser", "fonts/Marker Felt.ttf", 24);

 label->setPosition(Vec2(origin.x + visibleSize.width/2,
       origin.y + visibleSize.height - label->getContentSize().height));

 this->addChild(label, 1);

 sprFore = Sprite::create("HelloWorld.png");
 sprFore->setPosition(Vec2(visibleSize / 2) + origin);
 sprFore->retain();
 renderTexture = RenderTexture::create(visibleSize.width, visibleSize.height, Texture2D::PixelFormat::RGBA8888);
 renderTexture->setContentSize(visibleSize);
 renderTexture->retain();
 this->addChild(renderTexture);
 renderTexture->setPosition(Vec2(visibleSize / 2) + origin);

 renderTexture->beginWithClear(0, 0, 0, 0);
 sprFore->visit();
 renderTexture->end();

 auto listener = EventListenerTouchAllAtOnce::create();
 listener->onTouchesMoved = CC_CALLBACK_2(HelloWorld::onTouchesMoved, this);
 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

 //不断判断是否擦除完毕
 schedule(schedule_selector(HelloWorld::myUpdate), 0.5f);
 return true;
}

void HelloWorld::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
 MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
 return;
#endif

 Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
 exit(0);
#endif
}

void HelloWorld::myUpdate(float dt)
{
 if (myIsDataClear(renderTexture) == true)
 {

 log("image is clear !");
 }

 if (myIsDataClearInRect(renderTexture,300,200,50,50) == true)
 {

 log("image in rect is clear !");
 }
}

void HelloWorld::onTouchesMoved(const std::vector<Touch*>& touches, Event* event)
{
 auto touch = touches[0];
 auto start = touch->getLocation();
 auto end = touch->getPreviousLocation();

 // begin drawing to the render texture
 renderTexture->begin();

 // for extra points, we'll draw this smoothly from the last position and vary the sprite's
 // scale/rotation/offset
 float distance = start.getDistance(end);
 if (distance > 1)
 {
 int d = (int)distance;
 _brushs.clear();
 for (int i = 0; i < d; ++i)
 {
 //橡皮擦
 auto sprite = CCSprite::create("red.png");//主要根据图片定义橡皮擦的形状
 BlendFunc blendFunc;
 blendFunc.src = GL_ZERO;
 blendFunc.dst = GL_ONE_MINUS_SRC_ALPHA;
 sprite->setBlendFunc(blendFunc);
 sprite->setScale(1.8f);
 renderTexture->addChild(sprite);
 _brushs.pushBack(sprite);
 }
 for (int i = 0; i < d; i++)
 {
 float difx = end.x - start.x;
 float dify = end.y - start.y;
 float delta = (float)i / distance;
 _brushs.at(i)->setPosition(Vec2(start.x + (difx * delta), start.y + (dify * delta)));
 _brushs.at(i)->visit();
 }
 }

 // finish drawing and return context back to the screen
 renderTexture->end();

}

bool HelloWorld::myIsDataClear(RenderTexture *pRenderTexture)
{
 bool m_bEraserOk = false;

 Image* image = new Image();
 image = pRenderTexture->newImage(true);

 int m = 3;
 if (image->hasAlpha())
 {
 m = 4;
 }

 unsigned char *data_ = image->getData();

 int x = 0, y = 0;
 /// 这里要提醒一点,即Opengl下,其中心点坐标在左上角
 for (x = 0; x < pRenderTexture->getContentSize().width; ++x)
 {
 for (y = 0; y < pRenderTexture->getContentSize().height; ++y)
 {
 //获取每个点的像素点值
 unsigned char *pixel = data_ + (x + y * image->getWidth()) * m;

 // You can see/change pixels' RGBA value(0-255) here !
 unsigned int r = (unsigned int)*pixel;
 unsigned int g = (unsigned int)*(pixel + 1);
 unsigned int b = (unsigned int)*(pixel + 2);
 unsigned int a = (unsigned int)*(pixel + 3);

 if (r != 0 && g != 0 && b != 0 && a != 0)
 {
 m_bEraserOk = false;
 break;
 }
 }
 //如果改列 有一个点的像素点值不为零 跳出
 if (pRenderTexture->getContentSize().height != y)
 {
 break;
 }
 }

 //如果所有点的像素点值都为0 则擦除完毕
 if (x == pRenderTexture->getContentSize().width && y == pRenderTexture->getContentSize().height)
 {
 m_bEraserOk = true;
 }

 delete image;

 return m_bEraserOk;
}

bool HelloWorld::myIsDataClearInRect(RenderTexture *pRenderTexture, int x, int y, int width, int height)
{

 bool m_bEraserOk = false;

 Image* image = new Image();
 image = pRenderTexture->newImage(true);

 int m = 3;
 if (image->hasAlpha())
 {
 m = 4;
 }

 int i = 0, j = 0;
 unsigned char* mdata = (unsigned char*)image->getData();
 for (i = 0; i < width; ++i)
 {
 for (j = 0; j < height; ++j)
 {

 unsigned char *pixel = mdata + (i + x + (image->getHeight() - y - (height - j)) * image->getWidth()) * m;

 // You can see/change pixels' RGBA value(0-255) here !
 unsigned int r = (unsigned int)*pixel;
 unsigned int g = (unsigned int)*(pixel + 1);
 unsigned int b = (unsigned int)*(pixel + 2);
 unsigned int a = (unsigned int)*(pixel + 3);

 if (r != 0 && g != 0 && b != 0 && a != 0)
 {
 break;
 }

 }

 if (height != j)
 {
 break;
 }
 }

 if (i == width && j == height)
 {
 m_bEraserOk = true;
 }
 return m_bEraserOk;
}

看下运行效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • cocos2dx+lua实现橡皮擦功能

    游戏中刮刮乐是怎么实现的?做了一个小例子看了一下. 实现原理:随着触摸点的移动,通过setBlendFunc函数设置部分区域的颜色混合(将上层图片透明度为0,底层我们想要的图片就显示出来) --橡皮擦功能测试 local function initInfo() local scene = CCScene:create() local layer = CCLayer:create() scene:addChild(layer) --擦除后要显示的图片 local tupian = CCSprite

  • cocos2dx实现橡皮擦效果以及判断是否擦除完毕

    本文实例为大家分享了cocos2dx实现橡皮擦效果,以及判断是否擦除完毕,供大家参考,具体内容如下 首先修改HelloWorld.h文件 #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" #include "cocos-ext.h" USING_NS_CC_EXT; USING_NS_CC; class HelloWorld : public coco

  • 基于canvas剪辑区域功能实现橡皮擦效果

    效果如图 这是基础结构 没什么好说的 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-C

  • Android自定义橡皮擦效果

    本文实例为大家分享了Android自定义橡皮擦效果,使用贝塞尔曲线处理曲线转折处 public class picFingerToTest extends View { private Paint paint; private Bitmap decodeResourceSRC; private Bitmap createBitmapDST; // 手指路径,使用贝塞尔路线 private Path path; private float perX; private float perY; pub

  • js canvas实现橡皮擦效果

    本文实例为大家分享了canvas实现橡皮擦效果的具体代码,供大家参考,具体内容如下 html部分 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html" charset="utf-8" /> <meta name="viewport" content="in

  • 判断是否输入完毕再激活提交按钮

    从才子的BLOG上转来的: window.attachEvent("onload",function (){AutoSizeDIV('CODE_7643')})  以下是程序代码: <script language="javascript">function check(){    s.disabled=(t1.value==""||t2.value==""||t3.value=="")}<

  • cocos2dx实现刮奖效果

    本文实例为大家分享了cocos2dx刮奖效果实现代码,供大家参考,具体内容如下 刮奖效果其实挺简单的,需要用到RenderTexture来进行渲染,通过你所要渲染的图层,把该层的颜色进行设置混合就可以达到效果,具体看代码,我用的lua实现的. local winsize = cc.Director:sharedDirector():getWinSize(); local dataSprite = cc.Sprite:create("Star.png")--要把这个图片刮出来 dataS

  • 原生js实现返回顶部缓冲效果

    运行原理 通过定时器30毫秒执行一次滚动条上升,每次上升的高度为当前高度的80%,这样就达到了上升缓冲的动画效果. 判断当滚动条高度超过一屏时,按钮显示,默认隐藏 知识要点 scrollTop//获取滚动条高度 需要写兼容 clientHeight//可视窗口高度 需要写兼容 setInterval//定时器 window.onscroll//滚动触发事件 完整代码 <!DOCTYPE html> <html lang="en"> <head> &l

  • Android实现点击Button产生水波纹效果

    先上图,看看接下来我要向大家介绍的是个什么东西,如下图: 接下来要介绍的就是如何实现上述图中的波纹效果,这种效果如果大家没有体验过的话,可以看看百度手机卫士或者360手机卫士,里面的按钮点击效果都是这样的,另外Android 5.0以上的版本也出现了这种效果.不多说,下面聊聊具体的怎么实现. 首先大家看到的是三个button,水波纹的出现给我们的错觉是直接将波纹绘制在button上面的,但是这样能做到吗?首先button自己有background和src,如果把半透明的水波纹当作backgrou

  • JavaScript 的方法重载效果

    在 JavaScript 的方法内,有个叫做 arguments 的变量数组,它是只读的,所有实际传入的参数变量都 放在了里面,通过它,我们可以对传入的参数进行类型检查,从而实现重载的效果. 判断一个变量的类型有两种方法. 1,用 typeof 语句: 复制代码 代码如下: function check(){ if(typeof arguments[0] == 'string') alert('你传入的参数是个字符串'); else if(typeof arguments[0] == 'numb

  • 判断是否是IE6版本并给出提示升级浏览器

    下面的效果,只能在IE6下才能看到效果. 判断IE版本并给出提示升级浏览器 #ie6-warning{ background:rgb(255,255,225) url("/upload/201006/20100628012515690.gif") no-repeat scroll 3px center; position:absolute; top:0; left:0; font-size:12px; color:#333; width:97%; padding: 2px 15px 2

随机推荐