sprite_lib.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. function CSpriteLibrary(){
  2. var _oLibSprites;
  3. var _iNumSprites;
  4. var _iCntSprites;
  5. var _cbCompleted;
  6. var _cbTotalCompleted;
  7. var _cbOwner;
  8. this.init = function( cbCompleted,cbTotalCompleted, cbOwner ){
  9. _iNumSprites = 0;
  10. _iCntSprites = 0;
  11. _cbCompleted = cbCompleted;
  12. _cbTotalCompleted = cbTotalCompleted;
  13. _cbOwner = cbOwner;
  14. _oLibSprites = {};
  15. };
  16. this.addSprite = function( szKey, szPath ){
  17. if ( _oLibSprites.hasOwnProperty(szKey) ){
  18. return;
  19. }
  20. _oLibSprites[szKey] = { szPath:szPath, oSprite: new Image() };
  21. _iNumSprites++;
  22. };
  23. this.getSprite = function( szKey ){
  24. if (!_oLibSprites.hasOwnProperty(szKey)){
  25. return null;
  26. }else{
  27. return _oLibSprites[szKey].oSprite;
  28. }
  29. };
  30. this._onSpritesLoaded = function(){
  31. _cbTotalCompleted.call(_cbOwner);
  32. };
  33. this._onSpriteLoaded = function(){
  34. _cbCompleted.call(_cbOwner);
  35. if (++_iCntSprites === _iNumSprites) {
  36. this._onSpritesLoaded();
  37. }
  38. };
  39. this.loadSprites = function(){
  40. for (var szKey in _oLibSprites) {
  41. _oLibSprites[szKey].oSprite["oSpriteLibrary"] = this;
  42. _oLibSprites[szKey].oSprite.onload = function(){
  43. this.oSpriteLibrary._onSpriteLoaded();
  44. };
  45. _oLibSprites[szKey].oSprite.src = _oLibSprites[szKey].szPath;
  46. }
  47. };
  48. this.getNumSprites=function(){
  49. return _iNumSprites;
  50. };
  51. }