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", 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();
    
}

 

你可能感兴趣的:(cocos2d-x)