sprite_lib.js 1.7 KB

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