利用ClippingNode来实现橡皮擦功能

利用ClippingNode来实现橡皮擦功能,第1张

概述我这里没有封装主要讲一下原理 通过这种方式不能完全实现自定义图片涂擦功能 我先上一段代码 SpriteBatchNode* spriteTexture = SpriteBatchNode::create("123.png");//橡皮擦图片 addChild(spriteTexture); auto node = Sprite::create("HelloWorld.png");//创建一个需

我这里没有封装主要讲一下原理通过这种方式不能完全实现自定义图片涂擦功能

我先上一段代码

SpriteBatchNode* spriteTexture = SpriteBatchNode::create("123.png");//橡皮擦图片	addChild(spriteTexture);	auto node = Sprite::create("HelloWorld.png");//创建一个需要剪裁的节点可以是精灵也可是层	node->setposition(visibleSize.wIDth/2,visibleSize.height/2);	auto Cliper = ClipPingNode::create();//创建剪裁区域 把需要剪裁的NODe都可以添加进来	Cliper->addChild(node);//添加NODE	auto EraserList = Node::create();//创建 一个NODE是用来剪裁 通过不停的添加精灵来实现橡皮擦的效果	Cliper->setStencil(EraserList);//设置剪裁区域的剪刀是EraserList (可以这样理解吧)	Cliper->setInverted(true);//切换 显示被减区域还是反向	this->addChild(Cliper);//添加cliper到该层	/*创建监听事件*/	auto linstener = EventListenertouchOneByOne::create();	linstener->ontouchBegan = [this,Cliper,EraserList,spriteTexture](touch *touch,Event *event)	{			return true;			};	linstener->ontouchmoved = [this,Event *event)	{		auto erasersprite = Sprite::createWithTexture(spriteTexture->getTexture());		erasersprite->setposition(touch->getLocation());		EraserList->addChild(erasersprite);	};	linstener->ontouchended = [this,Event *event)	{		auto erasersprite = Sprite::createWithTexture(spriteTexture->getTexture());		erasersprite->setposition(touch->getLocation());		EraserList->addChild(erasersprite);//通过不停添加精灵实现橡皮擦效果	};	_eventdispatcher->addEventListenerWithSceneGraPHPriority(linstener,this);

好我们来看下效果我的123.png是这张图

效果图:

大家会发现我123.png是圆的为什么橡皮擦还是方的因为创建的时候就是一张2D纹理拥有长宽(像素);当然如果你想要圆形也是可以的

通过drawpolygon函数画一个圆然后添加到EraserList当中就能实现这里就不多说了代码注释也很清楚 ;

哪要怎么实现真正的自定义图片擦除呢在下一篇我会写出我想的到方法实现

总结

以上是内存溢出为你收集整理的利用ClippingNode来实现橡皮擦功能全部内容,希望文章能够帮你解决利用ClippingNode来实现橡皮擦功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1013068.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-22
下一篇2022-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存