Skip to content

Instantly share code, notes, and snippets.

@piayo
Last active December 15, 2015 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piayo/5220405 to your computer and use it in GitHub Desktop.
Save piayo/5220405 to your computer and use it in GitHub Desktop.
PIXI.MovieClip.prototype.setTickFrame / PIXI.MovieClip.prototype.removeTickFrame
PIXI.MovieClip = function(a) {
PIXI.Sprite.call(this, a[0]);
this.textures = a;
this.currentFrame = 0;
this.animationSpeed = 1;
//added:start--------------------
this._now = null;
this._interval = 0;
this._lastTime = 0;
this._tick = null;
//added:end--------------------
};
//* noneChanged
PIXI.MovieClip.constructor = PIXI.MovieClip;
PIXI.MovieClip.prototype = Object.create(PIXI.Sprite.prototype);
PIXI.MovieClip.prototype.stop = function() {
this.playing = !1
};
PIXI.MovieClip.prototype.play = function() {
this.playing = !0
};
PIXI.MovieClip.prototype.gotoAndStop = function(a) {
this.playing = !1;
this.currentFrame = a;
this.setTexture(this.textures[(this.currentFrame + 0.5 | 0) % this.textures.length])
};
PIXI.MovieClip.prototype.gotoAndPlay = function(a) {
this.currentFrame = a;
this.playing = !0
};
// noneChanged */
//added:start--------------------
PIXI.MovieClip.prototype.setTickFrame = function(func, itv){
this._now = window.performance && (performance.now || performance.mozNow || performance.msNow || performance.oNow || performance.webkitNow) || null;
this._interval = itv;
this._tick = func;
};
PIXI.MovieClip.prototype.removeTickFrame = function(){
this._interval = 0;
this._tick = null;
};
PIXI.MovieClip.prototype._getTime = function() {
return (this._now&&this._now.call(performance))||(new Date().getTime());
}
//added:end--------------------
PIXI.MovieClip.prototype.updateTransform = function() {
PIXI.Sprite.prototype.updateTransform.call(this);
if(!this.playing)return;
this.currentFrame += this.animationSpeed;
var round = (this.currentFrame + 0.5) | 0;
this.setTexture(this.textures[round % this.textures.length]);
//added:start--------------------
if(this._tick){
var time = this._getTime();
if (this._getTime() - this._lastTime >= (this._interval-1)*0.97) {
this._tick();
this._lastTime = time;
}
}
//added:end--------------------
};
@piayo
Copy link
Author

piayo commented Mar 22, 2013

//sample:
//MC.setTickFrame(function, interval)

var count = 0;
MC.setTickFrame(function(){
console.log("tick! -> " + count);
if(count > 2){
console.log("tick -> removed")
this.removeTickFrame();
}
count++;
}, 500);

//log was
//tick! -> 0
//tick! -> 1
//tick! -> 2
//tick! -> 3
//tick -> removed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment