HTML5 game development resource loading (2 )-

Source: Internet
Author: User
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.

  1. {
  2. "AssetRoot": "url/to/assets ",
  3. "Bundles ":[
  4. {
  5. "Name": "unique bundle name ",
  6. "Contents ":[
  7. "Relative/path/to/asset.jpg ",
  8. "Another/assetpipeline"
  9. ]
  10. },
  11. "AutoDownload": true
  12. }
  13. Var gal = new GameAssetLoader ("http://path.to/gal.manifest ");
  14. // Load the GAL. If manifest indicates autoDownload, this call will
  15. // Start loading assets one by one.
  16. Gal. init (function (){
  17. // Called when the library is initialized
  18. });


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.

  1. AssetManager. prototype. downloadAll = function (downloadCallback ){
  2. For (var I = 0; I <this. downloadQueue. length; I ++ ){
  3. Var path = this. downloadQueue [I];
  4. Var img = new Image ();
  5. Var that = this;
  6. Img. addEventListener ("load", function (){
  7. // Coming soon
  8. }, False );
  9. Img. addEventListener ("error", function (){
  10. // Coming soon
  11. }, False );
  12. Img. src = path;
  13. }
  14. }

  15. During the download process, a progress bar is usually required to display the progress. Therefore, AssetManager must be counted.



  16. Function AssetManager (){
  17. This. successCount = 0;
  18. This. errorCount = 0;
  19. This. downloadQueue = [];
  20. }

  21. AssetManager. prototype. isDone = function (){
  22. Return (this. downloadQueue. length = this. successCount + this. errorCount );
  23. }
  24. AssetManager. prototype. getProcess = function (){
  25. Return (this. successCount + this. errorCount)/this. downloadQueue. length;
  26. }


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.

  1. Img. addEventListener ("load", function (){
  2. That. successCount + = 1;
  3. If (that. isDone ()){
  4. DownloadCallback ();
  5. }
  6. }, False );
  7. Img. addEventListener ("error", function (){
  8. That. errorCount + = 1;
  9. If (that. isDone ()){
  10. DownloadCallback ();
  11. }
  12. }, 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.