How to use DrawNode with RenderTexture cocos2d-x

2.3k views Asked by At

I cannot draw a simple line or a dot with DrawNode and RenderTexture.

This is how to I implemented:

AppDelegate.cpp

auto scene = Scene::create();
auto layer = BackgroundLayer::create();
scene->addChild(layer);

// run
director->runWithScene(scene);

BackgroundLayer.cpp

bool BackgroundLayer::init()
{
    if ( !LayerColor::initWithColor(Color4B::WHITE) )
    {
        return false;
    }
    auto renderTexture = RenderTexture::create(300, 200);
    renderTexture->beginWithClear(0, 0, 0, 1); // black
    auto drawPrimitive = DrawNode::create();
    drawPrimitive->retain();
    drawPrimitive->drawDot(Point(250, 250), 20, Color4F::RED);
    drawPrimitive->visit();
    renderTexture->end();
    renderTexture->retain();
    renderTexture->setPosition(this->getContentSize()/2);
    this->addChild(renderTexture, 10000);
}

I tried with some complex shape with DrawNode, but the result is just a black rectangle stayed in the center of the screen.

I compile with cocos2d-x v3.6 & VS2013.

1

There are 1 answers

0
shunmugam ip On
auto renderTexture = RenderTexture::create(50, 50);
renderTexture->beginWithClear(0, 0, 0, 1); // black
auto drawPrimitive = DrawNode::create();
drawPrimitive->retain();
drawPrimitive->drawDot(Point(10, 10), 2, Color4F::RED);
drawPrimitive->visit();
renderTexture->end();
renderTexture->retain();
renderTexture->setPosition(SCREENSIZE.width/2,SCREENSIZE.height/2);
this->addChild(renderTexture, 10000);

Try this it will create red dot on the texture