This commit is contained in:
Eric Rowell
2013-04-02 22:07:04 -07:00
parent 5340ced25d
commit 25ff34bdde
3 changed files with 40 additions and 17 deletions

View File

@@ -23,9 +23,9 @@
this.shapeType = 'Path'; this.shapeType = 'Path';
this._setDrawFuncs(); this._setDrawFuncs();
this.dataArray = Kinetic.Path.parsePathData(this.attrs.data); this.dataArray = Kinetic.Path.parsePathData(this.getData());
this.on('dataChange', function() { this.on('dataChange', function() {
that.dataArray = Kinetic.Path.parsePathData(that.attrs.data); that.dataArray = Kinetic.Path.parsePathData(this.getData());
}); });
}, },
drawFunc: function(canvas) { drawFunc: function(canvas) {
@@ -74,10 +74,6 @@
}; };
Kinetic.Global.extend(Kinetic.Path, Kinetic.Shape); Kinetic.Global.extend(Kinetic.Path, Kinetic.Shape);
/*
* Utility methods written by jfollas to
* handle length and point measurements
*/
Kinetic.Path.getLineLength = function(x1, y1, x2, y2) { Kinetic.Path.getLineLength = function(x1, y1, x2, y2) {
return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}; };

View File

@@ -7,12 +7,13 @@
* @param {String} config.animation animation key * @param {String} config.animation animation key
* @param {Object} config.animations animation map * @param {Object} config.animations animation map
* @param {Integer} [config.index] animation index * @param {Integer} [config.index] animation index
* @param {Image} image image object
* {{ShapeParams}} * {{ShapeParams}}
* {{NodeParams}} * {{NodeParams}}
*/ */
Kinetic.Sprite = function(config) { Kinetic.Sprite = function(config) {
this._initSprite(config); this._initSprite(config);
}; }
Kinetic.Sprite.prototype = { Kinetic.Sprite.prototype = {
_initSprite: function(config) { _initSprite: function(config) {
@@ -31,14 +32,21 @@
}); });
}, },
drawFunc: function(canvas) { drawFunc: function(canvas) {
var anim = this.attrs.animation, index = this.attrs.index, f = this.attrs.animations[anim][index], context = canvas.getContext(), image = this.attrs.image; var anim = this.getAnimation(),
index = this.getIndex(),
f = this.getAnimations()[anim][index],
context = canvas.getContext(),
image = this.getImage();
if(image) { if(image) {
context.drawImage(image, f.x, f.y, f.width, f.height, 0, 0, f.width, f.height); context.drawImage(image, f.x, f.y, f.width, f.height, 0, 0, f.width, f.height);
} }
}, },
drawHitFunc: function(canvas) { drawHitFunc: function(canvas) {
var anim = this.attrs.animation, index = this.attrs.index, f = this.attrs.animations[anim][index], context = canvas.getContext(); var anim = this.getAnimation(),
index = this.getIndex(),
f = this.getAnimations()[anim][index],
context = canvas.getContext();
context.beginPath(); context.beginPath();
context.rect(0, 0, f.width, f.height); context.rect(0, 0, f.width, f.height);
@@ -63,14 +71,14 @@
this.anim.node = layer; this.anim.node = layer;
this.interval = setInterval(function() { this.interval = setInterval(function() {
var index = that.attrs.index; var index = that.getIndex();
that._updateIndex(); that._updateIndex();
if(that.afterFrameFunc && index === that.afterFrameIndex) { if(that.afterFrameFunc && index === that.afterFrameIndex) {
that.afterFrameFunc(); that.afterFrameFunc();
delete that.afterFrameFunc; delete that.afterFrameFunc;
delete that.afterFrameIndex; delete that.afterFrameIndex;
} }
}, 1000 / this.attrs.frameRate); }, 1000 / this.getFrameRate());
this.anim.start(); this.anim.start();
}, },
@@ -95,13 +103,17 @@
this.afterFrameFunc = func; this.afterFrameFunc = func;
}, },
_updateIndex: function() { _updateIndex: function() {
var i = this.attrs.index; var index = this.getIndex(),
var a = this.attrs.animation; animation = this.getAnimation(),
if(i < this.attrs.animations[a].length - 1) { animations = this.getAnimations(),
this.attrs.index++; anim = animations[animation],
len = anim.length;
if(index < len - 1) {
this.setIndex(index + 1);
} }
else { else {
this.attrs.index = 0; this.setIndex(0);
} }
} }
}; };
@@ -110,6 +122,7 @@
// add getters setters // add getters setters
Kinetic.Node.addGetterSetter(Kinetic.Sprite, 'animation'); Kinetic.Node.addGetterSetter(Kinetic.Sprite, 'animation');
Kinetic.Node.addGetterSetter(Kinetic.Sprite, 'animations'); Kinetic.Node.addGetterSetter(Kinetic.Sprite, 'animations');
Kinetic.Node.addGetterSetter(Kinetic.Sprite, 'image');
Kinetic.Node.addGetterSetter(Kinetic.Sprite, 'index', 0); Kinetic.Node.addGetterSetter(Kinetic.Sprite, 'index', 0);
Kinetic.Node.addGetterSetter(Kinetic.Sprite, 'frameRate', 17); Kinetic.Node.addGetterSetter(Kinetic.Sprite, 'frameRate', 17);
@@ -127,6 +140,13 @@
* @param {Object} animations * @param {Object} animations
*/ */
/**
* set image
* @name setImage
* @methodOf Kinetic.Sprite.prototype
* @param {Image} image
*/
/** /**
* set animation frame index * set animation frame index
* @name setIndex * @name setIndex
@@ -146,6 +166,12 @@
* @methodOf Kinetic.Sprite.prototype * @methodOf Kinetic.Sprite.prototype
*/ */
/**
* get image
* @name getImage
* @methodOf Kinetic.Sprite.prototype
*/
/** /**
* get animation frame index * get animation frame index
* @name getIndex * @name getIndex

View File

@@ -84,7 +84,6 @@ Test.Modules.SPRITE = {
image: imageObj, image: imageObj,
animation: 'standing', animation: 'standing',
animations: anims, animations: anims,
index: 0,
frameRate: Math.random() * 6 + 6, frameRate: Math.random() * 6 + 6,
frameRate: 10, frameRate: 10,
draggable: true, draggable: true,
@@ -114,6 +113,8 @@ Test.Modules.SPRITE = {
//document.body.appendChild(layer.bufferCanvas.element) //document.body.appendChild(layer.bufferCanvas.element)
test(sprite.getShapeType() === 'Sprite', 'shape type should be Sprite'); test(sprite.getShapeType() === 'Sprite', 'shape type should be Sprite');
test(sprite.getIndex() === 0, 'sprite index should default to 0');
}; };
imageObj.src = '../assets/scorpion-sprite.png'; imageObj.src = '../assets/scorpion-sprite.png';
} }