首页 > 代码库 > Cocos2d-x3.2 Loading场景的设计
Cocos2d-x3.2 Loading场景的设计
//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); };
//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()); }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。