如何使用CCRenderTexture创建背景?

问题描述 投票:1回答:1

我试图使用CCRenderTexture绘制一个简单的背景。

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

用一些颜色清除它。

添加到节点,

为节点添加了标签

================================================== ========================================当我运行它时,我看到的只是黑色屏幕与你好世界上的标签。

那纹理在哪里?

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-x
1个回答
0
投票

This CCRenderTexture tutorial适用于Cocos2D iPhone,但它可能会给你一些想法。一般原则是相同的。例如,您应该尝试从该rendertexture创建一个单独的sprite,并将该sprite添加为child。

© www.soinside.com 2019 - 2024. All rights reserved.