سؤال

I am unable to create a sprite from render texture in cocos2d-x 3.0 which was possible in 2.2.x.

here is the code in 3.0 that I use.

Sprite* HelloWorld::createNewTexture(float width, float height)
{
    auto rt = RenderTexture::create(width, height);
    rt->beginWithClear(0.5, 0.5, 0.5, 1.0);
    rt->end();

    auto s = Sprite::createWithTexture(rt->getSprite()->getTexture());

    return s;
}
هل كانت مفيدة؟

المحلول

If your goal is obtaining a sprite with no image and only a background color then you can use this code

Sprite* HelloWorld::createNewTexture(float width, float height)
{
    auto s = Sprite::create();
    s->setTextureRect(Rect(0,0,width,height));
    s->setColor(Color3B::BLUE);
    s->setOpacity(128);

    return s;
}

However If you have to use RenderTexture then you should add a retain to it:

auto rt = RenderTexture::create(width, height);
rt->retain(); <--- ADD THIS LINE
rt->beginWithClear(0.5, 0.5, 0.5, 1.0);
rt->end();    

in your code. It works for me with cocos2d-x 3.0. More info at this link.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top