我无法在cocos2d-x 3.0中使用渲染纹理创建精灵,这在2.2.x中是可能的。
这是我使用的3.0中的代码。
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;
}
答案 0 :(得分:1)
如果您的目标是获取没有图像且只有背景颜色的精灵,那么您可以使用此代码
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;
}
但是如果你必须使用RenderTexture,那么你应该为它添加一个保留:
auto rt = RenderTexture::create(width, height);
rt->retain(); <--- ADD THIS LINE
rt->beginWithClear(0.5, 0.5, 0.5, 1.0);
rt->end();
你的代码中的。它适用于cocos2d-x 3.0。有关此link的更多信息。