During game development, it is very likely that there will be thousands or even hundreds of images. The most direct method is to write these images in the code, but the image name is easy to change, which may cause a lot of maintenance work, or even affect ......,.
4) download process management
4.1) How to manage thousands of resources
During game development, it is very likely that there will be thousands or even hundreds of images. The most direct method is to write these images in the code, but the image name is easy to change, resulting in a lot of maintenance work, and even affect the packaging and release of the Code. My colleague Boris provides a reference implementation method in his code demo library. This method ensures that you do not need to access the code when you need to modify or adjust the Resource Name or path.
- {
- "AssetRoot": "url/to/assets ",
- "Bundles ":[
- {
- "Name": "unique bundle name ",
- "Contents ":[
- "Relative/path/to/asset.jpg ",
- "Another/assetpipeline"
- ]
- },
- "AutoDownload": true
- }
- Var gal = new GameAssetLoader ("http://path.to/gal.manifest ");
- // Load the GAL. If manifest indicates autoDownload, this call will
- // Start loading assets one by one.
- Gal. init (function (){
- // Called when the library is initialized
- });
For more complete code, refer to the source code on GitHub.
4.2) How to Implement batch download
After obtaining the resource list, download the resource. Obviously, this method is required.
- AssetManager. prototype. downloadAll = function (downloadCallback ){
- For (var I = 0; I <this. downloadQueue. length; I ++ ){
- Var path = this. downloadQueue [I];
- Var img = new Image ();
- Var that = this;
- Img. addEventListener ("load", function (){
- // Coming soon
- }, False );
- Img. addEventListener ("error", function (){
- // Coming soon
- }, False );
- Img. src = path;
- }
- }
-
- During the download process, a progress bar is usually required to display the progress. Therefore, AssetManager must be counted.
-
- Function AssetManager (){
- This. successCount = 0;
- This. errorCount = 0;
- This. downloadQueue = [];
- }
- AssetManager. prototype. isDone = function (){
- Return (this. downloadQueue. length = this. successCount + this. errorCount );
- }
- AssetManager. prototype. getProcess = function (){
- Return (this. successCount + this. errorCount)/this. downloadQueue. length;
- }
Obviously, you also need to count the load and error events of each img. Please note that the downloadAll function has a parameter called downloadCallback. After the resource download is complete, notify this function to enter the game.
- Img. addEventListener ("load", function (){
- That. successCount + = 1;
- If (that. isDone ()){
- DownloadCallback ();
- }
- }, False );
- Img. addEventListener ("error", function (){
- That. errorCount + = 1;
- If (that. isDone ()){
- DownloadCallback ();
- }
- }, False
4.3) different levels in the game
Games are usually divided into checkpoints, and there is no need to download all the resources of the game to the local place from the very beginning. After all, not every player will pass the game through customs. In order to download resources as needed, a complete resource loader should be able to add a tag or attribute to each resource to indicate which level it belongs. At the beginning of each level, only the resources associated with this level are downloaded. At the end of each level, the resources of the next level are downloaded. It not only reduces unnecessary waiting time, but also effectively reduces the pressure on the server.
5. Implementation of the resource Loader
5.1 PreloadJS
Official Website: http://www.createjs.com /#! /PreloadJS/download
Open source code: https://github.com/CreateJS/PreloadJS/
Class Libraries dedicated for resource download are very easy to use and fully considered. First, we recommend a software, especially when readers do not want to load a particularly large game engine, this software can be used as the first choice.
Specific examples can refer to: https://github.com/CreateJS/PreloadJS/tree/master/examples
(To be continued)