Home Web Front-end H5 Tutorial HTML5 游戏开发 之 资源加载篇(2)

HTML5 游戏开发 之 资源加载篇(2)

May 17, 2016 am 09:08 AM

       四) 下载过程的管理<br> <br>        4.1) 如何管理成千上百的资源<br> <br>        在游戏开发的过程中,很有可能会有成千上百张图片。最直接的方式,是将这些图片编写在代码中,但是图片的名字很容易改变的,会造成大量的维护工作,甚至影响代码的打包和发布。我的同事Boris,在他的代码演示库中,给出了一个参考实现方式,如下。这种方式,可以保证,在需要修改或者调整资源名称或者路径的时候,不需要接触代码。<br> <br>

  1. {<br>
  2.   "assetRoot": "url/to/assets",<br>
  3.   "bundles": [<br>
  4.   {<br>
  5.         "name": "unique bundle name",<br>
  6.         "contents": [<br>
  7.         "relative/path/to/asset.jpg",<br>
  8.         "another/asset.mp3"<br>
  9.           ]<br>
  10.   },<br>
  11.   "autoDownload": true<br>
  12. }<br>
  13. var gal = new GameAssetLoader("http://path.to/gal.manifest");<br>
  14. // Load the GAL. If manifest indicates autoDownload, this call will<br>
  15. // start loading assets one by one.<br>
  16. gal.init(function() {<br>
  17. // Called when the library is initialized<br>
  18. });
复制代码
<br>        更完整的代码,可以参考GitHub上的源代码<br> <br>        4.2) 如何实现批处理的下载<br> <br>        再获得了资源列表之后,就要开始资源的下载。显然,需要这样的方法。<br> <br>
  1. AssetManager.prototype.downloadAll = function(downloadCallback) {<br>
  2.   for (var i = 0; i
  3.   var path = this.downloadQueue[i];<br>
  4.   var img = new Image();<br>
  5.   var that = this;<br>
  6.   img.addEventListener("load", function() {<br>
  7.         // coming soon<br>
  8.   }, false);<br>
  9.   img.addEventListener("error", function() {<br>
  10.   // coming soon<br>
  11.   }, false);<br>
  12.   img.src = path;<br>
  13. }<br>
  14. }<br>
  15. <br>
    
    Copy after login
  16. 下载的过程中,一般情况下都需要一个进度条,来显示完成的情况,所以必须对AssetManager进行计数。<br>
  17. <br>
  18. <br>
  19. <br>
    
    Copy after login
  20. function AssetManager() {<br>
  21.   this.successCount = 0;<br>
  22.   this.errorCount = 0;<br>
  23.   this.downloadQueue = [];<br>
  24. }<br>
  25. <br>
  26. AssetManager.prototype.isDone = function() {<br>
  27.   return (this.downloadQueue.length == this.successCount + this.errorCount);<br>
  28. }<br>
  29. AssetManager.prototype.getProcess = function() {<br>
  30.   return (this.successCount + this.errorCount)/this.downloadQueue.length;<br>
  31. }
复制代码
<br>        显然,也需要对每个img的load和error事件,进行计数。还请注意downloadAll函数有个参数叫做downloadCallback,在资源下载完成以后,通知此函数,进入游戏过程中。<br> <br>
  1. img.addEventListener("load", function() {<br>
  2.   that.successCount += 1;<br>
  3.   if (that.isDone()) {<br>
  4.         downloadCallback();<br>
  5.   }<br>
  6. }, false);<br>
  7. img.addEventListener("error", function() {<br>
  8.   that.errorCount += 1;<br>
  9.   if (that.isDone()) {<br>
  10.         downloadCallback();<br>
  11.   }<br>
  12. }, false
复制代码
<br>        4.3) 游戏中的不同关卡<br> <br>        游戏通常是分关卡的,完全没有必要在一开始就将游戏的所有资源下载到本地,毕竟不是每个玩家都会将游戏通关。为了按需下载,比较完备的资源加载器,应该可以对每个资源配上一个标签或者属性,可以标志它属于哪一关。每一关的开始,只下载和本关相关联的资源,在每一关结束的时候,在去下载下一关的资源。不仅减少用户的不必要的等待时间,还可以有效的减少服务器的压力。<br> <br>        5.资源加载器的具体实现<br> <br>        5.1 PreloadJS<br> <br>        官方网站:http://www.createjs.com/#!/PreloadJS/download<br> <br>        开源代码:https://github.com/CreateJS/PreloadJS/<br> <br>        专门用于资源下载的类库,非常好用,考虑的也非常全面,首先推荐的一款软件,尤其是读者不希望加载特别大的游戏引擎是,这款软件可以作为首选。<br> <br>        具体的例子可以参考:https://github.com/CreateJS/PreloadJS/tree/master/examples<br> <br> (未完待续)<br> <br>
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run the h5 project How to run the h5 project Apr 06, 2025 pm 12:21 PM

Running the H5 project requires the following steps: installing necessary tools such as web server, Node.js, development tools, etc. Build a development environment, create project folders, initialize projects, and write code. Start the development server and run the command using the command line. Preview the project in your browser and enter the development server URL. Publish projects, optimize code, deploy projects, and set up web server configuration.

What exactly does H5 page production mean? What exactly does H5 page production mean? Apr 06, 2025 am 07:18 AM

H5 page production refers to the creation of cross-platform compatible web pages using technologies such as HTML5, CSS3 and JavaScript. Its core lies in the browser's parsing code, rendering structure, style and interactive functions. Common technologies include animation effects, responsive design, and data interaction. To avoid errors, developers should be debugged; performance optimization and best practices include image format optimization, request reduction and code specifications, etc. to improve loading speed and code quality.

How to make h5 click icon How to make h5 click icon Apr 06, 2025 pm 12:15 PM

The steps to create an H5 click icon include: preparing a square source image in the image editing software. Add interactivity in the H5 editor and set the click event. Create a hotspot that covers the entire icon. Set the action of click events, such as jumping to the page or triggering animation. Export H5 documents as HTML, CSS, and JavaScript files. Deploy the exported files to a website or other platform.

What is the H5 programming language? What is the H5 programming language? Apr 03, 2025 am 12:16 AM

H5 is not a standalone programming language, but a collection of HTML5, CSS3 and JavaScript for building modern web applications. 1. HTML5 defines the web page structure and content, and provides new tags and APIs. 2. CSS3 controls style and layout, and introduces new features such as animation. 3. JavaScript implements dynamic interaction and enhances functions through DOM operations and asynchronous requests.

What application scenarios are suitable for H5 page production What application scenarios are suitable for H5 page production Apr 05, 2025 pm 11:36 PM

H5 (HTML5) is suitable for lightweight applications, such as marketing campaign pages, product display pages and corporate promotion micro-websites. Its advantages lie in cross-platformity and rich interactivity, but its limitations lie in complex interactions and animations, local resource access and offline capabilities.

How to make pop-up windows with h5 How to make pop-up windows with h5 Apr 06, 2025 pm 12:12 PM

H5 pop-up window creation steps: 1. Determine the triggering method (click, time, exit, scroll); 2. Design content (title, text, action button); 3. Set style (size, color, font, background); 4. Implement code (HTML, CSS, JavaScript); 5. Test and deployment.

Is H5 page production a front-end development? Is H5 page production a front-end development? Apr 05, 2025 pm 11:42 PM

Yes, H5 page production is an important implementation method for front-end development, involving core technologies such as HTML, CSS and JavaScript. Developers build dynamic and powerful H5 pages by cleverly combining these technologies, such as using the &lt;canvas&gt; tag to draw graphics or using JavaScript to control interaction behavior.

What Does H5 Refer To? Exploring the Context What Does H5 Refer To? Exploring the Context Apr 12, 2025 am 12:03 AM

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

See all articles