Cocos2d x 如何使用CCRenderTexture创建背景?

Cocos2d x 如何使用CCRenderTexture创建背景?,cocos2d-x,Cocos2d X,我正在尝试使用CCRenderTexture绘制一个简单的背景 我创建了一个CCRenderTexture指针(使用宽度、ht、像素格式初始化) 用一些颜色把它擦干净 添加到节点 向节点添加了标签 ========================================================================================== 当我运行它时,我看到的只是黑屏,上面有hello world的标签 那么,纹理在哪里 bool HelloWorld::

我正在尝试使用CCRenderTexture绘制一个简单的背景

我创建了一个CCRenderTexture指针(使用宽度、ht、像素格式初始化)

用一些颜色把它擦干净

添加到节点

向节点添加了标签

========================================================================================== 当我运行它时,我看到的只是黑屏,上面有hello world的标签

那么,纹理在哪里

bool HelloWorld::init()
{
    bool bRet = false;
    do 
    {
    //////////////////////////////////////////////////////////////////////////
    // super init first
    //////////////////////////////////////////////////////////////////////////

    CC_BREAK_IF(! CCLayer::init());

    //////////////////////////////////////////////////////////////////////////
    // add your codes below...
    //////////////////////////////////////////////////////////////////////////

    // 2. Add a label shows "Hello World".

    // Create a label and initialize with string "Hello World".
    CCLabelTTF* pLabel = CCLabelTTF::labelWithString("Hello World", "Thonburi", 64);
    CC_BREAK_IF(! pLabel);

    // Get window size and place the label upper. 
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    pLabel->setPosition(ccp(size.width / 2, size.height - 20));

    // Add the label to HelloWorld layer as a child layer.
    this->addChild(pLabel, 1);

    CCRenderTexture *rt = CCRenderTexture::renderTextureWithWidthAndHeight(120, 120, kCCTexture2DPixelFormat_RGBA4444);

    rt->clear(CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1());

    rt->setPosition(ccp(size.width/3, size.height/3));

    this->addChild(rt, 0);

    bRet = true;
} while (0);

return bRet;
}
适用于Cocos2D iPhone,但它可能会给您一些想法。一般原则是相同的。例如,您应该尝试从该渲染器中创建一个单独的精灵,并将该精灵添加为子精灵。

我有以下代码:

bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Scene::init() )
    {
        return false;
    }

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


    RenderTexture *rt = RenderTexture::create(120, 120, kCCTexture2DPixelFormat_RGBA4444);
    rt->clear(CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1());
    rt->setPosition(ccp(visibleSize.width/3, visibleSize.height/3));

    this->addChild(rt, 0);
    return true;
}
这段代码生成了这个

我没有看邮件的日期。 我希望这能帮助其他人