2012-12-01 12:04:10 -08:00
|
|
|
(function() {
|
|
|
|
/**
|
|
|
|
* Sprite constructor
|
|
|
|
* @constructor
|
2013-05-16 00:28:49 -07:00
|
|
|
* @memberof Kinetic
|
2012-12-01 12:04:10 -08:00
|
|
|
* @augments Kinetic.Shape
|
|
|
|
* @param {Object} config
|
2012-12-22 23:08:03 -08:00
|
|
|
* @param {String} config.animation animation key
|
|
|
|
* @param {Object} config.animations animation map
|
2013-01-02 23:55:56 -08:00
|
|
|
* @param {Integer} [config.index] animation index
|
2013-04-02 22:07:04 -07:00
|
|
|
* @param {Image} image image object
|
2013-01-26 20:42:19 -08:00
|
|
|
* {{ShapeParams}}
|
|
|
|
* {{NodeParams}}
|
2012-12-01 12:04:10 -08:00
|
|
|
*/
|
|
|
|
Kinetic.Sprite = function(config) {
|
|
|
|
this._initSprite(config);
|
2013-04-02 22:07:04 -07:00
|
|
|
}
|
2012-12-01 12:04:10 -08:00
|
|
|
|
|
|
|
Kinetic.Sprite.prototype = {
|
|
|
|
_initSprite: function(config) {
|
2013-03-15 08:33:05 -07:00
|
|
|
this.createAttrs();
|
2013-01-01 00:41:13 -08:00
|
|
|
|
2012-12-01 12:04:10 -08:00
|
|
|
// call super constructor
|
|
|
|
Kinetic.Shape.call(this, config);
|
2013-01-01 00:41:13 -08:00
|
|
|
this.shapeType = 'Sprite';
|
2012-12-01 12:04:10 -08:00
|
|
|
this._setDrawFuncs();
|
|
|
|
|
|
|
|
this.anim = new Kinetic.Animation();
|
|
|
|
var that = this;
|
|
|
|
this.on('animationChange', function() {
|
|
|
|
// reset index when animation changes
|
|
|
|
that.setIndex(0);
|
|
|
|
});
|
|
|
|
},
|
2012-12-09 09:52:33 -08:00
|
|
|
drawFunc: function(canvas) {
|
2013-04-02 22:07:04 -07:00
|
|
|
var anim = this.getAnimation(),
|
|
|
|
index = this.getIndex(),
|
|
|
|
f = this.getAnimations()[anim][index],
|
|
|
|
context = canvas.getContext(),
|
|
|
|
image = this.getImage();
|
2012-12-01 12:04:10 -08:00
|
|
|
|
2012-12-09 09:52:33 -08:00
|
|
|
if(image) {
|
|
|
|
context.drawImage(image, f.x, f.y, f.width, f.height, 0, 0, f.width, f.height);
|
2012-12-01 12:04:10 -08:00
|
|
|
}
|
|
|
|
},
|
2012-12-09 09:52:33 -08:00
|
|
|
drawHitFunc: function(canvas) {
|
2013-04-02 22:07:04 -07:00
|
|
|
var anim = this.getAnimation(),
|
|
|
|
index = this.getIndex(),
|
|
|
|
f = this.getAnimations()[anim][index],
|
|
|
|
context = canvas.getContext();
|
2012-12-06 20:23:18 -08:00
|
|
|
|
2012-07-28 10:46:16 -07:00
|
|
|
context.beginPath();
|
2012-12-06 20:23:18 -08:00
|
|
|
context.rect(0, 0, f.width, f.height);
|
2012-07-28 10:46:16 -07:00
|
|
|
context.closePath();
|
2012-12-09 09:52:33 -08:00
|
|
|
canvas.fill(this);
|
2012-12-01 12:04:10 -08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* start sprite animation
|
2013-05-16 00:28:49 -07:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2012-12-01 12:04:10 -08:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
*/
|
2013-05-05 20:57:31 -07:00
|
|
|
this.anim.setLayers(layer);
|
2012-12-01 12:04:10 -08:00
|
|
|
|
|
|
|
this.interval = setInterval(function() {
|
2013-04-02 22:07:04 -07:00
|
|
|
var index = that.getIndex();
|
2012-12-01 12:04:10 -08:00
|
|
|
that._updateIndex();
|
|
|
|
if(that.afterFrameFunc && index === that.afterFrameIndex) {
|
|
|
|
that.afterFrameFunc();
|
|
|
|
delete that.afterFrameFunc;
|
|
|
|
delete that.afterFrameIndex;
|
|
|
|
}
|
2013-04-02 22:07:04 -07:00
|
|
|
}, 1000 / this.getFrameRate());
|
2012-07-28 10:46:16 -07:00
|
|
|
|
2012-12-01 12:04:10 -08:00
|
|
|
this.anim.start();
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* stop sprite animation
|
2013-05-16 00:28:49 -07:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2012-12-01 12:04:10 -08:00
|
|
|
*/
|
|
|
|
stop: function() {
|
|
|
|
this.anim.stop();
|
|
|
|
clearInterval(this.interval);
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* set after frame event handler
|
2013-05-16 00:28:49 -07:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2012-12-01 12:04:10 -08:00
|
|
|
* @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() {
|
2013-04-02 22:07:04 -07:00
|
|
|
var index = this.getIndex(),
|
|
|
|
animation = this.getAnimation(),
|
|
|
|
animations = this.getAnimations(),
|
|
|
|
anim = animations[animation],
|
|
|
|
len = anim.length;
|
|
|
|
|
|
|
|
if(index < len - 1) {
|
|
|
|
this.setIndex(index + 1);
|
2012-12-01 12:04:10 -08:00
|
|
|
}
|
|
|
|
else {
|
2013-04-02 22:07:04 -07:00
|
|
|
this.setIndex(0);
|
2012-12-01 12:04:10 -08:00
|
|
|
}
|
2012-07-28 10:46:16 -07:00
|
|
|
}
|
2012-12-01 12:04:10 -08:00
|
|
|
};
|
2013-05-07 23:51:02 -07:00
|
|
|
Kinetic.Util.extend(Kinetic.Sprite, Kinetic.Shape);
|
2012-12-01 12:04:10 -08:00
|
|
|
|
|
|
|
// add getters setters
|
2013-03-15 08:33:05 -07:00
|
|
|
Kinetic.Node.addGetterSetter(Kinetic.Sprite, 'animation');
|
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 key
|
|
|
|
* @name setAnimation
|
2013-05-16 00:28:49 -07:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2012-12-01 12:04:10 -08:00
|
|
|
* @param {String} anim animation key
|
2012-07-26 22:58:38 -07:00
|
|
|
*/
|
|
|
|
|
2013-05-02 10:22:21 -07:00
|
|
|
/**
|
|
|
|
* get animation key
|
|
|
|
* @name getAnimation
|
2013-05-16 00:28:49 -07:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2012-07-26 22:58:38 -07:00
|
|
|
*/
|
2012-12-01 12:04:10 -08:00
|
|
|
|
2013-05-02 10:22:21 -07:00
|
|
|
Kinetic.Node.addGetterSetter(Kinetic.Sprite, 'animations');
|
2013-04-02 22:07:04 -07:00
|
|
|
|
2012-07-26 22:58:38 -07:00
|
|
|
/**
|
2013-05-02 10:22:21 -07:00
|
|
|
* set animations map
|
|
|
|
* @name setAnimations
|
2013-05-16 00:28:49 -07:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2013-05-02 10:22:21 -07:00
|
|
|
* @param {Object} animations
|
2012-07-26 22:58:38 -07:00
|
|
|
*/
|
2012-12-01 12:04:10 -08:00
|
|
|
|
2013-05-02 10:22:21 -07:00
|
|
|
/**
|
|
|
|
* get animations map
|
|
|
|
* @name getAnimations
|
2013-05-16 00:28:49 -07:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2012-12-01 12:04:10 -08:00
|
|
|
*/
|
|
|
|
|
2013-05-02 10:22:21 -07:00
|
|
|
Kinetic.Node.addGetterSetter(Kinetic.Sprite, 'image');
|
|
|
|
|
2012-12-01 12:04:10 -08:00
|
|
|
/**
|
2013-05-02 10:22:21 -07:00
|
|
|
* set image
|
|
|
|
* @name setImage
|
2013-05-16 00:28:49 -07:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2013-05-02 10:22:21 -07:00
|
|
|
* @param {Image} image
|
2012-12-01 12:04:10 -08:00
|
|
|
*/
|
|
|
|
|
2013-05-02 10:22:21 -07:00
|
|
|
/**
|
2013-04-02 22:07:04 -07:00
|
|
|
* get image
|
|
|
|
* @name getImage
|
2013-05-16 00:28:49 -07:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2013-04-02 22:07:04 -07:00
|
|
|
*/
|
|
|
|
|
2013-05-02 10:22:21 -07:00
|
|
|
Kinetic.Node.addGetterSetter(Kinetic.Sprite, 'index', 0);
|
|
|
|
|
2012-12-01 12:04:10 -08:00
|
|
|
/**
|
2013-05-02 10:22:21 -07:00
|
|
|
* set animation frame index
|
|
|
|
* @name setIndex
|
2013-05-16 00:28:49 -07:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2013-05-02 10:22:21 -07:00
|
|
|
* @param {Integer} index frame index
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2012-12-01 12:04:10 -08:00
|
|
|
* get animation frame index
|
|
|
|
* @name getIndex
|
2013-05-16 00:28:49 -07:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2012-12-01 12:04:10 -08:00
|
|
|
*/
|
2013-05-02 10:22:21 -07:00
|
|
|
|
|
|
|
Kinetic.Node.addGetterSetter(Kinetic.Sprite, 'frameRate', 17);
|
|
|
|
|
2013-05-07 10:19:54 -07:00
|
|
|
/**
|
|
|
|
* set frame rate in frames / second. Default is 17 frames per second. Increase this number to make the sprite
|
|
|
|
* animation run faster, and decrease the number to make the sprite animation run slower
|
|
|
|
* @name setFrameRate
|
2013-05-16 00:28:49 -07:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2013-05-07 10:19:54 -07:00
|
|
|
* @param {Integer} frameRate
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get frame rate
|
|
|
|
* @name getFrameRate
|
2013-05-16 00:28:49 -07:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2013-05-07 10:19:54 -07:00
|
|
|
*/
|
|
|
|
|
2012-12-01 12:04:10 -08:00
|
|
|
})();
|