Frames can now set the alpha and visible properties of the sprites. Frame.onUpdate now called.

This commit is contained in:
Richard Davey 2017-04-06 03:45:45 +01:00
parent c4924fbd70
commit 105a2234e7
4 changed files with 53 additions and 12 deletions

View file

@ -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 // The index of this frame within the Animation.frames array
this.index = index; this.index = index;
@ -16,13 +16,21 @@ var AnimationFrame = function (index, frame, duration, onUpdate)
this.nextFrame = null; this.nextFrame = null;
// Additional time (in ms) this frame should appear for - added onto the msPerFrame // 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? // What % through the animation progress is this frame?
this.progress = 0; this.progress = 0;
// Callback if this frame gets displayed // 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; AnimationFrame.prototype.constructor = AnimationFrame;

View file

@ -44,13 +44,12 @@ AnimationManager.prototype = {
// { key: textureKey, frame: textureFrame }, // { key: textureKey, frame: textureFrame },
// { key: textureKey, frame: textureFrame, duration: float }, // { key: textureKey, frame: textureFrame, duration: float },
// { key: textureKey, frame: textureFrame, onUpdate: function } // { key: textureKey, frame: textureFrame, onUpdate: function }
// { key: textureKey, frame: textureFrame, flipX: boolean, flipY: boolean }
// { key: textureKey, frame: textureFrame, alpha: float } // { key: textureKey, frame: textureFrame, alpha: float }
// { key: textureKey, frame: textureFrame, visible: boolean } // { key: textureKey, frame: textureFrame, visible: boolean }
// ], // ]
// framerate: integer, // framerate: integer
// duration: float (seconds, optional, ignored if framerate is set), // duration: float (seconds, optional, ignored if framerate is set)
// skipMissedFrames: boolean, // skipMissedFrames: boolean
// delay: integer // delay: integer
// repeat: -1 = forever, otherwise integer // repeat: -1 = forever, otherwise integer
// repeatDelay: integer // repeatDelay: integer
@ -58,7 +57,7 @@ AnimationManager.prototype = {
// hideOnComplete: boolean // hideOnComplete: boolean
// onStart: function // onStart: function
// onRepeat: function // onRepeat: function
// onComplete: function, // onComplete: function
// transitions: [ // transitions: [
// { // {
// key: string <- key of the animation to blend with, // key: string <- key of the animation to blend with,

View file

@ -31,10 +31,27 @@ var GetFrames = function (textureManager, frames)
var frame = GetObjectValue(item, 'frame', 0); var frame = GetObjectValue(item, 'frame', 0);
var duration = GetObjectValue(item, 'duration', 0); var duration = GetObjectValue(item, 'duration', 0);
var onUpdate = GetObjectValue(item, 'onUpdate', null); var onUpdate = GetObjectValue(item, 'onUpdate', null);
var alpha = GetObjectValue(item, 'alpha', null);
var visible = GetObjectValue(item, 'visible', null);
var textureFrame = textureManager.getFrame(key, frame); 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); animationFrame.isFirst = (!prev);

View file

@ -53,10 +53,27 @@ Animation.prototype = {
updateFrame: function (animationFrame) updateFrame: function (animationFrame)
{ {
var sprite = this.parent;
this.currentFrame = animationFrame; this.currentFrame = animationFrame;
this.parent.texture = animationFrame.frame.texture; sprite.texture = animationFrame.frame.texture;
this.parent.frame = animationFrame.frame; 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) load: function (key, startFrame)