English subtitles

← Caching images - HTML5 Game Development

Get Embed Code
4 Languages

Showing Revision 6 created 05/25/2016 by Udacity Robot.

  1. Let's take a look at our previous code to load an image. Now remember, it was
  2. three lines of code to create an image, set a callback, and then define the
  3. source. The first steps in writing an asset manager would be to cache this
  4. image once it's loaded so that duplicate calls won't issue a reload, but rather
  5. would return the image we've already loaded in memory. To do this, you're going
  6. to implement the concept of a cache. What will happen is, the first request
  7. will go through, and the data will be loaded. When it's loaded, you add it to
  8. the cache using some key value, like its name, so that subsequent requests,
  9. rather than going out to disk, or to the web, will check the cache first, and
  10. return the already loaded asset. What I'd like you to do is to modify the
  11. following code so the asset loader checks the cache before attempting to load
  12. the image. Now there's a couple things to point out here, is that gCachedAssets
  13. is actually a dictionary object, which means that you need to specify a key and
  14. value when accessing and referencing to it. Also take note, that here in the
  15. img.onload callback is where you need to do your heavy lifting to push your
  16. data into this cache object. Good luck.