2012-12-01 12:04:10 -08:00
|
|
|
(function() {
|
|
|
|
/**
|
|
|
|
* Sprite constructor
|
|
|
|
* @constructor
|
|
|
|
* @augments Kinetic.Shape
|
|
|
|
* @param {Object} config
|
|
|
|
*/
|
|
|
|
Kinetic.Sprite = function(config) {
|
|
|
|
this._initSprite(config);
|
|
|
|
};
|
|
|
|
|
|
|
|
Kinetic.Sprite.prototype = {
|
|
|
|
_initSprite: function(config) {
|
|
|
|
this.setDefaultAttrs({
|
|
|
|
index: 0,
|
|
|
|
frameRate: 17
|
|
|
|
});
|
|
|
|
this.shapeType = "Sprite";
|
|
|
|
|
|
|
|
// call super constructor
|
|
|
|
Kinetic.Shape.call(this, config);
|
|
|
|
this._setDrawFuncs();
|
|
|
|
|
|
|
|
this.anim = new Kinetic.Animation();
|
|
|
|
var that = this;
|
|
|
|
this.on('animationChange', function() {
|
|
|
|
// reset index when animation changes
|
|
|
|
that.setIndex(0);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
drawFunc: function(context) {
|
|
|
|
var anim = this.attrs.animation;
|
|
|
|
var index = this.attrs.index;
|
|
|
|
var f = this.attrs.animations[anim][index];
|
2012-12-06 20:11:14 -08:00
|
|
|
var width = f.width || this.attrs.width;
|
|
|
|
var height = f.height || this.attrs.height;
|
2012-12-01 12:04:10 -08:00
|
|
|
|
|
|
|
if(this.attrs.image) {
|
2012-12-05 18:43:58 +00:00
|
|
|
this.drawImage(context, this.attrs.image, f.x, f.y, f.width, f.height, 0, 0, width, height);
|
2012-12-01 12:04:10 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
drawHitFunc: function(context) {
|
|
|
|
var anim = this.attrs.animation;
|
|
|
|
var index = this.attrs.index;
|
|
|
|
var f = this.attrs.animations[anim][index];
|
2012-12-06 20:16:52 -08:00
|
|
|
var width = f.width || this.attrs.width;
|
|
|
|
var height = f.height || this.attrs.height;
|
2012-12-05 18:43:58 +00:00
|
|
|
|
2012-07-28 10:46:16 -07:00
|
|
|
context.beginPath();
|
2012-12-05 18:43:58 +00:00
|
|
|
context.rect(0, 0, width, height);
|
2012-07-28 10:46:16 -07:00
|
|
|
context.closePath();
|
2012-12-06 20:16:52 -08:00
|
|
|
this.fill(context);
|
2012-12-01 12:04:10 -08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* start sprite animation
|
|
|
|
* @name start
|
|
|
|
* @methodOf Kinetic.Sprite.prototype
|
|
|
|
*/
|
|
|
|
start: function() {
|
|
|
|
var that = this;
|
|
|
|
var layer = this.getLayer();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* animation object has no executable function because
|
|
|
|
* the updates are done with a fixed FPS with the setInterval
|
|
|
|
* below. The anim object only needs the layer reference for
|
|
|
|
* redraw
|
|
|
|
*/
|
|
|
|
this.anim.node = layer;
|
|
|
|
|
|
|
|
this.interval = setInterval(function() {
|
|
|
|
var index = that.attrs.index;
|
|
|
|
that._updateIndex();
|
|
|
|
if(that.afterFrameFunc && index === that.afterFrameIndex) {
|
|
|
|
that.afterFrameFunc();
|
|
|
|
delete that.afterFrameFunc;
|
|
|
|
delete that.afterFrameIndex;
|
|
|
|
}
|
|
|
|
}, 1000 / this.attrs.frameRate);
|
2012-07-28 10:46:16 -07:00
|
|
|
|
2012-12-01 12:04:10 -08:00
|
|
|
this.anim.start();
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* stop sprite animation
|
|
|
|
* @name stop
|
|
|
|
* @methodOf Kinetic.Sprite.prototype
|
|
|
|
*/
|
|
|
|
stop: function() {
|
|
|
|
this.anim.stop();
|
|
|
|
clearInterval(this.interval);
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* set after frame event handler
|
|
|
|
* @name afterFrame
|
|
|
|
* @methodOf Kinetic.Sprite.prototype
|
|
|
|
* @param {Integer} index frame index
|
|
|
|
* @param {Function} func function to be executed after frame has been drawn
|
|
|
|
*/
|
|
|
|
afterFrame: function(index, func) {
|
|
|
|
this.afterFrameIndex = index;
|
|
|
|
this.afterFrameFunc = func;
|
|
|
|
},
|
|
|
|
_updateIndex: function() {
|
|
|
|
var i = this.attrs.index;
|
|
|
|
var a = this.attrs.animation;
|
|
|
|
if(i < this.attrs.animations[a].length - 1) {
|
|
|
|
this.attrs.index++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.attrs.index = 0;
|
|
|
|
}
|
2012-07-28 10:46:16 -07:00
|
|
|
}
|
2012-12-01 12:04:10 -08:00
|
|
|
};
|
|
|
|
Kinetic.Global.extend(Kinetic.Sprite, Kinetic.Shape);
|
|
|
|
|
|
|
|
// add getters setters
|
|
|
|
Kinetic.Node.addGettersSetters(Kinetic.Sprite, ['animation', 'animations', 'index']);
|
|
|
|
|
2012-07-26 22:58:38 -07:00
|
|
|
/**
|
2012-12-01 12:04:10 -08:00
|
|
|
* set animation key
|
|
|
|
* @name setAnimation
|
2012-07-26 22:58:38 -07:00
|
|
|
* @methodOf Kinetic.Sprite.prototype
|
2012-12-01 12:04:10 -08:00
|
|
|
* @param {String} anim animation key
|
2012-07-26 22:58:38 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2012-12-01 12:04:10 -08:00
|
|
|
* set animations object
|
|
|
|
* @name setAnimations
|
2012-07-26 22:58:38 -07:00
|
|
|
* @methodOf Kinetic.Sprite.prototype
|
2012-12-01 12:04:10 -08:00
|
|
|
* @param {Object} animations
|
2012-07-26 22:58:38 -07:00
|
|
|
*/
|
2012-12-01 12:04:10 -08:00
|
|
|
|
2012-07-26 22:58:38 -07:00
|
|
|
/**
|
2012-12-01 12:04:10 -08:00
|
|
|
* set animation frame index
|
|
|
|
* @name setIndex
|
2012-07-26 22:58:38 -07:00
|
|
|
* @methodOf Kinetic.Sprite.prototype
|
|
|
|
* @param {Integer} index frame index
|
|
|
|
*/
|
2012-12-01 12:04:10 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* get animation key
|
|
|
|
* @name getAnimation
|
|
|
|
* @methodOf Kinetic.Sprite.prototype
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get animations object
|
|
|
|
* @name getAnimations
|
|
|
|
* @methodOf Kinetic.Sprite.prototype
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get animation frame index
|
|
|
|
* @name getIndex
|
|
|
|
* @methodOf Kinetic.Sprite.prototype
|
|
|
|
*/
|
|
|
|
})();
|