bool WorldWindow::saveSpriteImg(const std::string& fileName)
{
bool flag = false;
Size wsize = Director::getInstance()->getWinSize();
CCRenderTexture *texture = CCRenderTexture::create(wsize.width, wsize.height, Texture2D::PixelFormat::RGBA8888, GL_DEPTH24_STENCIL8);
texture->setPosition(ccp(0, 0));
texture->ignoreAnchorPointForPosition(true);
texture->setAnchorPoint(CCPointZero);
texture->beginWithClear(0.0f, 0.0f, 0.0f, 0.0f);
// -- start 需要渲染的节点/Users/topgame1602/Desktop/saveSpriteImg.js
Node *n = _uiMainNode->getNodeByName("UI");
n->visit();
// -- end
texture->end();
// 当前渲染的节点
texture->saveToFile("aa1.png", kCCImageFormatPNG, true, [=](RenderTexture* t, const std::string& s){
CCLog("%s", s.c_str());
});
return flag;
}
本文介绍了一种使用Cocos2d-x在游戏中截取指定节点图像并保存的方法。通过创建渲染纹理,调整其属性,并将指定节点渲染到该纹理上,最后将纹理内容保存为图片文件。

1000

被折叠的 条评论
为什么被折叠?



