文章摘自:http://www.jb51.cc/article/p-aseyqndm-bcu.html
AndroID和win32已经测试通过,理论上IOS也可以。
1、AssetsManagerDelegateProtocol
AssetsManagerDelegateProtocol,用于与服务器校验版本号,更新下载资源包,并对成功、出错、下载进度等进行回调。
2、资源包名称
所下载资源包名称默认为:cocos2dx-update-temp-package.zip。
如果想要修改文件名,直接修改引擎下 extensions\assets-manager\AsetsManager.ccp中的TEMP_PACKAGE_file_name
3、服务器
下以是我的资源包路径和本版号。
http://shezzer.sinaapp.com/downloadTest/cocos2dx-update-temp-package.zip
http://shezzer.sinaapp.com/downloadTest/version.php
4、实现
// Upgrade.h//<img src="http://img.blog.csdn.net/20140728093037654?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTGlhbmdzaGFvemU=/Font/5a6L5L2T/Fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />// Created by Sharezer on 14-07-26.////#ifndef _UPGRADE_H_#define _UPGRADE_H_#include "cocos2d.h"#include "extensions/cocos-ext.h"class Upgrade : public cocos2d::cclayer,public cocos2d::extension::AssetsManagerDelegateProtocol{public: Upgrade(); virtual ~Upgrade(); virtual bool init(); voID upgrade(cocos2d::Ref* pSender); //检查版本更新 voID reset(cocos2d::Ref* pSender); //重置版本 virtual voID onError(cocos2d::extension::AssetsManager::ErrorCode errorCode); //错误信息 virtual voID onProgress(int percent); //更新下载进度 virtual voID onSuccess(); //下载成功 CREATE_FUNC(Upgrade);private: cocos2d::extension::AssetsManager* getAssetManager(); voID initDownloadDir(); //创建下载目录private: std::string _pathToSave; cocos2d::Label *_showDownloadInfo;};#endif
//Upgrade.cpp
#include "Upgrade.h" #include "HelloWorldScene.h" #if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) #include <dirent.h> #include <sys/stat.h> #endif USING_NS_CC; USING_NS_CC_EXT; #define DOWNLOAD_FIEL "download" //下载后保存的文件夹名 Upgrade::Upgrade(): _pathToSave(""),_showDownloadInfo(NulL) { } Upgrade::~Upgrade() { AssetsManager* assetManager = getAssetManager(); CC_SAFE_DELETE(assetManager); } bool Upgrade::init() { if (!cclayer::init()) { return false; } Size winSize = Director::getInstance()->getWinSize(); initDownloadDir(); _showDownloadInfo = Label::createWithSystemFont("","Arial",20); this->addChild(_showDownloadInfo); _showDownloadInfo->setposition(Vec2(winSize.wIDth / 2,winSize.height / 2 - 20)); auto itemLabel1 = MenuItemLabel::create( Label::createWithSystemFont("reset","arail",20),CC_CALLBACK_1(Upgrade::reset,this)); auto itemLabel2 = MenuItemLabel::create( Label::createWithSystemFont("Upgrad",CC_CALLBACK_1(Upgrade::upgrade,this)); auto menu = Menu::create(itemLabel1,itemLabel2,NulL); this->addChild(menu); itemLabel1->setposition(Vec2(winSize.wIDth / 2,winSize.height / 2 + 20)); itemLabel2->setposition(Vec2(winSize.wIDth / 2,winSize.height / 2 )); menu->setposition(Vec2::ZERO); return true; } voID Upgrade::onError(AssetsManager::ErrorCode errorCode) { if (errorCode == AssetsManager::ErrorCode::NO_NEW_VERSION) { _showDownloadInfo->setString("no new version"); } else if (errorCode == AssetsManager::ErrorCode::NETWORK) { _showDownloadInfo->setString("network error"); } else if (errorCode == AssetsManager::ErrorCode::CREATE_file) { _showDownloadInfo->setString("create file error"); } } voID Upgrade::onProgress(int percent) { if (percent < 0) return; char progress[20]; snprintf(progress,20,"download %d%%",percent); _showDownloadInfo->setString(progress); } voID Upgrade::onSuccess() { cclOG("download success"); _showDownloadInfo->setString("download success"); std::string path = fileUtils::getInstance()->getWritablePath() + DOWNLOAD_FIEL; //fileUtils::getInstance()->addSearchPath(path,true); auto scene = HelloWorld::scene(); Director::getInstance()->replaceScene(scene); } AssetsManager* Upgrade::getAssetManager() { static AssetsManager *assetManager = NulL; if (!assetManager) { assetManager = new AssetsManager("http://shezzer.sinaapp.com/downloadTest/cocos2dx-update-temp-package.zip","http://shezzer.sinaapp.com/downloadTest/version.PHP",_pathToSave.c_str()); assetManager->setDelegate(this); assetManager->setConnectionTimeout(3); } return assetManager; } voID Upgrade::initDownloadDir() { cclOG("initDownloadDir"); _pathToSave = CCfileUtils::getInstance()->getWritablePath(); _pathToSave += DOWNLOAD_FIEL; cclOG("Path: %s",_pathToSave.c_str()); #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(),0); } #endif cclOG("initDownloadDir end"); } voID Upgrade::reset(Ref* 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 std::string command = "rd /s /q "; // Path may include space. command += "\"" + _pathToSave + "\""; system(command.c_str()); #endif getAssetManager()->deleteVersion(); initDownloadDir(); } voID Upgrade::upgrade(Ref* pSender) { _showDownloadInfo->setString(""); getAssetManager()->update(); }
运行:
reset:重置版本号,办删除下载资源。
Upgrad:校验版本号,当有更新时下载新资源。
以win32为,在cocos2d-x\build\DeBUG.win32\ 中,可以看到我们之前设置的下载目录download。
onSuccess中,当下载成功后,将跳转到HelloWorld。
这时可以在HelloWorld中直接使用已下载的资源。
具体实现代码见下载地址:点击打开链接
总结以上是内存溢出为你收集整理的Cocos2d-x 3.2 自动更新 -- 使用AssetsManager更新游戏资源包全部内容,希望文章能够帮你解决Cocos2d-x 3.2 自动更新 -- 使用AssetsManager更新游戏资源包所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)