babylon.sprite.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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._frameCount = 0;
  9. };
  10. // Members
  11. BABYLON.Sprite.prototype.position = null;
  12. BABYLON.Sprite.prototype.size = 1.0;
  13. BABYLON.Sprite.prototype.angle = 0;
  14. BABYLON.Sprite.prototype.cellIndex = 0;
  15. BABYLON.Sprite.prototype.invertU = 0;
  16. BABYLON.Sprite.prototype.invertV = 0;
  17. BABYLON.Sprite.prototype._animationStarted = false;
  18. BABYLON.Sprite.prototype._loopAnimation = false;
  19. BABYLON.Sprite.prototype._fromIndex = false;
  20. BABYLON.Sprite.prototype._toIndex = false;
  21. BABYLON.Sprite.prototype._delay = false;
  22. BABYLON.Sprite.prototype._direction = 1;
  23. // Methods
  24. BABYLON.Sprite.prototype.playAnimation = function (from, to, loop, delay) {
  25. this._fromIndex = from;
  26. this._toIndex = to;
  27. this._loopAnimation = loop;
  28. this._delay = delay;
  29. this._animationStarted = true;
  30. this._direction = from < to ? 1 : -1;
  31. this.cellIndex = from;
  32. this._time = 0;
  33. };
  34. BABYLON.Sprite.prototype.stopAnimation = function() {
  35. this._animationStarted = false;
  36. };
  37. BABYLON.Sprite.prototype._animate = function (deltaTime) {
  38. if (!this._animationStarted)
  39. return;
  40. this._time += deltaTime;
  41. if (this._time > this._delay) {
  42. this._time = this._time % this._delay;
  43. this.cellIndex += this._direction;
  44. if (this.cellIndex == this._toIndex) {
  45. if (this._loopAnimation) {
  46. this.cellIndex = this._fromIndex;
  47. } else {
  48. this._animationStarted = false;
  49. }
  50. }
  51. }
  52. }
  53. })();