babylon.sprite.js 2.4 KB

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