//GameScene.cpp
#include "GameScene.h"
USING_NS_CC;
cocos2d::Scene* GameScene::createScene()
{
auto scene = Scene::create(); //创建一个场景
auto layer = GameScene::create(); //创建一个图层
scene->addChild(layer);
return scene;
}
//初始化当前的图层
bool GameScene::init()
{
if (!Layer::init()) //初始化父类
return false ;
//获取屏幕大小
size = Director::getInstance()->getVisibleSize();
//auto size = Director::getInstance()->getWinSize();
//添加一个图片精灵
sprite = Sprite::create( "OnePiece_1.png" );
sprite->setPosition(Vec2(size.width/2, size.height/2));
this ->addChild(sprite);
//创建一个菜单按钮
auto item = MenuItemFont::create( "click" , CC_CALLBACK_1(GameScene::menuCallback, this ));
item->setPosition(Vec2(size.width*0.9, size.height/2));
auto menu = Menu::create(item, NULL);
menu->setPosition(Vec2::ZERO);
this ->addChild(menu);
return true ;
}
void GameScene::menuCallback(Ref* pSender)
{
//屏幕截取
auto texture = RenderTexture::create(size.width, size.height);
texture->begin();
Director::getInstance()->getRunningScene()->visit(); //遍历屏幕所有节点
texture->end();
if (texture->saveToFile( "ScreenShot.png" , Image::Format::PNG))
{ //3.0版本改变处,支持PNG与JPG格式
log ( "Success." );
//获取截图
//方式一
std::string filePath = FileUtils::getInstance()->getWritablePath() + "ScreenShot.png" ;
//方式二
//auto sprite = Sprite::createWithTexture(texture->getSprite()->getTexture());
auto sprite = Sprite::create(filePath.c_str());
sprite->setPosition(Vec2(size.width/2, size.height/2));
//sprite->setFlippedY(true); //将截图正常显示
sprite->setScale(0.5f);
this ->addChild(sprite);
}
}
|