【Cocos2d-x】新手自学(七)百分比读取进度加载图片资源到缓存(2.0.1版本)

本文档基于Cocos2d-x 2.0.1版本,介绍了如何新手自学加载图片资源到缓存,并通过百分比显示加载进度。在新版本中,加载参数有所变化,加载完成后调用特定函数更新进度条直至100%。教程包括修改头文件、实现相关函数,并提供了示例图片。
摘要由CSDN通过智能技术生成

昨天下载了最新的cocos2d-x   是2.0.1版本的...所以本文章是参考2.0版本的例子写的...





可以发现,2.0版本后不单单只有一个帧率了..还多了两个参数.第一个参数可以理解为场景中的对象个数吧.第二个还不清楚...


1. 创建好一个工程后,先修改对于的.h文件

class HelloWorld : public cocos2d::CCLayer
{
public:
    virtual bool init();  
    static cocos2d::CCScene* scene();
	HelloWorld();

	void addSprite();//用于加载完成后添加精灵
	void loadingCallBack(cocos2d::CCObject *obj);//读进度回调函数

	LAYER_CREATE_FUNC(HelloWorld);

private:
	cocos2d::CCLabelTTF *m_pLableLoading;
	cocos2d::CCLabelTTF *m_pLabelPercent;
	int m_nNumberOfSprites;
	int m_nNumberOfLoadedSprites;
};

2. 然后在对应的cpp文件中完成各种函数


//原例子中的代码都是在构造函数中写的.在这里构造函数只用于初始化了两个成员变量
HelloWorld::HelloWorld():m_nNumberOfSprites(20),m_nNumberOfLoadedSprites(0)
{

}


bool HelloWorld::init()
{
    bool bRet = false;
    do 
    {
        CC_BREAK_IF(! CCLayer::init());
		CCSize size = CCDirector::sharedDirector()->getWinSize();

		//创建读条时的标签
		m_pLableLoading = CCLabelTTF::create("Loading...","Arial",20);
		m_pLabelPercent = CCLabelTTF::create("%0","Arial",30);
		
		m_pLabelPercent->setPosition(CCPointMake(size.width/2, size.height/2 +50));
		m_pLableLoading->setPosition(CCPointMake(size.width/2, size.height/2 -10));

		this->addChild(m_pLableLoading);
		this->addChild(m_pLabelPercent);

		//加载图片到Cache...每加载一次就调用我们自己写的回调函数
		CCTextureCache::sharedTextureCache()->addImageAsync("img/bg.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
		CCTextureCache::sharedTextureCache()->addImageAsync("img/01.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
		CCTextureCache::sharedTextureCache()->addImageAsync("img/02.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
		CCTextureCache::sharedTextureCache()->addImageAsync("img/03.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
		CCTextureCache::sharedTextureCache()->addImageAsync("img/04.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
		CCTextureCache::sharedTextureCache()->addImageAsync("img/05.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
		CCTextureCache::sharedTextureCache()->addImageAsync("img/06.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
		CCTextureCache::sharedTextureCache()->addImageAsync("img/07.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
		CCTextureCache::sharedTextureCache()->addImageAsync("img/08.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
		CCTextureCache::sharedTextureCache()->addImageAsync("img/09.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
		CCTextureCache::sharedTextureCache()->addImageAsync("img/10.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
		CCTextureCache::sharedTextureCache()->addImageAsync("img/11.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
		CCTextureCache::sharedTextureCache()->addImageAsync("img/12.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
		CCTextureCache::sharedTextureCache()->addImageAsync("img/13.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
		CCTextureCache::sharedTextureCache()->addImageAsync("img/14.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
		CCTextureCache::sharedTextureCache()->addImageAsync("img/15.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
		CCTextureCache::sharedTextureCache()->addImageAsync("img/16.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
		CCTextureCache::sharedTextureCache()->addImageAsync("img/17.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
		CCTextureCache::sharedTextureCache()->addImageAsync("img/18.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
		CCTextureCache::sharedTextureCache()->addImageAsync("img/19.png", this, callfuncO_selector(HelloWorld::loadingCallBack));

        bRet = true;
    } while (0);

    return bRet;
}

3. 代码很多都是重复的..只是加载的图片不一样,大家注意加载的图片路径就可以了..接下来实现回调函数


//回调函数
void HelloWorld::loadingCallBack(CCObject *obj)
{
	++m_nNumberOfLoadedSprites;//每调用一次就+1
	char tmp[10];

	//这里格式化,因为我们只加载了20张图片,所以除以总数后就是0.05,所以每加载一次就涨5%
	sprintf(tmp,"%%%d",(int)(((float)m_nNumberOfLoadedSprites / m_nNumberOfSprites) * 100));
	m_pLabelPercent->setString(tmp);//重设标签的内容

	//判断,如果全部加载完成后,就执行
	if (m_nNumberOfLoadedSprites == m_nNumberOfSprites)
	{
		//移除那两个标签文字..
		this->removeChild(m_pLableLoading, true);
		this->removeChild(m_pLabelPercent, true);
		//调用自己写的函数..从新绘制场景内容
		addSprite();
	}
	
}

4. 实现addSprite()函数,这个函数从新初始化了场景..然后显示


void HelloWorld::addSprite()
{
	CCSize size = CCDirector::sharedDirector()->getWinSize();

	//创建背景图片
	CCSprite *bg = CCSprite::create("img/bg.png");
	bg->setPosition(CCPointMake(size.width/2, size.height/2));

	//创建其他小图片
	CCSprite *s1 = CCSprite::create("img/01.png");
	CCSprite *s2 = CCSprite::create("img/02.png");
	CCSprite *s3 = CCSprite::create("img/03.png");
	CCSprite *s4 = CCSprite::create("img/04.png");
	CCSprite *s5 = CCSprite::create("img/05.png");
	CCSprite *s6 = CCSprite::create("img/06.png");
	CCSprite *s7 = CCSprite::create("img/07.png");
	CCSprite *s8 = CCSprite::create("img/08.png");
	CCSprite *s9 = CCSprite::create("img/09.png");
	CCSprite *s10 = CCSprite::create("img/10.png");
	CCSprite *s11 = CCSprite::create("img/11.png");
	CCSprite *s12 = CCSprite::create("img/12.png");
	CCSprite *s13 = CCSprite::create("img/13.png");
	CCSprite *s14 = CCSprite::create("img/14.png");
	CCSprite *s15 = CCSprite::create("img/15.png");
	CCSprite *s16 = CCSprite::create("img/16.png");
	CCSprite *s17 = CCSprite::create("img/17.png");
	CCSprite *s18 = CCSprite::create("img/18.png");
	CCSprite *s19 = CCSprite::create("img/19.png");

	//设置位置
	s1->setPosition(CCPointMake(70, size.height/2));
	s2->setPosition(CCPointMake(90, size.height/2));
	s3->setPosition(CCPointMake(110, size.height/2));
	s4->setPosition(CCPointMake(130, size.height/2));
	s5->setPosition(CCPointMake(150, size.height/2));
	s6->setPosition(CCPointMake(170, size.height/2));
	s7->setPosition(CCPointMake(190, size.height/2));
	s8->setPosition(CCPointMake(210, size.height/2));
	s9->setPosition(CCPointMake(230, size.height/2));
	s10->setPosition(CCPointMake(250, size.height/2));
	s11->setPosition(CCPointMake(270, size.height/2));
	s12->setPosition(CCPointMake(290, size.height/2));
	s13->setPosition(CCPointMake(310, size.height/2));
	s14->setPosition(CCPointMake(330, size.height/2));
	s15->setPosition(CCPointMake(350, size.height/2));
	s16->setPosition(CCPointMake(370, size.height/2));
	s17->setPosition(CCPointMake(390, size.height/2));
	s18->setPosition(CCPointMake(410, size.height/2));
	s19->setPosition(CCPointMake(430, size.height/2));

	//添加进入场景
	this->addChild(bg);

	this->addChild(s1);
	this->addChild(s2);
	this->addChild(s3);
	this->addChild(s4);
	this->addChild(s5);
	this->addChild(s6);
	this->addChild(s7);
	this->addChild(s8);
	this->addChild(s9);
	this->addChild(s10);
	this->addChild(s11);
	this->addChild(s12);
	this->addChild(s13);
	this->addChild(s14);
	this->addChild(s15);
	this->addChild(s16);
	this->addChild(s17);
	this->addChild(s18);
	this->addChild(s19);
}

这样,运行的时候就会出现读条界面了,但是闪的很快,比较电脑的处理能力还是很快的嘛..可以在回调函数里面设置一个断点,就会发现是5%的增长..


就我的理解来说,这一个例子的思路,首先是把图片加载进入内存.然后出发一次回调函数,更新读条进度..直到100%就调用另外函数...


图片的话,随便弄个几张就好了.显示效果的话,注意设置位置就行,也可以用我的图片啦:

http://115.com/file/anpzdgqa#img.rar


by...XX

深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 1. **神经网络(Neural Networks)**:深度学习的基础是人工神经网络,它是由多个层组成的网络结构,包括输入层、隐藏层和输出层。每个层由多个神经元组成,神经元之间通过权重连接。 2. **前馈神经网络(Feedforward Neural Networks)**:这是最常见的神经网络类型,信息从输入层流向隐藏层,最终到达输出层。 3. **卷积神经网络(Convolutional Neural Networks, CNNs)**:这种网络特别适合处理具有网格结构的数据,如图像。它们使用卷积层来提取图像的特征。 4. **循环神经网络(Recurrent Neural Networks, RNNs)**:这种网络能够处理序列数据,如时间序列或自然语言,因为它们具有记忆功能,能够捕捉数据中的时间依赖性。 5. **长短期记忆网络(Long Short-Term Memory, LSTM)**:LSTM 是一种特殊的 RNN,它能够学习长期依赖关系,非常适合复杂的序列预测任务。 6. **生成对抗网络(Generative Adversarial Networks, GANs)**:由两个网络组成,一个生成器和一个判别器,它们相互竞争,生成器生成数据,判别器评估数据的真实性。 7. **深度学习框架**:如 TensorFlow、Keras、PyTorch 等,这些框架提供了构建、训练和部署深度学习模型的工具和库。 8. **激活函数(Activation Functions)**:如 ReLU、Sigmoid、Tanh 等,它们在神经网络中用于添加非线性,使得网络能够学习复杂的函数。 9. **损失函数(Loss Functions)**:用于评估模型的预测与真实值之间的差异,常见的损失函数包括均方误差(MSE)、交叉熵(Cross-Entropy)等。 10. **优化算法(Optimization Algorithms)**:如梯度下降(Gradient Descent)、随机梯度下降(SGD)、Adam 等,用于更新网络权重,以最小化损失函数。 11. **正则化(Regularization)**:技术如 Dropout、L1/L2 正则化等,用于防止模型过拟合。 12. **迁移学习(Transfer Learning)**:利用在一个任务上训练好的模型来提高另一个相关任务的性能。 深度学习在许多领域都取得了显著的成就,但它也面临着一些挑战,如对大量数据的依赖、模型的解释性差、计算资源消耗大等。研究人员正在不断探索新的方法来解决这些问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值