用的3.3的ccp-empty-test,但是用3.2就是正常的,截屏代码用的是这个大神写的没动过
http://www.cocoachina.com/bbs/read.php?tid=196339
引擎已经加入了截屏的接口
请参考base/ccUtils.cpp文件
void captureScreen(const std::function<void(bool, const std::string&)>& afterCaptured, const std::string& filename)
NewRendererTest也有对应的示例。
嗯,我看到了,还是有个疑问,如果想截取的是一个节点的纹理,底板能不能设置成透明的,比如我要绘制一棵树,树上的装饰品也要绘制出来,他们的背景是透明的,目前是黑色的
auto renderTexture = RenderTexture::create(SCREEN.width, SCREEN.height, Texture2D::PixelFormat::RGBA8888, GL_DEPTH24_STENCIL8_OES); 我用的这个方法
那篇文章的代码我测试了一下还是正常的,
环境是XCode6.0,iOS8.1模拟器,安卓我没有再进行测试。Cocos2d-x版本是最新的(我直接clone引擎仓库,同步更新,至少是3.3beta0+)
因为captureScreen函数是使用glReadPixels实现,所以你要的效果还是得用RenderTexture实现。
============以下代码和效果我同样只在Mac和iOS模拟器上测试过,环境和上面一致,并没经过大量测试,代码也没有进行防御,只供参考===============
void Director::saveScreenshot(const std::string& fileName, Node* node, const std::function& callback) { Image::Format format; //进行后缀判断 if(std::string::npos != fileName.find_last_of(".")){ auto extension = fileName.substr(fileName.find_last_of("."),fileName.length()); if (!extension.compare(".png")) { format = Image::Format::PNG; } else if(!extension.compare(".jpg")) { format = Image::Format::JPG; } else{ CCLOG("cocos2d: the image can only be saved as JPG or PNG format"); return; } } else { CCLOG("cocos2d: the image can only be saved as JPG or PNG format"); return ; } //获取屏幕尺寸,初始化一个空的渲染纹理对象 auto renderTexture = RenderTexture::create(getWinSize().width, getWinSize().height, Texture2D::PixelFormat::RGBA8888); //清空并开始获取 renderTexture->beginWithClear(0.0f, 0.0f, 0.0f, 0.0f); //遍历传入节点对象,填充纹理到RenderTexture中,这里应该要有防御代码 node->visit(); //结束获取 renderTexture->end(); //保存文件 renderTexture->saveToFile(fileName , format); //使用schedule在下一帧中调用callback函数 auto fullPath = FileUtils::getInstance()->getWritablePath() + fileName; auto scheduleCallback = &,fullPath,callback](float dt){ callback(fullPath); }; auto _schedule = getRunningScene()->getScheduler(); _schedule->schedule(scheduleCallback, this, 0.0f,0,0.0f, false, "screenshot"); } ``` 改的东西很简单,就是把getRunningScene()改为手动传入,你就是你说的那个节点 上图,有3个Sprire,层次关系是小的add到大的上。也就是大Sprite1--中Sprite2--小Sprite3。 另外我把最大的Sprite scale了,目的是看背景是否截出来是透明的。 如果传入的是Spriet1,那效果是Sprite123都能看到,注意close按钮和HelloWorld文本看不到了,背景透明了。 如果传入的是Sprite2的,那效果是只能看到Sprite23,Sprite1看不到,按钮和HelloWorld文本也看不到,背景也是透明。
多谢大大指教,问题解决了,哦也!