cocos2d-x 2.2 资源更新AssetsManager例子代码

直接上代码:

//
//  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", 25);
    this->addChild(_showDownloadInfo);
    _showDownloadInfo->setAnchorPoint(ccp(0, 0));
    _showDownloadInfo->setPosition(ccp(20, 20));
    
    
    CCMenuItemLabel *itemLabel1 = CCMenuItemLabel::create(CCLabelTTF::create("reset", "Arail", 20), this, menu_selector(UpgradeLayer::reset));
    
    CCMenuItemLabel *itemLabel2 = CCMenuItemLabel::create(CCLabelTTF::create("upgrade", "Arail", 20), 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/2, winSize.height/2 + 20));
    itemLabel2->setPosition(ccp(winSize.width/2, winSize.height/2 - 20));
    
    menu->setPosition(ccp(0, 0));
    
    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[20];
    snprintf(progress, 20, "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(3);
    }
    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(), 0);
	}
#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();
    
}

更新代码中的资源下载路径和版本路径换成需要的路径就可以了



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值