0

0

Cocos2dx 3.0 过渡篇(十)资源加载进度条Loading...

php中文网

php中文网

发布时间:2016-06-07 15:42:48

|

1415人浏览过

|

来源于php中文网

原创

http://blog.csdn.net/start530/article/details/19420317 本来这篇博文是昨晚就要写的,可是因为今早要去参加考驾照相关的体检,而我最害怕的就是视力没能达到5.0,毕竟这阶段对着屏幕的时间过久。 所以呢,昨晚我几乎没碰电脑,没玩手机,早睡早起。体检顺

http://blog.csdn.net/start530/article/details/19420317


本来这篇博文是昨晚就要写的,可是因为今早要去参加考驾照相关的体检,而我最害怕的就是视力没能达到5.0,毕竟这阶段对着屏幕的时间过久。

所以呢,昨晚我几乎没碰电脑,没玩手机,早睡早起。体检顺利通过!


首先,我要说的是:这次我要写的主题是进度条。 额,等等,先收起你手里愤怒的西瓜刀。我也才知道TestCpp也有这个例子啊。不过TestCpp里的只有label的变化,而我的多加了个进度条。
请容我对我的这种手段取个好听的名称:画龙点睛!


恩,步骤如下:
1、创建label和progressTimer;
2、加载资源,每加载一张都调用回调函数;
3、加载完成,进入新的界面。



首先看下头文件:HelloWorld.h

松果AI写作
松果AI写作

专业全能的高效AI写作工具

下载
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::Layer
{
public:
	HelloWorld():m_numSp(20),m_loadedSp(0),loadProgress(NULL){};

    static cocos2d::Scene* createScene();
    virtual bool init();  

	void loadingCallback(Object* pSender);//加载一张图片完成后跳转的毁掉函数

	void gotoNewLayer();//加载完后的跳转函数
    CREATE_FUNC(HelloWorld);

private:
	cocos2d::ProgressTimer* loadProgress;//进度条

	cocos2d::LabelTTF* percentLabel;//加载进度label
	cocos2d::LabelTTF* loadLabel;//显示 loading: 的label

	int m_numSp;//要加载的精灵数目,初始化为 20 张
	int m_loadedSp;//已加载的精灵数目
};

#endif // __HELLOWORLD_SCENE_H__

1、创建
Size visibleSize = Director::getInstance()->getVisibleSize();
Point origin = Director::getInstance()->getVisibleOrigin();

loadLabel = LabelTTF::create("Loading:","Arial",20);//创建显示Loading: 的label
loadLabel->setPosition(Point(visibleSize.width/2-30,visibleSize.height/2+30));
this->addChild(loadLabel,1);

percentLabel = LabelTTF::create("0%","Arial",20);//创建显示百分比的label
percentLabel->setPosition(Point(visibleSize.width/2+35,visibleSize.height/2+30));
this->addChild(percentLabel,2);

auto loadBg = Sprite::create("sliderTrack.png");//进程条的底图
loadBg->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
this->addChild(loadBg,1);

loadProgress = ProgressTimer::create(Sprite::create("sliderProgress.png"));//创建一个进程条
loadProgress->setBarChangeRate(Point(1,0));//设置进程条的变化速率
loadProgress->setType(ProgressTimer::Type::BAR);//设置进程条的类型
loadProgress->setMidpoint(Point(0,1));//设置进度的运动方向
loadProgress->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
loadProgress->setPercentage(0.0f);//设置初始值为0
this->addChild(loadProgress,2);

2、加载图片
//加载20张图片,每加载完一张就调用回调函数:loadingCallback
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld1.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld2.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld3.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld4.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld5.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld6.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld7.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));

Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));

3、图片加载后的回调函数:
void HelloWorld::loadingCallback(Object* pSender)
{
	++m_loadedSp;//每进到这个函数一次,让m_loadedSp + 1

	char buf_str[16];
	sprintf(buf_str,"%d%%",(int)(((float)m_loadedSp / m_numSp) * 100),m_numSp);
	percentLabel->setString(buf_str);//更新percentLabel的值

	float newPercent = 100 - ((float)m_numSp - (float)m_loadedSp)/((float)m_numSp/100);//计算进度条当前的百分比
	//因为加载图片速度很快,所以就没有使用ProgressTo,
	//或者ProgressFromTo这种动作来更新进度条
	loadProgress->setPercentage(newPercent);//更新进度条

	//图片加载完成后
	if(m_loadedSp == m_numSp)
	{
		this->removeChild(loadProgress);//将添加的几个对象删除掉
		this->removeChild(percentLabel);
		this->removeChild(loadLabel);

		//加载完既要跳转到gotoNewLayer,在这里可以
		//创建新的Scene,新的Layer,或者其他什么乱七八糟的
		this->gotoNewLayer();
	}
}

4、进入新的界面
void HelloWorld::gotoNewLayer()
{
	auto size = Director::getInstance()->getWinSize();

	auto sp = Sprite::create("HelloWorld.png");//用之前加载到缓存中的图片,创建一个精灵。
	sp->setPosition(Point(size.width/2,size.height/2));
	this->addChild(sp,1);
}


因为代码里注释都写的挺详细的,所以我也就不说太多废话了。
恩,写完了。这篇是下班后加班写的,外面又下了大雨,我要赶紧冲回去吃饭了。风一般的男纸


http://blog.csdn.net/start530?viewmode=contents


相关专题

更多
php与html混编教程大全
php与html混编教程大全

本专题整合了php和html混编相关教程,阅读专题下面的文章了解更多详细内容。

1

2026.01.13

PHP 高性能
PHP 高性能

本专题整合了PHP高性能相关教程大全,阅读专题下面的文章了解更多详细内容。

5

2026.01.13

MySQL数据库报错常见问题及解决方法大全
MySQL数据库报错常见问题及解决方法大全

本专题整合了MySQL数据库报错常见问题及解决方法,阅读专题下面的文章了解更多详细内容。

6

2026.01.13

PHP 文件上传
PHP 文件上传

本专题整合了PHP实现文件上传相关教程,阅读专题下面的文章了解更多详细内容。

5

2026.01.13

PHP缓存策略教程大全
PHP缓存策略教程大全

本专题整合了PHP缓存相关教程,阅读专题下面的文章了解更多详细内容。

3

2026.01.13

jQuery 正则表达式相关教程
jQuery 正则表达式相关教程

本专题整合了jQuery正则表达式相关教程大全,阅读专题下面的文章了解更多详细内容。

1

2026.01.13

交互式图表和动态图表教程汇总
交互式图表和动态图表教程汇总

本专题整合了交互式图表和动态图表的相关内容,阅读专题下面的文章了解更多详细内容。

5

2026.01.13

nginx配置文件详细教程
nginx配置文件详细教程

本专题整合了nginx配置文件相关教程详细汇总,阅读专题下面的文章了解更多详细内容。

3

2026.01.13

nginx部署php项目教程汇总
nginx部署php项目教程汇总

本专题整合了nginx部署php项目教程汇总,阅读专题下面的文章了解更多详细内容。

5

2026.01.13

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
玩转 Vue3.0 新特性
玩转 Vue3.0 新特性

共19课时 | 1.8万人学习

Vue3.0 极速入门教程
Vue3.0 极速入门教程

共55课时 | 5.5万人学习

vue 3.0全新实战课程-第一季
vue 3.0全新实战课程-第一季

共51课时 | 22.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号