【cocos2dx学习笔记】制作Loding场景
来源:互联网 发布:中国进口数据 2008年 编辑:程序博客网 时间:2024/04/30 01:21
为什么要制作loading场景?
在实际游戏中会有一个场景中拥有大量的资源,直接跳转会造成卡顿,而且在场景跳转的过程中会存在一个时间段,在这个时间段中跳转前的场景和将要跳转到的场景都占用着系统资源,会使系统更加卡。
制作loading场景的两种方法:
1、伪loading跳转:
制作一个只拥有背景和进度条的场景夹在两个场景的中间作为缓冲,自己设定时间使得场景进行两次跳转。
2、真loading跳转
利用此函数
CCTextureCache::sharedTextureCache()->addImageAsync("HelloWorld.png", this, callfuncO_selector(MyScene::loadingcallback));进行预加载图片资源
具体代码如下:
loading场景MyScene的头文件:
#include "cocos2d.h"class MyScene:public cocos2d::CCLayer{public:MyScene();~MyScene();virtual bool init();static cocos2d::CCScene* scene();void menuCallBack(CCObject *sender);//void funcCallBack();void turntoscene(); //转换场景void loadingcallback(cocos2d::CCObject* sender); //加载每个资源的回调函数private:cocos2d::CCLabelTTF* plable1; //loading....字样cocos2d::CCLabelTTF* plable2; // 百分号float numberofSprites; // 需要预加载的资源数量float numberoflodedSprites; // 已经预加载好的资源数量cocos2d::CCProgressTimer* loadprogess; //进度条CREATE_FUNC(MyScene);};
MyScene源文件中的init函数
bool MyScene::init(){CCSize size = CCDirector::sharedDirector()->getWinSize();plable1 = CCLabelTTF::create("Loding.......", "Arial", 20);plable2 = CCLabelTTF::create("0%", "Arial", 20);plable1->setPosition(ccp(size.width*0.4,size.height*0.4));plable2->setPosition(ccp(size.width*0.57, size.height*0.4));addChild(plable1);addChild(plable2);numberofSprites = 200;numberoflodedSprites = 0;//CCSprite* loadbg = CCSprite::create(""); //进度条底图//loadbg->setPosition(ccp(size.width / 2, size.height*0.3));//addChild(loadbg);//创建进度条loadprogess = CCProgressTimer::create(CCSprite::create("btn-about-selected.png"));loadprogess->setBarChangeRate(ccp(1, 0)); //进度条变化速率loadprogess->setType(kCCProgressTimerTypeBar); //进度条类型loadprogess->setMidpoint(ccp(0,1)); // 设置进度条变化方向loadprogess->setPosition(ccp(size.width / 2, size.height*0.3));loadprogess->setPercentage(0); //设置初始值addChild(loadprogess, 1);//加载资源for (int i = 0; i < 100; i++){CCTextureCache::sharedTextureCache()->addImageAsync("HelloWorld.png", this, callfuncO_selector(MyScene::loadingcallback));CCTextureCache::sharedTextureCache()->addImageAsync("Icon.png", this, callfuncO_selector(MyScene::loadingcallback));}return true;}MyScene源文件的loadingcallback函数
void MyScene::loadingcallback(cocos2d::CCObject* sender){numberoflodedSprites++;char a[10];float values = (numberoflodedSprites / numberofSprites) * 100;sprintf(a, "%d%%", (int)values);CCLOG("callback");plable2->setString(a);loadprogess->setPercentage(values);if (numberoflodedSprites == numberofSprites)turntoscene();}MyScene源文件的场景转换函数
void MyScene::turntoscene(){CCDirector::sharedDirector()->replaceScene(HelloWorld::scene());}效果图如下:
0 0
- 【cocos2dx学习笔记】制作Loding场景
- Cocos2dx学习笔记(二)MyHelloWorldScene场景类
- 【cocos2dx】cocos2dx学习笔记
- Loding
- cocos2dx -- 学习笔记 利用UIButton制作虚拟按键
- cocos2dx 3.3 + lua 学习笔记(02)--- 游戏场景的基本搭建
- cocos2dx进阶学习之场景切换
- [cocos2dx 日常学习]场景回溯
- cocos2dx学习笔记
- cocos2dx 学习笔记 c4
- cocos2dx 学习笔记
- quick-cocos2dx学习笔记
- [cocos2dx]学习笔记 jsonDict
- cocos2dx --tinyxml2 学习笔记
- cocos2dx学习笔记一
- COCOS2DX,LUA,学习笔记
- cocos2dx学习笔记2
- cocos2dx学习笔记
- 身体各个局部暴瘦的方法!
- 程序只启动一个实例的几种方法
- 我的大四
- POJ 2662-A Walk Through the Forest(最短路+记忆化搜索)
- QuickNews新闻客户端
- 【cocos2dx学习笔记】制作Loding场景
- poj 3278
- Unity3D学习之(循环的例题解析)
- iOS点语法@property属性
- 解决vim的backspace不能正常使用
- [Unity3d]一个优秀的程序必备的几种设计模式(待续)
- c++中的引用
- java中简单工厂模式
- 开源项目(L ProgressBar)