cocos植物大战僵尸(一)load场景:异步加载资源

前端之家收集整理的这篇文章主要介绍了cocos植物大战僵尸(一)load场景:异步加载资源前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1项目描述

1.1功能描述

使用cocos2dx3.0C++编写植物大战僵尸,目前完成26种植物编写,后续添加各种僵尸。连载教程陆续更新,apk下载:http://download.csdn.net/detail/luoyikun/9078421


2详细设计

2.1load场景

2.1.1异步加载资源

要求异步加载资源同时显示Loading...的动画效果。项目加载的是一个plistpng。首先加载png

Director::getInstance()->getTextureCache()->addImageAsync(PATH_GAME_CACHE_PNG,CC_CALLBACK_1(CLoadingScene::onCallPlistImageAsync,this));

PATH_GAME_CACHE_PNG是要加载的大图路径。然后在回调函数中加载plist,加载完成后跳转场景。

void CLoadingScene::onCallPlistImageAsync(Texture2D* texture)
{
	SpriteFrameCache::getInstance()->addSpriteFramesWithFile(PATH_GAME_CACHE_PLIST,texture);
	CSceneMgr::getInstance()->sceneChange(CSceneMgr::enMainMenuScene);//游戏场景切换进去
}

2.1.2load场景显示加载动画

显示Loading...最后的三个点会逐步显示。具体思路:



void CLoadingScene::labLoadingShow()
{
	std::string sLoad = "Loading...";
	Label* laber = Label::create("","Terminal",50);
	this->addChild(laber);
	laber->setAnchorPoint(ccp(0,0.5));
	Size sizeWin = Director::getInstance()->getVisibleSize();
	laber->setPosition(ccp(sizeWin.width / 2 - 100,sizeWin.height / 2));

	Vector<FiniteTimeAction*> m_vecAct;
	for (int i = 7; i <= (int)sLoad.length(); i++)
	{
		auto delay = DelayTime::create(0.1f);
		m_vecAct.pushBack(delay);
		std::string str = sLoad.substr(0,i);
		auto func = [laber,str]() throw() {laber->setString(str); };
		auto call = CallFunc::create(func);
		m_vecAct.pushBack((FiniteTimeAction*)call);
	}
	auto delay = DelayTime::create(0.1f);
	m_vecAct.pushBack(delay);
	auto func = [laber]() throw() {laber->setString("Loading"); };
	auto call = CallFunc::create(func);
	m_vecAct.pushBack((FiniteTimeAction*)call);

	RepeatForever* actForever = RepeatForever::create(Sequence::create(m_vecAct));
	laber->runAction(actForever);
}

最终实现效果,开始游戏显示loading动画,加载完成后进入游戏。


load场景源码下载:

http://download.csdn.net/detail/luoyikun/9078495

原文链接:https://www.f2er.com/cocos2dx/341490.html

猜你在找的Cocos2d-x相关文章