转自: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. struts2 action 返回图片流

    数据库为mssql zp字段为image类型 java代码 OutputStream out = null; try { String contenttype = "image/jpeg&q ...

  2. 嵌入式 busybox自带的tftp、telnet、ftp服务器

    a.tftp server服务器配置与使用 方法1: #udpsvd -vE 069 tftpd -c /root& //上面的0表示对所有ip地址都进行侦听 方法2: #vi/etc/ine ...

  3. Drupal 7.23:函数module_invoke_all()注释

    /** * Invokes a hook in all enabled modules that implement it. * * All arguments are passed by value ...

  4. strcpy基本用法

    C语言标准库函数strcpy,把从src地址开始且含有NULL结束符的字符串复制到以dest开始的地址空间. C语言函数 原型声明:extern char *strcpy(char* dest, co ...

  5. FILETIME, SYSTEMTIME 与 time_t 相互转换

    FILETIME, SYSTEMTIME 与 time_t 相互转换 2009-08-24 15:37:14|  分类: 默认分类|举报|字号 订阅     //******************* ...

  6. SpringMVC + Spring + MyBatis 学习笔记:SpringMVC和Spring一同工作的时候,AOP事务管理不起作用的解决方法

    系统:WIN8.1 数据库:Oracle 11GR2 开发工具:MyEclipse 8.6 框架:Spring3.2.9.SpringMVC3.2.9.MyBatis3.2.8 SpringMVC 的 ...

  7. httpd.conf 禁止运行PHP和html页面

    <VirtualHost *:80>    ServerName www.test.com    DocumentRoot /var/www/www.test.com    ErrorDo ...

  8. 【openstack报错】【因更新包而致】IncompatibleObjectVersion: Version 1.9 of Instance is not supported

    [时间]2014年2月18日 [平台]ubuntu 12.04.3 openstack havana [日志]/var/log/upstart/nova-compute.log  内容如下: ERRO ...

  9. 基于MapReduce的关系代数运算(1)

    1.选择运算 Map函数:对R中的每个元组t,检测它是否满足条件C,如果满足,则产生一个键值对(t,t) Reduce函数:直接将每个键值对传递到输出即可 2.投影运算 Map函数:对R中的每个元组t ...

  10. 转】Maven学习总结(九)——使用Nexus搭建Maven私服

    原博文出自于:http://www.cnblogs.com/xdp-gacl/p/4068967.html 感谢! 一.搭建nexus私服的目的 为什么要搭建nexus私服,原因很简单,有些公司都不提 ...