[Cocos2d-js official documentation] Second, the resource manager Assets Manager

This document will introduce a new feature heavyweight Cocos2d-JS 3.0: Resource Manager (JSB supports only). Explorer is designed to run as a resource boom when the game update, here's resources can be images, audio and even game script itself. Use Explorer, you will be able to upload new resources to your server, your game will track changes on a remote server to download new resources to the user's device and use the new resources in the game. In this way, new design, new game play experience even new content will immediately be pushed to your user's hand. It is important that you do not need to for each channel to repackage your application and experience pain updated review of the application, this process no cost!

scenes to be used

Imagine your game has been released in the App Store, but you suddenly find a design or oversight on some very negative user feedback, you certainly worried, but after the changes are complete, packaged software or have to wait countless channels and annoying App store review ... until approved on-line, users also need to update their software version, or ... maybe not pass the audit. See here, there are so experienced people will not feel good, I want to say, such a process is too painful, and many users may already be lost in the process.

Other usage scenarios:

  • Valentine's Day is coming, you want to organize a game in the event, the opportunity is missed is certainly not the results you most want to see.
  • When you find a serious bug.
  • When you want to add some new scenes or points to extend the life of the game.
  • And so many other cases ...

For these cases, if you can get in quickly after the development is completed deployment to the hands of users, I said quickly, maybe a night is it not very attractive it? Explorer is designed for these circumstances.

characteristic

Cocos2d-JS v3.0 RC0 version of Explorer adds a lot of powerful features, making the process quicker and more convenient updated hot.

  • Multi-threaded parallel download support
  • Two-progress statistics: file-level and byte-level
  • Zip compressed file support
  • http
  • Detailed error reports
  • File download failed retry support

Use Explorer

In fact, use Explorer to use very simple, first of all, you will need the initial application package configuration file in JSON format (manifest).

In this configuration file, you need to provide the address of the corresponding configuration file on the server, the current version of the resource and a set of resource description, then you can use in the game code jsb.AssetsManagerAPI to check or update the latest version of the corresponding resource.

Profiles

The following is a simple example configuration file:

  1.  
    {
  2.  
    "packageUrl" : "http://example.com/assets_manager/TestScene/",
  3.  
    "remoteVersionUrl" : "http://example.com/assets_manager/TestScene/version.manifest",
  4.  
    "remoteManifestUrl" : "http://example.com/assets_manager/TestScene/project.manifest",
  5.  
    "version" : "1.0.0",
  6.  
    "engineVersion" : "Cocos2d-JS v3.0 RC0",
  7.  
     
  8.  
    "assets" : {
  9.  
    "Images/background.jpg" : {
  10.  
    "md5" : "..."
  11.  
    },
  12.  
    "Images/icon.png" : {
  13.  
    "md5" : "..."
  14.  
    },
  15.  
    "Images/button.png" : {
  16.  
    "md5" : "..."
  17.  
    },
  18.  
    "src/game.js" : {
  19.  
    "md5" : "..."
  20.  
    },
  21.  
    "src/layer.js" : {
  22.  
    "md5" : "..."
  23.  
    },
  24.  
    "compressed.zip" : {
  25.  
    "md5" : "...",
  26.  
    "compressed" : true
  27.  
    }
  28.  
    },
  29.  
     
  30.  
    "searchPaths" : [
  31.  
    "res/"
  32.  
    ]
  33.  
    }
  • packageUrl: Download the root path to the remote resource.
  • remoteVersionUrl: path to the remote version of the file, used to determine whether a new version of the server-side resources.
  • remoteManifestUrl: path to the remote configuration file that contains version information, and all information resources.
  • version: profile corresponding version.
  • engineVersion: profile corresponding engine version.
  • assets: All resource information.
    • key: the key represents a resource relative path (relative to packageUrl).
    • md5: md5 version information represents the resource file.
    • compressed: [Optional] If true, the file is downloaded automatically after being decompressed, currently only supports zip compression format.
  • searchPaths: need to add to the search path list cocos2d engine.

Version of the file version.manifestfile should contain exactly the same as the profile of the top five information. This file is optional, if it is not found or success or failure, the Explorer will automatically download the complete profile. But when the configuration file contains a lot of resources are very large, the version of the file will greatly reduce the time version comparison.

Use jsb.AssetsManager

The following is jsb.AssetsManageran example of codes used:

  1.  
    var manager = new jsb.AssetsManager(manifestUrl, storagePath);
  2.  
     
  3.  
    manager.update();
  4.  
    // As the download process is asynchronous, you need to increase the number of index manager to ensure that it will not be freed Cocos2d-x memory management
  5.  
    manager.retain();
  6.  
     
  7.  
    if (!manager.getLocalManifest().isLoaded()) {
  8.  
    cc.log( "Fail to update assets, step skipped.");
  9.  
    }
  10.  
    else {
  11.  
    var listener = new cc.EventListenerAssetsManager(manager, function(event) {
  12.  
    switch (event.getEventCode())
  13.  
    {
  14.  
    case cc.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST:
  15.  
    cc.log( "No local manifest file found, skip assets update.");
  16.  
    break;
  17.  
    case cc.EventAssetsManager.UPDATE_PROGRESSION:
  18.  
    var percent = event.getPercent();
  19.  
    var filePercent = event.getPercentByFile();
  20.  
    cc.log( "Download percent : " + percent + " | File percent : " + filePercent);
  21.  
    break;
  22.  
    case cc.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
  23.  
    case cc.EventAssetsManager.ERROR_PARSE_MANIFEST:
  24.  
    cc.log( "Fail to download manifest file, update skipped.");
  25.  
    break;
  26.  
    case cc.EventAssetsManager.ALREADY_UP_TO_DATE:
  27.  
    case cc.EventAssetsManager.UPDATE_FINISHED:
  28.  
    cc.log( "Update finished.");
  29.  
    // With the addition of the reference count manager, you need at the right time to release it out and make sure that the object is no longer being used to
  30.  
    manager.release();
  31.  
    break;
  32.  
    case cc.EventAssetsManager.UPDATE_FAILED:
  33.  
    cc.log( "Update failed. " + event.getMessage());
  34.  
    // re-direct resources download failed, we suggest you to count the number of retries, giving up more than a certain number of times
  35.  
    manager.downloadFailedAssets();
  36.  
    break;
  37.  
    case cc.EventAssetsManager.ERROR_UPDATING:
  38.  
    cc.log( "Asset update error: " + event.getAssetId() + ", " + event.getMessage());
  39.  
    break;
  40.  
    case cc.EventAssetsManager.ERROR_DECOMPRESS:
  41.  
    cc.log( event.getMessage());
  42.  
    break;
  43.  
    default:
  44.  
    break;
  45.  
    }
  46.  
    }
  47.  
    }

You can also refer to js-teststhe Extensions/AssetsManagerTesttest cases. Other jsb.AssetsManagerAPI for the following:

  • checkUpdate()
  • getState()
  • getStoragePath()
  • getLocalManifest()
  • getRemoteManifest ()

Known bug

Explorer may encounter unable to create and download the file in question on the windows and iOS simulator, we will solve this problem as soon as possible, at the same time, use the iOS-device debugging.

 

Reprinted from: https: //blog.csdn.net/qinning199/article/details/40396545

Guess you like

Origin www.cnblogs.com/wodehao0808/p/11929556.html
Recommended