Cocos2d-x--实现Loading界面加载资源

转自:http://blog.csdn.net/zlqqhs/article/details/9269245


原理,使用

CCTextureCache::sharedTextureCache()->addImageAsync("background.png", this, callfuncO_selector(Assets::loadingCallBack));

加载纹理时,每加载一张纹理调用一次Assets::loadingCallBack,Assets::loadingCallBack实现进度条的进度跟进和判断跳转


步骤:

1.新建一个Cocos2d-x工程,保证能成功运行(我一直在说废话,呵呵)

2.在HelloWorldScene.h文件中修改和增加代码

3.在HelloWorldScene.cpp文件中修改和增加代码

4.新建SecondScene.h和SecondScene.cpp两个文件,用于加载完成后跳转


1.新建一个Cocos2d-x工程,保证能成功运行,没有其他的了


2.在HelloWorldScene.h文件中修改和增加代码


  1. HelloWorld(); 
  2.  
  3.     virtual bool init();   
  4.  
  5.     static cocos2d::CCScene* scene(); 
  6.      
  7.     CREATE_FUNC(Assets); 
  8.  
  9.     void loadingCallBack(cocos2d::CCObject *obj);//回调函数,每加载一张纹理就调用一次 
  10.  
  11.  
  12. private: 
  13.     cocos2d::CCLabelTTF *m_pLabelLoading;//文字标签,用于显示“Loading”字样 
  14.     cocos2d::CCLabelTTF *m_pLabelPercent;//文字标签,用于显示百分比 
  15.  
  16.     int currentNum;//当前加载到第几张 
  17.     int totalNum;//加载总数 
HelloWorld();

    virtual bool init();  

    static cocos2d::CCScene* scene();
    
    CREATE_FUNC(Assets);

	void loadingCallBack(cocos2d::CCObject *obj);//回调函数,每加载一张纹理就调用一次


private:
	cocos2d::CCLabelTTF *m_pLabelLoading;//文字标签,用于显示“Loading”字样
	cocos2d::CCLabelTTF *m_pLabelPercent;//文字标签,用于显示百分比

	int currentNum;//当前加载到第几张
	int totalNum;//加载总数


3.在HelloWorldScene.cpp文件中修改和增加代码


构造函数,初始化currentNum和totalNum

  1. HelloWorld::HelloWorld():currentNum(0),totalNum(500) 
  2.      
HelloWorld::HelloWorld():currentNum(0),totalNum(500)
{
	
}

修改init()中的do代码块为:


  1. CC_BREAK_IF(! CCLayer::init()); 
  2.  
  3.         //取得屏幕大小 
  4.         CCSize size = CCDirector::sharedDirector()->getWinSize(); 
  5.  
  6.         //文字标签,用于显示Loding字样和加载的进度 
  7.         m_pLabelLoading = CCLabelTTF::create("Loading...", "Arial", 25); 
  8.         m_pLabelPercent = CCLabelTTF::create("0%", "Arial", 25) ; 
  9.  
  10.         m_pLabelLoading->setPosition(ccp(size.width / 2, size.height / 2 + 50)); 
  11.         m_pLabelPercent->setPosition(ccp(size.width / 2, size.height / 2)); 
  12.  
  13.         this->addChild(m_pLabelLoading); 
  14.         this->addChild(m_pLabelPercent); 
  15.  
  16.         //加载HelloWorld.png纹理 
  17.         for (int i = 0; i < 500; i++) 
  18.         { 
  19.             //加载纹理,回调Assets::loadingCallBack 
  20.             CCTextureCache::sharedTextureCache()->addImageAsync("HelloWorld.png", this, callfuncO_selector(Assets::loadingCallBack)); 
  21.         } 
  22.          
  23.         bRet = true
CC_BREAK_IF(! CCLayer::init());

		//取得屏幕大小
		CCSize size = CCDirector::sharedDirector()->getWinSize();

		//文字标签,用于显示Loding字样和加载的进度
		m_pLabelLoading = CCLabelTTF::create("Loading...", "Arial", 25);
		m_pLabelPercent = CCLabelTTF::create("0%", "Arial", 25) ;

		m_pLabelLoading->setPosition(ccp(size.width / 2, size.height / 2 + 50));
		m_pLabelPercent->setPosition(ccp(size.width / 2, size.height / 2));

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

		//加载HelloWorld.png纹理
		for (int i = 0; i < 500; i++)
		{
			//加载纹理,回调Assets::loadingCallBack
			CCTextureCache::sharedTextureCache()->addImageAsync("HelloWorld.png", this, callfuncO_selector(Assets::loadingCallBack));
		}
		
        bRet = true;

回调函数loadingCallBack的实现:

  1. void Assets::loadingCallBack(cocos2d::CCObject *obj) 
  2.     //currentNum初始值为0,每加载一张纹理值自加1 
  3.     ++currentNum; 
  4.     char tmp[10]; 
  5.     //求得百分比 
  6.     sprintf(tmp, "%d%%", (int)(((float)currentNum / totalNum) * 100)); 
  7.     //设置标签显示的内容 
  8.     m_pLabelPercent->setString(tmp); 
  9.  
  10.     //如果当前加载的张数跟总数相等则进行场景跳转 
  11.     if (currentNum == totalNum) 
  12.     { 
  13.         this->removeChild(m_pLabelPercent, true); 
  14.         this->removeChild(m_pLabelLoading, true); 
  15.         //场景跳转 
  16.         CCScene *m_SecondScene = SecondScene::scene();   
  17.         CCDirector::sharedDirector()->replaceScene(CCTransitionJumpZoom::create(1.0f,m_SecondScene)); 
  18.     } 
void Assets::loadingCallBack(cocos2d::CCObject *obj)
{
	//currentNum初始值为0,每加载一张纹理值自加1
	++currentNum;
	char tmp[10];
	//求得百分比
	sprintf(tmp, "%d%%", (int)(((float)currentNum / totalNum) * 100));
	//设置标签显示的内容
	m_pLabelPercent->setString(tmp);

	//如果当前加载的张数跟总数相等则进行场景跳转
	if (currentNum == totalNum)
	{
		this->removeChild(m_pLabelPercent, true);
		this->removeChild(m_pLabelLoading, true);
		//场景跳转
		CCScene *m_SecondScene = SecondScene::scene();  
		CCDirector::sharedDirector()->replaceScene(CCTransitionJumpZoom::create(1.0f,m_SecondScene));
	}
}


4.新建SecondScene.h和SecondScene.cpp两个文件,用于加载完成后跳转


新建场景的步骤详见http://blog.csdn.net/zlqqhs/article/details/9245125

将SecondScene.cpp文件下的init函数下的代码改为:

  1. //通过名字从CCTextureCache中取得纹理 
  2.         CCTexture2D *texture = CCTextureCache::sharedTextureCache()->textureForKey("HelloWorld.png"); 
  3.         //实例化精灵 
  4.         CCSprite *m_sprite = CCSprite::createWithTexture(texture); 
  5.         CC_BREAK_IF(!m_sprite); 
  6.  
  7.         //设置精灵位置 
  8.         m_sprite->setPosition(ccp(100, 100)); 
  9.  
  10.         //添加精灵到场景 
  11.         this->addChild(m_sprite); 
  12.         bRet = true
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值