cocos2d-x 2.2 资源更新AssetsManager例子代码
转自: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例子代码的更多相关文章
- 基于Quick-cocos2d-x的资源更新方案 二
写在前面 又是12点半了,对于一个程序员来说,这是一个黄金时间,精力旺盛,我想,是最适合整理和分享一些思路的时候了. 自从上次写了 基于Quick-cocos2d-x的资源更新方案 同样可见quick ...
- 基于Quick-cocos2d-x的资源更新方案 一
图片来自网络 思绪何来 昨天写了一篇关于更新方案的理论 游戏开发:通过路径搜索优先级来进行补丁升级(从端游到手游) 今天继续细化一下 由于新项目采用的是Quick-cocos2d-x,那我就直接给出我 ...
- 武装你的WEBAPI-OData资源更新Delta
本文属于OData系列 目录 武装你的WEBAPI-OData入门 武装你的WEBAPI-OData便捷查询 武装你的WEBAPI-OData分页查询 武装你的WEBAPI-OData资源更新Delt ...
- mysql联合查询更新数据库例子
mysql联合查询更新数据库例子,用户表,部门表,把用户表中的部门属性更新为部门表的主键UPDATE user_table AS utINNER JOIN belongdept AS bd ON bd ...
- 另类Unity热更新大法:代码注入式补丁热更新
对老项目进行热更新 项目用纯C#开发的? 眼看Unity引擎热火朝天,无数程序猿加入到了Unity开发的大本营. 一些老项目,在当时ulua/slua还不如今天那样的成熟,因此他们选择了全c#开发:也 ...
- GridView中的编辑和删除按钮,执行更新和删除代码之前的更新提示或删除提示
在GridView中,可以通过设计界面GridViewr任务->编辑列->CommandField,很简单的添加的编辑和删除按钮 在前台源码中,可以看到GridView自动生成了两个列. ...
- <<精通iOS开发>>第14章例子代码彻底清除警告
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 上一篇我们解决了<<精通iOS开发>> ...
- git与eclipse集成之更新特性分支代码到个人特性分支
1.1. 更新特性分支代码到个人特性分支 在基于特性分支开发的过程中,存在多人向特性分支提交代码的情况,开发者需要关注特性分支代码与个人分支代码保持同步,否则可能导致提交代码冲突. 具体代码同步步骤: ...
- svn自动更新服务器最新代码
1.很简单打开dos界面 cd到svn exe目录下,运行 cd C:\Program Files\TortoiseSVN\bin --svn安装目录(作者使用时TortoiseSVN客户端,其 ...
随机推荐
- 【转】ios开发之AppDelegate
创建应用程序之后之后,默认有AppDelegate.h文件与AppDelegate.m文件. AppDelegate为何物? AppDelegate为整个应用的一个代理,提供程序启动.退出等类似 ...
- 微信支付-JSAPI支付V3-查询退款
接口地址 接口链接:https://api.mch.weixin.qq.com/pay/refundquery 是否需要证书 不需要. 请求参数 字段名 变量名 必填 类型 示例值 描述 公众账号ID ...
- Yii系列教程(三):集成Redis
1安装Redis 切换至/usr/local/src下,下载并安装redis: $ wgethttp://redis.googlecode.com/files/redis-2.6.12.tar.gz ...
- Java 设计模式学习总结(下)
(八)模板方法 模板方法模式在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中.模板方法使得子类可以在不改变算法结构的情况下,重新定义算法的某些步骤. templateMethod()会依次调用 ...
- 正则 提取html标签value
using System.Text.RegularExpressions; //step2: extract expected info //<h1 class="h1user&quo ...
- VC6.0到VS2013全部版本下载地址
Microsoft Visual Studio 6.0 下载:英文版360云盘下载: http://l11.yunpan.cn/lk/sVeBLC3bhumrI英文版115网盘下载: http://1 ...
- iOS学习笔记之回调(一)
什么是回调 看了好多关于回调的解释的资料,一开始总觉得这个概念理解起来有点困难,可能是因为自己很少遇到这种类型的调用吧.探索良久之后,才算有点启发,下面是自己的一点理解. 我们知道,在OSI网络七层模 ...
- SQL求 交集 并集 差集
故事是这样的….. 故事情节: 表 tb_test 有两列, colA , colB; 求 colA , colB 的并交差集… -- 计算并集 SELECT DISTINCT colB FROM t ...
- java中的Switch case语句
java中的Switch case 语句 在Switch语句中有4个关键字:switch,case break,default. 在switch(变量),变量只能是整型或者字符型,程序先读出这个变量的 ...
- Eclipse使用技巧总结
Eclipse设置工作空间的字符编码: 打开eclipse开发界面,依次点击Window->Preferences->General->Workspace 修改Text file e ...