转自:http://www.58player.com/blog-2327-601.html

//
// UpgradeLayer.h
// AmazeDemo
//
// Created by lsw on 14-3-26.
//
// #ifndef AmazeDemo_UpgradeLayer_h
#define AmazeDemo_UpgradeLayer_h
#include "cocos2d.h"
#include "AssetsManager.h" class UpgradeLayer : public cocos2d::CCLayer, public cocos2d::extension::AssetsManagerDelegateProtocol
{
public:
UpgradeLayer();
virtual ~UpgradeLayer(); static cocos2d::CCScene *scene();
virtual bool init(); void upgrade(cocos2d::CCObject *pSender);
void reset(cocos2d::CCObject *pSender); /* @brief Call back function for error
@param errorCode Type of error
*/
virtual void onError(cocos2d::extension::AssetsManager::ErrorCode errorCode);
/** @brief Call back function for recording downloading percent
@param percent How much percent downloaded
@warn This call back function just for recording downloading percent.
AssetsManager will do some other thing after downloading, you should
write code in onSuccess() after downloading.
*/
virtual void onProgress(int percent);
/** @brief Call back function for success
*/
virtual void onSuccess();
CREATE_FUNC(UpgradeLayer);
private:
cocos2d::extension::AssetsManager *getAssetManager();
void initDownloadDir();
private:
std::string pathToSave;
cocos2d::CCLabelTTF *_showDownloadInfo;
}; #endif //实现cpp //
// UpgradeLayer.cpp
// AmazeDemo
//
// Created by lsw on 14-3-26.
//
// #include "UpgradeLayer.h" #if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
#include <dirent.h>
#include <sys/stat.h>
#endif USING_NS_CC;
USING_NS_CC_EXT;
using namespace std; UpgradeLayer::UpgradeLayer()
:pathToSave("")
,_showDownloadInfo(NULL)
{ } UpgradeLayer::~UpgradeLayer()
{
AssetsManager *assetManager = getAssetManager();
CC_SAFE_DELETE(assetManager);
} CCScene* UpgradeLayer::scene()
{
CCScene *scene = CCScene::create();
UpgradeLayer *layer = UpgradeLayer::create();
scene->addChild(layer);
return scene;
} bool UpgradeLayer::init()
{
if (!CCLayer::init())
{
return false;
} initDownloadDir();
_showDownloadInfo = CCLabelTTF::create("", "Arial", );
this->addChild(_showDownloadInfo);
_showDownloadInfo->setAnchorPoint(ccp(, ));
_showDownloadInfo->setPosition(ccp(, )); CCMenuItemLabel *itemLabel1 = CCMenuItemLabel::create(CCLabelTTF::create("reset", "Arail", ), this, menu_selector(UpgradeLayer::reset)); CCMenuItemLabel *itemLabel2 = CCMenuItemLabel::create(CCLabelTTF::create("upgrade", "Arail", ), this, menu_selector(UpgradeLayer::upgrade)); CCMenu *menu = CCMenu::create(itemLabel1, itemLabel2, NULL);
this->addChild(menu);
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
itemLabel1->setPosition(ccp(winSize.width/, winSize.height/ + ));
itemLabel2->setPosition(ccp(winSize.width/, winSize.height/ - )); menu->setPosition(ccp(, )); return true;
} void UpgradeLayer::onError(AssetsManager::ErrorCode errorCode)
{
if (errorCode == AssetsManager::kNoNewVersion)
{
_showDownloadInfo->setString("no new version");
}
else if (errorCode == AssetsManager::kNetwork)
{
_showDownloadInfo->setString("network err");
}
} void UpgradeLayer::onProgress(int percent)
{
char progress[];
snprintf(progress, , "download %d%%", percent);
_showDownloadInfo->setString(progress);
} void UpgradeLayer::onSuccess()
{
_showDownloadInfo->setString("download success");
} AssetsManager* UpgradeLayer::getAssetManager()
{
static AssetsManager *assetManager = NULL;
if (!assetManager)
{
assetManager = new AssetsManager("https://raw.github.com/minggo/AssetsManagerTest/master/package.zip",
"https://raw.github.com/minggo/AssetsManagerTest/master/version",
pathToSave.c_str());
assetManager->setDelegate(this);
assetManager->setConnectionTimeout();
}
return assetManager;
} void UpgradeLayer::initDownloadDir()
{
pathToSave = CCFileUtils::sharedFileUtils()->getWritablePath();
pathToSave += "tmpDir"; #if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
DIR *pDir = NULL;
pDir = opendir(pathToSave.c_str());
if (!pDir)
{
mkdir(pathToSave.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
}
#else
if ((GetFileAttributesA(pathToSave.c_str())) == INVALID_FILE_ATTRIBUTES)
{
CreateDirectoryA(pathToSave.c_str(), );
}
#endif
} void UpgradeLayer::reset(CCObject *pSender)
{
_showDownloadInfo->setString("");
// Remove downloaded files
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
string command = "rm -r ";
// Path may include space.
command += "\"" + pathToSave + "\"";
system(command.c_str());
#else
string command = "rd /s /q ";
// Path may include space.
command += "\"" + pathToSave + "\"";
system(command.c_str());
#endif
getAssetManager()->deleteVersion();
initDownloadDir();
} void UpgradeLayer::upgrade(CCObject *pSender)
{
_showDownloadInfo->setString("");
getAssetManager()->update(); }

cocos2d-x 2.2 资源更新AssetsManager例子代码的更多相关文章

  1. 基于Quick-cocos2d-x的资源更新方案 二

    写在前面 又是12点半了,对于一个程序员来说,这是一个黄金时间,精力旺盛,我想,是最适合整理和分享一些思路的时候了. 自从上次写了 基于Quick-cocos2d-x的资源更新方案 同样可见quick ...

  2. 基于Quick-cocos2d-x的资源更新方案 一

    图片来自网络 思绪何来 昨天写了一篇关于更新方案的理论 游戏开发:通过路径搜索优先级来进行补丁升级(从端游到手游) 今天继续细化一下 由于新项目采用的是Quick-cocos2d-x,那我就直接给出我 ...

  3. 武装你的WEBAPI-OData资源更新Delta

    本文属于OData系列 目录 武装你的WEBAPI-OData入门 武装你的WEBAPI-OData便捷查询 武装你的WEBAPI-OData分页查询 武装你的WEBAPI-OData资源更新Delt ...

  4. mysql联合查询更新数据库例子

    mysql联合查询更新数据库例子,用户表,部门表,把用户表中的部门属性更新为部门表的主键UPDATE user_table AS utINNER JOIN belongdept AS bd ON bd ...

  5. 另类Unity热更新大法:代码注入式补丁热更新

    对老项目进行热更新 项目用纯C#开发的? 眼看Unity引擎热火朝天,无数程序猿加入到了Unity开发的大本营. 一些老项目,在当时ulua/slua还不如今天那样的成熟,因此他们选择了全c#开发:也 ...

  6. GridView中的编辑和删除按钮,执行更新和删除代码之前的更新提示或删除提示

    在GridView中,可以通过设计界面GridViewr任务->编辑列->CommandField,很简单的添加的编辑和删除按钮 在前台源码中,可以看到GridView自动生成了两个列. ...

  7. <<精通iOS开发>>第14章例子代码彻底清除警告

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 上一篇我们解决了<<精通iOS开发>> ...

  8. git与eclipse集成之更新特性分支代码到个人特性分支

    1.1. 更新特性分支代码到个人特性分支 在基于特性分支开发的过程中,存在多人向特性分支提交代码的情况,开发者需要关注特性分支代码与个人分支代码保持同步,否则可能导致提交代码冲突. 具体代码同步步骤: ...

  9. svn自动更新服务器最新代码

    1.很简单打开dos界面 cd到svn exe目录下,运行 cd C:\Program Files\TortoiseSVN\bin    --svn安装目录(作者使用时TortoiseSVN客户端,其 ...

随机推荐

  1. shell script入门

    从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用户的角度来看,Shell是用户与Linux操作系统沟通的桥梁.用户既可以输入命令执行,又可以利用 Shell脚本编程,完成更加复杂的操 ...

  2. android4.0以上部分手机绘图时会出现重影

    canvas外层的div需要设定属性style="overflow:visible;-webkit-transform: translateZ(0);

  3. hdu 3572 Task Schedule(最大流)2010 ACM-ICPC Multi-University Training Contest(13)——Host by UESTC

    题意: 告诉我们有m个任务和k个机器.第i个任务需要ci天完成,最早从第ai天开始,最晚在第bi天结束.每台机器每天可以执行一个任务.问,是否可以将所有的任务都按时完成? 输入: 首行输入一个整数t, ...

  4. Yii 实现MySQL多库和读写分离

    前段时间为SNS产品做了架构设计,在程序框架方面做了不少相关的压力测试,最终选定了YiiFramework,至于为什么没选用公司内部的PHP框架,其实理由很充分,公司的框架虽然是“前辈”们辛苦的积累, ...

  5. 因為 Hypervisor 未執行,所以無法啟動虛擬機器

    bcdedit /set hypervisorlaunchtype auto https://technet.microsoft.com/zh-tw/magazine/2009.02.hyperv.a ...

  6. Selenium2Library系列 keywords 之 _SelectElementKeywords 之 unselect_from_list(self, locator, *items)

    def unselect_from_list(self, locator, *items): """Unselects given values from select ...

  7. 创建优雅表格的8个js工具

    当需要呈现数百个表的数据时,展示和可访问性扮演着至关重要的角色.在这种情况下,倘若一个数据网格能够支持大量数据集的HTML Table并提供诸如排序.搜索.过滤和分页等功能,那是棒棒哒.在这篇文章中, ...

  8. Fisher information matrix笔记

    在看FK论文时,fisher information matrix是必须理解的. 从维基百科查阅到,Fisher information matrix是用利用最大似然函数估计来计算方差矩阵. 来源于: ...

  9. 高级正则表达式技术(Python版)

    正则表达式是从信息中搜索特定的模式的一把瑞士军刀.它们是一个巨大的工具库,其中的一些功能经常被忽视或未被充分利用.今天我将向你们展示一些正则表达式的高级用法. 举个例子,这是一个我们可能用来检测电话美 ...

  10. PHP中mysql_affected_rows()和mysql_num_rows()区别

    mysql_affected_rows -- 取得前一次 MySQL 操作所影响的记录行数mysql_num_rows -- 函数返回结果集中行的数目. config.php <?php hea ...