1 goog.provide('lime.animation.Loop'); 2 3 4 goog.require('goog.math.Coordinate'); 5 goog.require('lime.Sprite'); 6 goog.require('lime.animation.Animation'); 7 8 /** 9 * Loop animation again after it has finished 10 * @constructor 11 * @param {lime.animation.Animation} action Animation to loop. 12 * @extends lime.animation.Animation 13 */ 14 lime.animation.Loop = function(action) { 15 16 lime.animation.Animation.call(this); 17 18 this.action_ = action; 19 goog.events.listen(action, lime.animation.Event.STOP, 20 this.handleActionEnd_, false, this); 21 22 this.setLimit(0); 23 24 this.timesRun_ = 0; 25 26 this.playing_ = 0; 27 28 this.setDuration(action.duration_); 29 30 }; 31 goog.inherits(lime.animation.Loop, lime.animation.Animation); 32 33 /** 34 * @inheritDoc 35 * @see lime.animation.Animation#makeTargetProp 36 */ 37 lime.animation.Loop.prototype.initTarget = function(target) { 38 lime.animation.Animation.prototype.initTarget.call(this, target); 39 40 this.setDuration(this.action_.duration_); 41 }; 42 43 /** 44 * Return the run limit value for animation. 0 means no limit. 45 * @return {number} Limit. 46 */ 47 lime.animation.Loop.prototype.getLimit = function() { 48 return this.limit_; 49 }; 50 51 /** 52 * Set new run limit for animation. 0 means no limit/infinity. 53 * @param {number} value New limit value. 54 * @return {lime.animation.Loop} object itself. 55 */ 56 lime.animation.Loop.prototype.setLimit = function(value) { 57 this.limit_ = value; 58 this.timesRun_ = 0; 59 return this; 60 }; 61 62 /** 63 * @inheritDoc 64 * @see lime.animation.Animation#play 65 */ 66 lime.animation.Loop.prototype.play = function() { 67 this.action_.play(); 68 this.playing_ = 1; 69 this.dispatchEvent({type: lime.animation.Event.START}); 70 }; 71 72 /** 73 * @inheritDoc 74 * @see lime.animation.Animation#stop 75 */ 76 lime.animation.Loop.prototype.stop = function() { 77 this.playing_ = 0; 78 this.action_.stop(); 79 this.dispatchEvent({type: lime.animation.Event.STOP}); 80 81 }; 82 83 /** 84 * Handle action end. Start new action or stop if limit reached. 85 * @private 86 */ 87 lime.animation.Loop.prototype.handleActionEnd_ = function() { 88 this.timesRun_++; 89 if (this.playing_ && (!this.limit_ || this.timesRun_ < this.limit_)) { 90 this.action_.play(); 91 } 92 }; 93 94 /** 95 * @inheritDoc 96 * @see lime.animation.Animation#addTarget 97 */ 98 lime.animation.Loop.prototype.addTarget = function(target) { 99 this.action_.addTarget(target); 100 }; 101 102 /** 103 * @inheritDoc 104 * @see lime.animation.Animation#removeTarget 105 */ 106 lime.animation.Loop.prototype.removeTarget = function(target) { 107 this.action_.removeTarget(target); 108 }; 109