I want to trace sprites with fingers and erase them

I looked for various things. But I can not. Help me.

The problem is two.
1, The sprite becomes black.
2, I can not erase on the sprite. I can erase If I rub on the lower left.

This code

HelloWorldScene.h

#ifndef HELLOWORLD_SCENE_H
#define HELLOWORLD_SCENE_H

#include “cocos2d.h”

class HelloWorld : public cocos2d::Scene
{
public:
static cocos2d::Scene* createScene();

virtual bool init();

CREATE_FUNC(HelloWorld);

cocos2d::Sprite *sprite_2;
cocos2d::DrawNode *dn;
cocos2d::RenderTexture *rt;

cocos2d::EventListenerTouchOneByOne *listener;
bool onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event);
void onTouchMoved(cocos2d::Touch *touch, cocos2d::Event *event);

void updateRT();

};

#endif // HELLOWORLD_SCENE_H

HelloWorldScene.cpp

#include “HelloWorldScene.h”
#include “SimpleAudioEngine.h”

USING_NS_CC;

Scene* HelloWorld::createScene()
{
return HelloWorld::create();
}

// on “init” you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Scene::init() )
{
return false;
}

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

// wallPaper
auto bg = LayerColor::create(Color4B::WHITE, visibleSize.width, visibleSize.height);
this->addChild(bg, 0);

// star sprite
auto sprite_1 = Sprite::create("zz.png");
sprite_1->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
this->addChild(sprite_1, 1);

// gray square sprite
sprite_2 = Sprite::create("z.png");
sprite_2->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
this->addChild(sprite_2, 2);


dn = DrawNode::create();
dn->retain();


auto spr = sprite_2->getContentSize();
rt = RenderTexture::create(spr.width, spr.height);
rt->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
this->addChild(rt, 2);


BlendFunc bf;


bf.dst = GL_ONE_MINUS_SRC_ALPHA;
bf.src = GL_ZERO;
dn->setBlendFunc(bf);


bf.src = GL_ONE_MINUS_DST_ALPHA;
bf.dst = GL_ONE;
sprite_2->setBlendFunc(bf);


listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);


updateRT();

return true;

}

bool HelloWorld::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event)
{
Vec2 p = touch->getLocation();
dn->drawDot(p, 20, Color4F(1.0f, 1.0f, 1.0f, 1.0f));
this->updateRT();

return true;

}

void HelloWorld::onTouchMoved(cocos2d::Touch *touch, cocos2d::Event *event)
{
Vec2 p = touch->getLocation();
dn->drawDot(p, 20, Color4F(1.0f, 1.0f, 1.0f, 1.0f));
this->updateRT();
}

void HelloWorld::updateRT()
{
rt->beginWithClear(0.0f, 0.0f, 0.0f, 1.0f);
dn->visit();
sprite_2->visit();
rt->end();
}

Help me.
Thanks.

Looks to me as if you need to adjust your touch point by the location of your sprite - i.e. subtract the sprite location from the touch location before drawing.

1 Like

In Your OnTuchBegan and OnTouchMove

try changing the line to
Vec2 p = sprite_2->convertToNodeSpace((touch->getLocation()); // considering sprite_2 is the sprite instance on which you are trying to draw.

1 Like

I wanted to do something. Thank you.

The second problem was solved. Thank you everyone.

I have another problem.

auto spr = sprite_2->getContentSize();
rt = RenderTexture::create(spr.width, spr.height);
rt->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
this->addChild(rt, 2);

// Add
rt->beginWithClear(0.5f, 0.5f, 0.5f, 1.0f); // clash run ;(

The color of the sprite will turn black. please tell me.

Sorry, explain more, show us the Sprite you are using, what Cocos2d-x version?

All codes are at the beginning.

cocos2dx ver.3.15

I am in trouble because the image has turned black.

Show is the Sprite you are using, z.png helps people just drop in your image and code to test.

This z.png

z

oh, I see. That is the Sprite you are using. I read your post as you were using RenderTexture on a Sprite and getting just black as the result.

rt->saveToFile(“z.png”);

I added it like this, but the sprite is not reflected ;(