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.