Created
November 4, 2013 12:26
-
-
Save raldred/7301769 to your computer and use it in GitHub Desktop.
Simple interval timer for IGE based on the engine's tickDelta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var IntervalTimer; | |
IntervalTimer = IgeEntity.extend({ | |
classId: 'IntervalTimer', | |
init: function() { | |
IgeEntity.prototype.init.call(this); | |
this._acc = 0; | |
this._duration = 0; | |
this._callback = function() { | |
return this.log('no callback provided'); | |
}; | |
this._paused = true; | |
return this.mount(ige._currentViewport); | |
}, | |
duration: function(val) { | |
if (typeof val !== 'undefined') { | |
this._duration = val; | |
return this; | |
} else { | |
return this._duration; | |
} | |
}, | |
"do": function(val) { | |
if (typeof val !== 'undefined') { | |
this._callback = val; | |
return this; | |
} else { | |
return this._callback; | |
} | |
}, | |
pause: function(val) { | |
return this._paused = !!val; | |
}, | |
stop: function() { | |
this._paused = true; | |
return this.reset(); | |
}, | |
start: function() { | |
this._paused = false; | |
return this._callback.call(this); | |
}, | |
reset: function() { | |
this._acc = 0; | |
return this._paused = true; | |
}, | |
tick: function(ctx) { | |
if (this._paused) { | |
return; | |
} | |
this._acc += ige._tickDelta; | |
if (this._acc > this._duration) { | |
this._acc -= this._duration; | |
this._callback.call(this); | |
} | |
return IgeEntity.prototype.tick.call(this, ctx); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment