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客户端,其 ...
随机推荐
- 获取Request请求的路径信息
从Request对象中可以获取各种路径信息,以下例子: 假设请求的页面是index.jsp,项目是WebDemo,则在index.jsp中获取有关request对象的各种路径信息如下 String p ...
- Docker 基础技术:Linux Namespace(下)
导读 在Docker基础技术:Linux Namespace(上篇)中我们了解了,UTD.IPC.PID.Mount 四个namespace,我们模仿Docker做了一个相当相当山寨的镜像.在这一篇中 ...
- Sikuli简介
Sikuli是利用屏幕上能够看到的图型做自动化,能够通过这个手段来识别和控制元素,非常适合和Selenium和Robot Framework一起结合起来做自动化. 1.Sikuli主页 http:// ...
- FreeMarker笔记 第四章 其它
4.1 自定义指令 4.1.1 简介 自定义指令可以使用macro指令来定义.Java程序员若不想在模板中实现定义指令,而是在Java语言中实现指令的定义,这时可以使用freemarker.templ ...
- js 判断输入是否为正整数
javascript代码如下: var re = new RegExp("^[1-9][0-9]*$"); if (re.test("11k")) { cons ...
- Hadoop2.3+Hive0.12集群部署
0 机器说明 IP Role 192.168.1.106 NameNode.DataNode.NodeManager.ResourceManager 192.168.1.107 Secondary ...
- 启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误
原因1:给定目录下jvm.dll不存在. 对策:(1)重新安装jre或者jdk并配置好环境变量.(2)copy一个jvm.dll放在该目录下. 原因2:eclipse的版本与jre或者jdk版本不一致 ...
- Node-APN 开源推送服务
Node-APN是一个开放的结合了苹果推送通知的Node.js模块,该源码模块使用简单,反馈服务支持.错误处理,在发送出错时自动重发.遵从苹果的最佳实践. Node-APN(github)
- linux 从命令行自动识别文件并将其打开的命令
若是shell是 zsh,则可: 使用 alias -s 定义后缀别名 (zsh) % alias -s pl=perl % script.pl perl script.pl % alias -s p ...
- 随手记录一个 firefox的backgroundPostion-x和-y的问题
今天帮大师写了一天项目,后来在测试一个显示升序和降序的标签上面,我使用了一个backgroundPosition-y来判断当前icon的状态,却无法不管是使用闭包还是个钟手段,在 firefox下面总 ...