cocos2dx3.x《格斗》类游戏加载界面(一)

访问我的个人博客 秋码个人博客
最终呈现的效果图如下:
这里写图片描述
这里写图片描述
cocos2dx版本:cocos2d-x3.8
IDE版本:VS2013
首先使用cocos命令创建项目:

cocos new GeDou -p com.zhenqi.game -l cpp -d F:\cocos2d-xProject

这里写图片描述
双击它
这里写图片描述
新建一个场景 LoadingScene
LoadingScene.h文件定义如下

#ifndef __LOADING_SCENE_H__
#define __LOADING_SCENE_H__

#include "ui/CocosGUI.h"

#include "cocos2d.h"
USING_NS_CC;

class Loading : public Layer
{
public:
	static Scene* createScene();
	virtual bool init();

	//设置进度条
	void SetPro(float fRate);
	//资源预加载
	void LoadRec();
	//资源预加载回调函数
	void OnLoadArmatureData(float percent);
	//资源加载回调
	void OnLoadRecCallBack(Object *pObj);

    CREATE_FUNC(Loading);

protected:
	int	m_nLoadRecNum;				//资源加载数
	int	m_nLoadRecTotalNum;			//资源加载总数

	ui::LoadingBar* pProLoadRec;    //进度条
};

init方法的实现内容,基本上就是添加图片。

bool Loading::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

	Size size = Director::getInstance()->getWinSize();
	/************************************************************************/
	/*				背景图片                                                */
	/************************************************************************/
	//人物图片
	auto pPersonBg = Sprite::create("ui/loading_bg.png");
	pPersonBg->setPosition(ccp(size.width*0.5f, size.height*0.5f));
	addChild(pPersonBg, 100);

	//创建logo图
	auto pLogo = Sprite::create("ui/logo_bg.png");
	pLogo->setScale(0.95f);
	pLogo->setPosition(ccp(size.width*0.5f, 410));
	addChild(pLogo, 100);

	/************************************************************************/
	/*				进度条                                                  */
	/************************************************************************/
	//创建加载进度条背景
	auto pProBg = Sprite::create("ui/loading_progress_bg.png");
	pProBg->setPosition(ccp(size.width*0.5f, 100));
	addChild(pProBg, 100000);

	pProLoadRec = ui::LoadingBar::create("ui/loading_progress_bar.png");
	pProLoadRec->setPosition(pProBg->getPosition());
	pProLoadRec->setDirection(ui::LoadingBar::Direction::LEFT);
	pProLoadRec->setPercent(0);
	addChild(pProLoadRec, 100001, 1);//同时设置Tag值为1
	
	auto pLight = Sprite::create("ui/loading_progress_light.png");
	addChild(pLight, 300000, 2);
	pLight->setPosition(ccp(pProLoadRec->getPositionX() - pProLoadRec->getContentSize().width*0.5f, pProLoadRec->getPositionY()));

	m_nLoadRecNum = 0;
	m_nLoadRecTotalNum = 16;

	//预加载本层资源
	LoadRec();
    
    return true;
}

再看LoadRec()方法,实现如下:
关于ArmatureDataManager类运用,首先添加头文件。

#include "cocostudio/CocoStudio.h"
using namespace cocostudio;
//资源预加载
void Loading::LoadRec()
{
	TextureCache::getInstance()->addImageAsync("ui/serverselect_bg.png", CC_CALLBACK_1(Loading::OnLoadRecCallBack,this));
	
	ArmatureDataManager::getInstance()->addArmatureFileInfoAsync("armature/kulougongshou.ExportJson",this,schedule_selector(Loading::OnLoadArmatureData,this));
	ArmatureDataManager::getInstance()->addArmatureFileInfoAsync("armature/kulouzhanshi.ExportJson", this, schedule_selector(Loading::OnLoadArmatureData, this));
	ArmatureDataManager::getInstance()->addArmatureFileInfoAsync("armature/mayi.ExportJson", this, schedule_selector(Loading::OnLoadArmatureData, this));
	ArmatureDataManager::getInstance()->addArmatureFileInfoAsync("armature/bianyikunchong.ExportJson", this, schedule_selector(Loading::OnLoadArmatureData, this));
	
	ArmatureDataManager::getInstance()->addArmatureFileInfoAsync("armature/bubing.ExportJson", this, schedule_selector(Loading::OnLoadArmatureData, this));
	ArmatureDataManager::getInstance()->addArmatureFileInfoAsync("armature/xiaoyin.ExportJson", this, schedule_selector(Loading::OnLoadArmatureData, this));
	ArmatureDataManager::getInstance()->addArmatureFileInfoAsync("armature/NewProject.ExportJson", this, schedule_selector(Loading::OnLoadArmatureData, this));
	ArmatureDataManager::getInstance()->addArmatureFileInfoAsync("armature/minren1.ExportJson", this, schedule_selector(Loading::OnLoadArmatureData, this));
	
	ArmatureDataManager::getInstance()->addArmatureFileInfoAsync("armature/kulou_arrow.ExportJson", this, schedule_selector(Loading::OnLoadArmatureData, this));
	ArmatureDataManager::getInstance()->addArmatureFileInfoAsync("armature/naili.ExportJson", this, schedule_selector(Loading::OnLoadArmatureData, this));
	ArmatureDataManager::getInstance()->addArmatureFileInfoAsync("armature/NPC_kakaxi.ExportJson", this, schedule_selector(Loading::OnLoadArmatureData, this));
	ArmatureDataManager::getInstance()->addArmatureFileInfoAsync("armature/portal.ExportJson", this, schedule_selector(Loading::OnLoadArmatureData, this));
	
	ArmatureDataManager::getInstance()->addArmatureFileInfoAsync("armature/hited_light.ExportJson", this, schedule_selector(Loading::OnLoadArmatureData, this));
	ArmatureDataManager::getInstance()->addArmatureFileInfoAsync("armature/public_casting.ExportJson", this, schedule_selector(Loading::OnLoadArmatureData, this));
	ArmatureDataManager::getInstance()->addArmatureFileInfoAsync("armature/skill_light_1.ExportJson", this, schedule_selector(Loading::OnLoadArmatureData, this));
}

下面是回调函数:

void Loading::OnLoadArmatureData(float percent){
	m_nLoadRecNum++;
	SetPro((float)m_nLoadRecNum / (float)m_nLoadRecTotalNum);
	if (percent >= 1)
	{
		CCLog("addArmatureFileInfoAsync over");
	}
}

void Loading::OnLoadRecCallBack(Object *pObj){
	do
	{
		m_nLoadRecNum++;
		SetPro((float)m_nLoadRecNum / (float)m_nLoadRecTotalNum);
		return;
	} while (false);
	CCLog("Fun CNFLoginScene::OnLoadRecCallBack Error!");
}

最后再来看看这个setPro方法,也就是设置进度条进度。

void Loading::SetPro(float fRate)
{
	do
	{
		pProLoadRec->setPercent(fRate * 100);

		//同步光点
		auto pLight = dynamic_cast<Sprite *>(getChildByTag(2));
		if (pLight != NULL)
			pLight->setPositionX(pProLoadRec->getPositionX() + pProLoadRec->getContentSize().width*(fRate - 0.5f));

		//若加载完成
		if (fRate >= 1)
		{
			auto pDicLang = Dictionary::createWithContentsOfFile("ui_xml/loading_xml.xml");
			
			//得到加载信息label
			auto pStrLoading = dynamic_cast<String*>(pDicLang->objectForKey("loading_end"));
			auto pLabelLoading = dynamic_cast<LabelTTF *>(getChildByTag(3));
		}

		return;
	} while (false);
	CCLog("Fun CNFLoadingLayer::SetPro Error!");
}

后续将完成人物选择界面。待续中。。。。。。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

甄齐才

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值