Skip to content

Commit

Permalink
Merge pull request #63 from BoldBigflank/clone-animations
Browse files Browse the repository at this point in the history
animations clone on set
  • Loading branch information
straker committed Feb 23, 2019
2 parents b803cd5 + 3ec465c commit fbb0f2a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 35 deletions.
2 changes: 1 addition & 1 deletion dist/sprite.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/js/kontra.js

Large diffs are not rendered by default.

37 changes: 21 additions & 16 deletions kontra.js
Expand Up @@ -1317,22 +1317,6 @@ kontra = {
this.width = (properties.width !== undefined) ? properties.width : temp.width;
this.height = (properties.height !== undefined) ? properties.height : temp.height;
}
// animation sprite
else if (temp = properties.animations) {
this.animations = {}; // don't copy memory reference of animation object

// clone each animation so no sprite shares an animation
for (prop in temp) {
this.animations[prop] = temp[prop].clone();

// default the current animation to the first one in the list
firstAnimation = firstAnimation || this.animations[prop];
}

this.currentAnimation = firstAnimation;
this.width = this.width || firstAnimation.width;
this.height = this.height || firstAnimation.height;
}

return this;
}
Expand Down Expand Up @@ -1400,6 +1384,10 @@ kontra = {
return this.acceleration.y;
}

get animations() {
return this.anims
}

set x(value) {
this.position.x = value;
}
Expand All @@ -1419,6 +1407,23 @@ kontra = {
this.acceleration.y = value;
}

set animations(value) {
let prop, firstAnimation
this.anims = {}

// clone each animation so no sprite shares an animation
for (prop in value) {
this.anims[prop] = value[prop].clone()

// default the current animation to the first one in the list
firstAnimation = firstAnimation || this.anims[prop]
}

this.currentAnimation = firstAnimation
this.width = this.width || firstAnimation.width
this.height = this.height || firstAnimation.height
}

/**
* Determine if the sprite is alive.
* @memberof kontra.sprite
Expand Down
2 changes: 1 addition & 1 deletion kontra.min.js

Large diffs are not rendered by default.

37 changes: 21 additions & 16 deletions src/sprite.js
Expand Up @@ -148,22 +148,6 @@
this.width = (properties.width !== undefined) ? properties.width : temp.width;
this.height = (properties.height !== undefined) ? properties.height : temp.height;
}
// animation sprite
else if (temp = properties.animations) {
this.animations = {}; // don't copy memory reference of animation object

// clone each animation so no sprite shares an animation
for (prop in temp) {
this.animations[prop] = temp[prop].clone();

// default the current animation to the first one in the list
firstAnimation = firstAnimation || this.animations[prop];
}

this.currentAnimation = firstAnimation;
this.width = this.width || firstAnimation.width;
this.height = this.height || firstAnimation.height;
}

return this;
}
Expand Down Expand Up @@ -231,6 +215,10 @@
return this.acceleration.y;
}

get animations() {
return this.anims
}

set x(value) {
this.position.x = value;
}
Expand All @@ -250,6 +238,23 @@
this.acceleration.y = value;
}

set animations(value) {
let prop, firstAnimation
this.anims = {}

// clone each animation so no sprite shares an animation
for (prop in value) {
this.anims[prop] = value[prop].clone()

// default the current animation to the first one in the list
firstAnimation = firstAnimation || this.anims[prop]
}

this.currentAnimation = firstAnimation
this.width = this.width || firstAnimation.width
this.height = this.height || firstAnimation.height
}

/**
* Determine if the sprite is alive.
* @memberof kontra.sprite
Expand Down

0 comments on commit fbb0f2a

Please sign in to comment.