search
HomeWeb Front-endH5 TutorialHTML5 游戏开发 之 资源加载篇(2)

       四) 下载过程的管理<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>
    
  16. 下载的过程中,一般情况下都需要一个进度条,来显示完成的情况,所以必须对AssetManager进行计数。<br>
  17. <br>
  18. <br>
  19. <br>
    
  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
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
H5: Tools, Frameworks, and Best PracticesH5: Tools, Frameworks, and Best PracticesApr 11, 2025 am 12:11 AM

The tools and frameworks that need to be mastered in H5 development include Vue.js, React and Webpack. 1.Vue.js is suitable for building user interfaces and supports component development. 2.React optimizes page rendering through virtual DOM, suitable for complex applications. 3.Webpack is used for module packaging and optimize resource loading.

The Legacy of HTML5: Understanding H5 in the PresentThe Legacy of HTML5: Understanding H5 in the PresentApr 10, 2025 am 09:28 AM

HTML5hassignificantlytransformedwebdevelopmentbyintroducingsemanticelements,enhancingmultimediasupport,andimprovingperformance.1)ItmadewebsitesmoreaccessibleandSEO-friendlywithsemanticelementslike,,and.2)HTML5introducednativeandtags,eliminatingthenee

H5 Code: Accessibility and Semantic HTMLH5 Code: Accessibility and Semantic HTMLApr 09, 2025 am 12:05 AM

H5 improves web page accessibility and SEO effects through semantic elements and ARIA attributes. 1. Use, etc. to organize the content structure and improve SEO. 2. ARIA attributes such as aria-label enhance accessibility, and assistive technology users can use web pages smoothly.

Is h5 same as HTML5?Is h5 same as HTML5?Apr 08, 2025 am 12:16 AM

"h5" and "HTML5" are the same in most cases, but they may have different meanings in certain specific scenarios. 1. "HTML5" is a W3C-defined standard that contains new tags and APIs. 2. "h5" is usually the abbreviation of HTML5, but in mobile development, it may refer to a framework based on HTML5. Understanding these differences helps to use these terms accurately in your project.

What is the function of H5?What is the function of H5?Apr 07, 2025 am 12:10 AM

H5, or HTML5, is the fifth version of HTML. It provides developers with a stronger tool set, making it easier to create complex web applications. The core functions of H5 include: 1) elements that allow drawing graphics and animations on web pages; 2) semantic tags such as, etc. to make the web page structure clear and conducive to SEO optimization; 3) new APIs such as GeolocationAPI support location-based services; 4) Cross-browser compatibility needs to be ensured through compatibility testing and Polyfill library.

How to do h5 linkHow to do h5 linkApr 06, 2025 pm 12:39 PM

How to create an H5 link? Determine the link target: Get the URL of the H5 page or application. Create HTML anchors: Use the <a> tag to create an anchor and specify the link target URL. Set link properties (optional): Set target, title, and onclick properties as needed. Add to webpage: Add HTML anchor code to the webpage where you want the link to appear.

How to solve the h5 compatibility problemHow to solve the h5 compatibility problemApr 06, 2025 pm 12:36 PM

Solutions to H5 compatibility issues include: using responsive design that allows web pages to adjust layouts according to screen size. Use cross-browser testing tools to test compatibility before release. Use Polyfill to provide support for new APIs for older browsers. Follow web standards and use effective code and best practices. Use CSS preprocessors to simplify CSS code and improve readability. Optimize images, reduce web page size and speed up loading. Enable HTTPS to ensure the security of the website.

How to generate links with h5How to generate links with h5Apr 06, 2025 pm 12:33 PM

h5 pages can generate links in two ways: create links manually or use short link services. By manually creating, you just need to copy the URL of the h5 page; through the short link service, you need to paste the URL into the service and then get the shortened URL.

See all articles

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.