From 105a2234e7fd173c787510d5749a793a8f2e29ba Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Thu, 6 Apr 2017 03:45:45 +0100 Subject: [PATCH] Frames can now set the alpha and visible properties of the sprites. Frame.onUpdate now called. --- v3/src/animation/AnimationFrame.js | 14 +++++++++++--- v3/src/animation/AnimationManager.js | 11 +++++------ v3/src/animation/frame/GetFrames.js | 19 ++++++++++++++++++- v3/src/components/Animation.js | 21 +++++++++++++++++++-- 4 files changed, 53 insertions(+), 12 deletions(-) diff --git a/v3/src/animation/AnimationFrame.js b/v3/src/animation/AnimationFrame.js index 369653fdc..292566aaf 100644 --- a/v3/src/animation/AnimationFrame.js +++ b/v3/src/animation/AnimationFrame.js @@ -1,4 +1,4 @@ -var AnimationFrame = function (index, frame, duration, onUpdate) +var AnimationFrame = function (index, frame) { // The index of this frame within the Animation.frames array this.index = index; @@ -16,13 +16,21 @@ var AnimationFrame = function (index, frame, duration, onUpdate) this.nextFrame = null; // Additional time (in ms) this frame should appear for - added onto the msPerFrame - this.duration = duration; + this.duration = 0; // What % through the animation progress is this frame? this.progress = 0; // Callback if this frame gets displayed - this.onUpdate = onUpdate; + this.onUpdate = null; + + // When this frame hits, set sprite.alpha to this + this.setAlpha = false; + this.alpha = 1; + + // When this frame hits, set sprite.visible to this + this.setVisible = false; + this.visible = false; }; AnimationFrame.prototype.constructor = AnimationFrame; diff --git a/v3/src/animation/AnimationManager.js b/v3/src/animation/AnimationManager.js index 8a0965181..32db15c75 100644 --- a/v3/src/animation/AnimationManager.js +++ b/v3/src/animation/AnimationManager.js @@ -44,13 +44,12 @@ AnimationManager.prototype = { // { key: textureKey, frame: textureFrame }, // { key: textureKey, frame: textureFrame, duration: float }, // { key: textureKey, frame: textureFrame, onUpdate: function } - // { key: textureKey, frame: textureFrame, flipX: boolean, flipY: boolean } // { key: textureKey, frame: textureFrame, alpha: float } // { key: textureKey, frame: textureFrame, visible: boolean } - // ], - // framerate: integer, - // duration: float (seconds, optional, ignored if framerate is set), - // skipMissedFrames: boolean, + // ] + // framerate: integer + // duration: float (seconds, optional, ignored if framerate is set) + // skipMissedFrames: boolean // delay: integer // repeat: -1 = forever, otherwise integer // repeatDelay: integer @@ -58,7 +57,7 @@ AnimationManager.prototype = { // hideOnComplete: boolean // onStart: function // onRepeat: function - // onComplete: function, + // onComplete: function // transitions: [ // { // key: string <- key of the animation to blend with, diff --git a/v3/src/animation/frame/GetFrames.js b/v3/src/animation/frame/GetFrames.js index 4ca6d8e1e..53620aa86 100644 --- a/v3/src/animation/frame/GetFrames.js +++ b/v3/src/animation/frame/GetFrames.js @@ -31,10 +31,27 @@ var GetFrames = function (textureManager, frames) var frame = GetObjectValue(item, 'frame', 0); var duration = GetObjectValue(item, 'duration', 0); var onUpdate = GetObjectValue(item, 'onUpdate', null); + var alpha = GetObjectValue(item, 'alpha', null); + var visible = GetObjectValue(item, 'visible', null); var textureFrame = textureManager.getFrame(key, frame); - animationFrame = new AnimationFrame(index, textureFrame, duration, onUpdate); + animationFrame = new AnimationFrame(index, textureFrame); + + animationFrame.duration = duration; + animationFrame.onUpdate = onUpdate; + + if (alpha !== null) + { + animationFrame.setAlpha = true; + animationFrame.alpha = alpha; + } + + if (visible !== null) + { + animationFrame.setVisible = true; + animationFrame.visible = visible; + } animationFrame.isFirst = (!prev); diff --git a/v3/src/components/Animation.js b/v3/src/components/Animation.js index 780b2a66b..91823c848 100644 --- a/v3/src/components/Animation.js +++ b/v3/src/components/Animation.js @@ -53,10 +53,27 @@ Animation.prototype = { updateFrame: function (animationFrame) { + var sprite = this.parent; + this.currentFrame = animationFrame; - this.parent.texture = animationFrame.frame.texture; - this.parent.frame = animationFrame.frame; + sprite.texture = animationFrame.frame.texture; + sprite.frame = animationFrame.frame; + + if (animationFrame.setAlpha) + { + sprite.alpha = animationFrame.alpha; + } + + if (animationFrame.setVisible) + { + sprite.visible = animationFrame.visible; + } + + if (animationFrame.onUpdate) + { + animationFrame.onUpdate(sprite, animationFrame); + } }, load: function (key, startFrame)