1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
//GameScene.h
#include "cocos2d.h"
USING_NS_CC;
class
GameScene :
public
cocos2d::Layer
{
public
:
static
cocos2d::Scene* createScene();
virtual
bool
init();
void
menuCallback(Ref* pSender);
CREATE_FUNC(GameScene);
private
:
//注意不能用auto关键字
Size size;
Sprite *sprite;
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
//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);
}
}
|