2012-05-02 15:35:32 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
// Sprite
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
2012-07-04 03:07:27 +08:00
|
|
|
Kinetic.Sprite = Kinetic.Shape.extend({
|
|
|
|
/**
|
|
|
|
* Sprite constructor
|
|
|
|
* @constructor
|
|
|
|
* @augments Kinetic.Shape
|
|
|
|
* @param {Object} config
|
|
|
|
*/
|
|
|
|
init: function(config) {
|
|
|
|
this.setDefaultAttrs({
|
|
|
|
index: 0,
|
|
|
|
frameRate: 17
|
|
|
|
});
|
2012-05-02 15:35:32 +08:00
|
|
|
|
2012-07-04 03:07:27 +08:00
|
|
|
config.drawFunc = function() {
|
|
|
|
if(!!this.attrs.image) {
|
|
|
|
var context = this.getContext();
|
|
|
|
var anim = this.attrs.animation;
|
|
|
|
var index = this.attrs.index;
|
|
|
|
var f = this.attrs.animations[anim][index];
|
2012-05-02 15:35:32 +08:00
|
|
|
|
2012-07-04 03:07:27 +08:00
|
|
|
context.beginPath();
|
|
|
|
context.rect(0, 0, f.width, f.height);
|
|
|
|
context.closePath();
|
2012-06-10 15:02:16 +08:00
|
|
|
|
2012-07-04 03:07:27 +08:00
|
|
|
this.drawImage(this.attrs.image, f.x, f.y, f.width, f.height, 0, 0, f.width, f.height);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// call super constructor
|
|
|
|
this._super(config);
|
2012-06-21 03:55:34 +08:00
|
|
|
|
2012-07-04 03:07:27 +08:00
|
|
|
var that = this;
|
|
|
|
this.on('animationChange', function() {
|
|
|
|
// reset index when animation changes
|
|
|
|
that.setIndex(0);
|
|
|
|
});
|
|
|
|
},
|
2012-05-02 15:35:32 +08:00
|
|
|
/**
|
|
|
|
* start sprite animation
|
|
|
|
*/
|
|
|
|
start: function() {
|
|
|
|
var that = this;
|
|
|
|
var layer = this.getLayer();
|
|
|
|
this.interval = setInterval(function() {
|
|
|
|
that._updateIndex();
|
|
|
|
layer.draw();
|
|
|
|
if(that.afterFrameFunc && that.attrs.index === that.afterFrameIndex) {
|
|
|
|
that.afterFrameFunc();
|
|
|
|
}
|
|
|
|
}, 1000 / this.attrs.frameRate)
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* stop sprite animation
|
|
|
|
*/
|
|
|
|
stop: function() {
|
|
|
|
clearInterval(this.interval);
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* set after frame event handler
|
|
|
|
* @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-04 03:07:27 +08:00
|
|
|
});
|
2012-06-11 04:07:09 +08:00
|
|
|
|
2012-07-04 03:07:27 +08:00
|
|
|
// add getters setters
|
|
|
|
Kinetic.Node.addGettersSetters(Kinetic.Sprite, ['animation', 'animations', 'index']);
|
2012-06-11 04:07:09 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* set animation key
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name setAnimation
|
|
|
|
* @methodOf Kinetic.Sprite.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
* @param {String} anim animation key
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set animations obect
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name setAnimations
|
|
|
|
* @methodOf Kinetic.Sprite.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
* @param {Object} animations
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set animation frame index
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name setIndex
|
|
|
|
* @methodOf Kinetic.Sprite.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
* @param {Integer} index frame index
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get animation key
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name getAnimation
|
|
|
|
* @methodOf Kinetic.Sprite.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get animations object
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name getAnimations
|
|
|
|
* @methodOf Kinetic.Sprite.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get animation frame index
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name getIndex
|
|
|
|
* @methodOf Kinetic.Sprite.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
*/
|