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
|
//GameScene.h
#include "cocos2d.h"
USING_NS_CC;
class
GameScene :
public
cocos2d::Layer
{
public
:
static
Scene* createScene();
virtual
bool
init();
void
menuCallback(Ref* pSender);
void
loadingCallBack(Ref* pSender);
void
turnToHelloScene();
private
:
Size size;
Label *labelLoading;
//Loading字样
Label *labelPercent;
//进度条数字
int
numberOfSprites;
//需要加载的资源数目
int
numberOfLoadSprites;
//已经加载完的资源数目
ProgressTimer *loadProgress;
//进度条
CREATE_FUNC(GameScene);
};
|
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
65
66
67
68
69
70
71
72
73
74
75
76
77
|
//GameScene.cpp
#include "GameScene.h"
#include "HelloWorldScene.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();
//size = Director::getInstance()->getWinSize();
labelLoading = Label::createWithSystemFont(
"Loading..."
,
"Marker Felt"
, 50);
labelLoading->setPosition(Vec2(size.width/2, size.height/2));
this
->addChild(labelLoading);
labelPercent = Label::createWithSystemFont(
"%0"
,
"Marker Felt"
, 50);
labelPercent->setPosition(Vec2(size.width/2, size.height*0.3));
this
->addChild(labelPercent);
numberOfSprites = 200;
numberOfLoadSprites = 0;
//创建一个进度条
loadProgress = ProgressTimer::create(Sprite::create(
"sliderProgress.png"
));
loadProgress->setBarChangeRate(Vec2(1, 0));
//横向移动
loadProgress->setType(ProgressTimer::Type::BAR);
loadProgress->setMidpoint(Vec2(0, 0));
loadProgress->setPosition(Vec2(size.width/2, size.height*0.2));
loadProgress->setPercentage(0.0f);
this
->addChild(loadProgress, 1);
//加载资源,由于图片过少,进度条速度过快,这里让其加载图片多一些,总共200张
for
(
int
i=0; i<100; i++)
{
Director::getInstance()->getTextureCache()->addImageAsync(
"OnePiece_1.png"
, CC_CALLBACK_1(GameScene::loadingCallBack,
this
));
Director::getInstance()->getTextureCache()->addImageAsync(
"Flag_1.png"
, CC_CALLBACK_1(GameScene::loadingCallBack,
this
));
}
return
true
;
}
void
GameScene::loadingCallBack(Ref* pSender)
{
numberOfLoadSprites++;
char
tmp[10];
float
value = ((
float
)numberOfLoadSprites / numberOfSprites)*100;
//已加载的百分比
sprintf
(tmp,
"%d%%"
, (
int
)(value));
labelPercent->setString(tmp);
loadProgress->setPercentage(value);
//加载完成,切换场景
if
(numberOfLoadSprites == numberOfSprites) {
turnToHelloScene();
}
}
void
GameScene::turnToHelloScene()
{
//读完进度条后跳转到HelloWorld场景
Director::getInstance()->replaceScene(HelloWorld::createScene());
}
|