2012-12-02 04:04:10 +08:00
|
|
|
(function() {
|
|
|
|
/**
|
|
|
|
* Sprite constructor
|
|
|
|
* @constructor
|
2013-05-16 15:28:49 +08:00
|
|
|
* @memberof Kinetic
|
2012-12-02 04:04:10 +08:00
|
|
|
* @augments Kinetic.Shape
|
|
|
|
* @param {Object} config
|
2012-12-23 15:08:03 +08:00
|
|
|
* @param {String} config.animation animation key
|
|
|
|
* @param {Object} config.animations animation map
|
2013-01-03 15:55:56 +08:00
|
|
|
* @param {Integer} [config.index] animation index
|
2013-05-18 06:35:21 +08:00
|
|
|
* @param {Image} config.image image object
|
2013-06-02 01:27:44 +08:00
|
|
|
* @@shapeParams
|
|
|
|
* @@nodeParams
|
2013-05-18 06:35:21 +08:00
|
|
|
* @example
|
|
|
|
* var animations = {<br>
|
|
|
|
* idle: [{<br>
|
|
|
|
* x: 2,<br>
|
|
|
|
* y: 2,<br>
|
|
|
|
* width: 70,<br>
|
|
|
|
* height: 119<br>
|
|
|
|
* }, {<br>
|
|
|
|
* x: 71,<br>
|
|
|
|
* y: 2,<br>
|
|
|
|
* width: 74,<br>
|
|
|
|
* height: 119<br>
|
|
|
|
* }, {<br>
|
|
|
|
* x: 146,<br>
|
|
|
|
* y: 2,<br>
|
|
|
|
* width: 81,<br>
|
|
|
|
* height: 119<br>
|
|
|
|
* }, {<br>
|
|
|
|
* x: 226,<br>
|
|
|
|
* y: 2,<br>
|
|
|
|
* width: 76,<br>
|
|
|
|
* height: 119<br>
|
|
|
|
* }],<br>
|
|
|
|
* punch: [{<br>
|
|
|
|
* x: 2,<br>
|
|
|
|
* y: 138,<br>
|
|
|
|
* width: 74,<br>
|
|
|
|
* height: 122<br>
|
|
|
|
* }, {<br>
|
|
|
|
* x: 76,<br>
|
|
|
|
* y: 138,<br>
|
|
|
|
* width: 84,<br>
|
|
|
|
* height: 122<br>
|
|
|
|
* }, {<br>
|
|
|
|
* x: 346,<br>
|
|
|
|
* y: 138,<br>
|
|
|
|
* width: 120,<br>
|
|
|
|
* height: 122<br>
|
|
|
|
* }]<br>
|
|
|
|
* };<br><br>
|
|
|
|
*
|
|
|
|
* var imageObj = new Image();<br>
|
|
|
|
* imageObj.onload = function() {<br>
|
|
|
|
* var sprite = new Kinetic.Sprite({<br>
|
|
|
|
* x: 200,<br>
|
|
|
|
* y: 100,<br>
|
|
|
|
* image: imageObj,<br>
|
|
|
|
* animation: 'idle',<br>
|
|
|
|
* animations: animations,<br>
|
|
|
|
* frameRate: 7,<br>
|
2013-07-23 12:41:41 +08:00
|
|
|
* index: 0<br>
|
2013-05-18 06:35:21 +08:00
|
|
|
* });<br>
|
|
|
|
* };<br>
|
|
|
|
* imageObj.src = '/path/to/image.jpg'
|
2012-12-02 04:04:10 +08:00
|
|
|
*/
|
|
|
|
Kinetic.Sprite = function(config) {
|
2013-07-23 12:41:41 +08:00
|
|
|
this.___init(config);
|
2013-06-02 13:17:18 +08:00
|
|
|
};
|
2012-12-02 04:04:10 +08:00
|
|
|
|
|
|
|
Kinetic.Sprite.prototype = {
|
2013-07-23 12:41:41 +08:00
|
|
|
___init: function(config) {
|
2012-12-02 04:04:10 +08:00
|
|
|
// call super constructor
|
|
|
|
Kinetic.Shape.call(this, config);
|
2013-05-20 12:48:48 +08:00
|
|
|
this.className = 'Sprite';
|
2012-12-02 04:04:10 +08:00
|
|
|
|
|
|
|
this.anim = new Kinetic.Animation();
|
|
|
|
var that = this;
|
2013-07-25 13:56:21 +08:00
|
|
|
this.on('animationChange.kinetic', function() {
|
2012-12-02 04:04:10 +08:00
|
|
|
// reset index when animation changes
|
|
|
|
that.setIndex(0);
|
|
|
|
});
|
|
|
|
},
|
2013-09-01 12:49:18 +08:00
|
|
|
drawFunc: function(context) {
|
2013-07-23 12:41:41 +08:00
|
|
|
var anim = this.getAnimation(),
|
|
|
|
index = this.getIndex(),
|
|
|
|
f = this.getAnimations()[anim][index],
|
2013-04-03 13:07:04 +08:00
|
|
|
image = this.getImage();
|
2012-12-02 04:04:10 +08:00
|
|
|
|
2012-12-10 01:52:33 +08:00
|
|
|
if(image) {
|
2013-09-08 11:55:03 +08:00
|
|
|
context.drawImage(image, f.x, f.y, f.width, f.height, 0, 0, f.width, f.height);
|
2012-12-02 04:04:10 +08:00
|
|
|
}
|
|
|
|
},
|
2013-09-01 12:49:18 +08:00
|
|
|
drawHitFunc: function(context) {
|
2013-07-23 12:41:41 +08:00
|
|
|
var anim = this.getAnimation(),
|
|
|
|
index = this.getIndex(),
|
2013-09-08 11:55:03 +08:00
|
|
|
f = this.getAnimations()[anim][index];
|
2012-12-07 12:23:18 +08:00
|
|
|
|
2013-09-08 11:55:03 +08:00
|
|
|
context.beginPath();
|
|
|
|
context.rect(0, 0, f.width, f.height);
|
|
|
|
context.closePath();
|
2013-09-01 12:49:18 +08:00
|
|
|
context.fill(this);
|
2012-12-02 04:04:10 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* start sprite animation
|
2013-05-16 15:28:49 +08:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2012-12-02 04: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-06 11:57:31 +08:00
|
|
|
this.anim.setLayers(layer);
|
2012-12-02 04:04:10 +08:00
|
|
|
|
|
|
|
this.interval = setInterval(function() {
|
2013-04-03 13:07:04 +08:00
|
|
|
var index = that.getIndex();
|
2012-12-02 04:04:10 +08:00
|
|
|
that._updateIndex();
|
|
|
|
if(that.afterFrameFunc && index === that.afterFrameIndex) {
|
|
|
|
that.afterFrameFunc();
|
|
|
|
delete that.afterFrameFunc;
|
|
|
|
delete that.afterFrameIndex;
|
|
|
|
}
|
2013-04-03 13:07:04 +08:00
|
|
|
}, 1000 / this.getFrameRate());
|
2012-07-29 01:46:16 +08:00
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
this.anim.start();
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* stop sprite animation
|
2013-05-16 15:28:49 +08:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2012-12-02 04:04:10 +08:00
|
|
|
*/
|
|
|
|
stop: function() {
|
|
|
|
this.anim.stop();
|
|
|
|
clearInterval(this.interval);
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* set after frame event handler
|
2013-05-16 15:28:49 +08:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2012-12-02 04: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-03 13:07:04 +08:00
|
|
|
var index = this.getIndex(),
|
|
|
|
animation = this.getAnimation(),
|
|
|
|
animations = this.getAnimations(),
|
2013-07-23 12:41:41 +08:00
|
|
|
anim = animations[animation],
|
2013-04-03 13:07:04 +08:00
|
|
|
len = anim.length;
|
2013-07-23 12:41:41 +08:00
|
|
|
|
2013-04-03 13:07:04 +08:00
|
|
|
if(index < len - 1) {
|
|
|
|
this.setIndex(index + 1);
|
2012-12-02 04:04:10 +08:00
|
|
|
}
|
|
|
|
else {
|
2013-04-03 13:07:04 +08:00
|
|
|
this.setIndex(0);
|
2012-12-02 04:04:10 +08:00
|
|
|
}
|
2012-07-29 01:46:16 +08:00
|
|
|
}
|
2012-12-02 04:04:10 +08:00
|
|
|
};
|
2013-05-08 14:51:02 +08:00
|
|
|
Kinetic.Util.extend(Kinetic.Sprite, Kinetic.Shape);
|
2012-12-02 04:04:10 +08:00
|
|
|
|
|
|
|
// add getters setters
|
2013-08-11 12:11:34 +08:00
|
|
|
Kinetic.Factory.addGetterSetter(Kinetic.Sprite, 'animation');
|
2012-12-02 04:04:10 +08:00
|
|
|
|
2012-07-27 13:58:38 +08:00
|
|
|
/**
|
2012-12-02 04:04:10 +08:00
|
|
|
* set animation key
|
|
|
|
* @name setAnimation
|
2013-05-16 15:28:49 +08:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2012-12-02 04:04:10 +08:00
|
|
|
* @param {String} anim animation key
|
2012-07-27 13:58:38 +08:00
|
|
|
*/
|
|
|
|
|
2013-05-03 01:22:21 +08:00
|
|
|
/**
|
|
|
|
* get animation key
|
|
|
|
* @name getAnimation
|
2013-05-16 15:28:49 +08:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2012-07-27 13:58:38 +08:00
|
|
|
*/
|
2012-12-02 04:04:10 +08:00
|
|
|
|
2013-08-11 12:11:34 +08:00
|
|
|
Kinetic.Factory.addGetterSetter(Kinetic.Sprite, 'animations');
|
2013-04-03 13:07:04 +08:00
|
|
|
|
2012-07-27 13:58:38 +08:00
|
|
|
/**
|
2013-05-03 01:22:21 +08:00
|
|
|
* set animations map
|
|
|
|
* @name setAnimations
|
2013-05-16 15:28:49 +08:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2013-05-03 01:22:21 +08:00
|
|
|
* @param {Object} animations
|
2012-07-27 13:58:38 +08:00
|
|
|
*/
|
2012-12-02 04:04:10 +08:00
|
|
|
|
2013-05-03 01:22:21 +08:00
|
|
|
/**
|
|
|
|
* get animations map
|
|
|
|
* @name getAnimations
|
2013-05-16 15:28:49 +08:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2012-12-02 04:04:10 +08:00
|
|
|
*/
|
|
|
|
|
2013-08-11 12:11:34 +08:00
|
|
|
Kinetic.Factory.addGetterSetter(Kinetic.Sprite, 'image');
|
2013-05-03 01:22:21 +08:00
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
/**
|
2013-07-23 12:41:41 +08:00
|
|
|
* set image
|
2013-05-03 01:22:21 +08:00
|
|
|
* @name setImage
|
2013-05-16 15:28:49 +08:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2013-07-23 12:41:41 +08:00
|
|
|
* @param {Image} image
|
2012-12-02 04:04:10 +08:00
|
|
|
*/
|
|
|
|
|
2013-05-03 01:22:21 +08:00
|
|
|
/**
|
2013-04-03 13:07:04 +08:00
|
|
|
* get image
|
|
|
|
* @name getImage
|
2013-05-16 15:28:49 +08:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2013-04-03 13:07:04 +08:00
|
|
|
*/
|
|
|
|
|
2013-08-11 12:11:34 +08:00
|
|
|
Kinetic.Factory.addGetterSetter(Kinetic.Sprite, 'index', 0);
|
2013-05-03 01:22:21 +08:00
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
/**
|
2013-05-03 01:22:21 +08:00
|
|
|
* set animation frame index
|
|
|
|
* @name setIndex
|
2013-05-16 15:28:49 +08:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2013-05-03 01:22:21 +08:00
|
|
|
* @param {Integer} index frame index
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2012-12-02 04:04:10 +08:00
|
|
|
* get animation frame index
|
|
|
|
* @name getIndex
|
2013-05-16 15:28:49 +08:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2012-12-02 04:04:10 +08:00
|
|
|
*/
|
2013-05-03 01:22:21 +08:00
|
|
|
|
2013-08-11 12:11:34 +08:00
|
|
|
Kinetic.Factory.addGetterSetter(Kinetic.Sprite, 'frameRate', 17);
|
2013-05-03 01:22:21 +08:00
|
|
|
|
2013-05-08 01:19:54 +08: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 15:28:49 +08:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2013-05-08 01:19:54 +08:00
|
|
|
* @param {Integer} frameRate
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get frame rate
|
|
|
|
* @name getFrameRate
|
2013-05-16 15:28:49 +08:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2013-05-08 01:19:54 +08:00
|
|
|
*/
|
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
})();
|