babylon.sprite.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. var BABYLON = BABYLON || {};
  2. (function () {
  3. BABYLON.Sprite = function (name, manager) {
  4. this.name = name;
  5. this._manager = manager;
  6. this._manager.sprites.push(this);
  7. this.position = BABYLON.Vector3.Zero();
  8. this.color = new BABYLON.Color4(1.0, 1.0, 1.0, 1.0);
  9. this._frameCount = 0;
  10. };
  11. // Members
  12. BABYLON.Sprite.prototype.position = null;
  13. BABYLON.Sprite.prototype.size = 1.0;
  14. BABYLON.Sprite.prototype.angle = 0;
  15. BABYLON.Sprite.prototype.cellIndex = 0;
  16. BABYLON.Sprite.prototype.invertU = 0;
  17. BABYLON.Sprite.prototype.invertV = 0;
  18. BABYLON.Sprite.prototype.disposeWhenFinishedAnimating = false;
  19. BABYLON.Sprite.prototype._animationStarted = false;
  20. BABYLON.Sprite.prototype._loopAnimation = false;
  21. BABYLON.Sprite.prototype._fromIndex = false;
  22. BABYLON.Sprite.prototype._toIndex = false;
  23. BABYLON.Sprite.prototype._delay = false;
  24. BABYLON.Sprite.prototype._direction = 1;
  25. // Methods
  26. BABYLON.Sprite.prototype.playAnimation = function (from, to, loop, delay) {
  27. this._fromIndex = from;
  28. this._toIndex = to;
  29. this._loopAnimation = loop;
  30. this._delay = delay;
  31. this._animationStarted = true;
  32. this._direction = from < to ? 1 : -1;
  33. this.cellIndex = from;
  34. this._time = 0;
  35. };
  36. BABYLON.Sprite.prototype.stopAnimation = function() {
  37. this._animationStarted = false;
  38. };
  39. BABYLON.Sprite.prototype._animate = function (deltaTime) {
  40. if (!this._animationStarted)
  41. return;
  42. this._time += deltaTime;
  43. if (this._time > this._delay) {
  44. this._time = this._time % this._delay;
  45. this.cellIndex += this._direction;
  46. if (this.cellIndex == this._toIndex) {
  47. if (this._loopAnimation) {
  48. this.cellIndex = this._fromIndex;
  49. } else {
  50. this._animationStarted = false;
  51. if (this.disposeWhenFinishedAnimating) {
  52. this.dispose();
  53. }
  54. }
  55. }
  56. }
  57. }
  58. BABYLON.Sprite.prototype.dispose = function() {
  59. for (var i = 0; i < this._manager.sprites.length; i++) {
  60. if (this._manager.sprites[i] == this) {
  61. this._manager.sprites.splice(i, 1);
  62. }
  63. }
  64. }
  65. })();