Skip to content

Commit a2f0c2e

Browse files
committed
Added getTotalFrames and calculateDuration methods.
1 parent 90fbb0f commit a2f0c2e

1 file changed

Lines changed: 58 additions & 23 deletions

File tree

src/animations/Animation.js

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -104,35 +104,14 @@ var Animation = new Class({
104104
*/
105105
this.duration = GetValue(config, 'duration', null);
106106

107-
if (this.duration === null && this.frameRate === null)
108-
{
109-
// No duration or frameRate given, use default frameRate of 24fps
110-
this.frameRate = 24;
111-
this.duration = (this.frameRate / this.frames.length) * 1000;
112-
}
113-
else if (this.duration && this.frameRate === null)
114-
{
115-
// Duration given but no frameRate, so set the frameRate based on duration
116-
// I.e. 12 frames in the animation, duration = 4000 ms
117-
// So frameRate is 12 / (4000 / 1000) = 3 fps
118-
this.frameRate = this.frames.length / (this.duration / 1000);
119-
}
120-
else
121-
{
122-
// frameRate given, derive duration from it (even if duration also specified)
123-
// I.e. 15 frames in the animation, frameRate = 30 fps
124-
// So duration is 15 / 30 = 0.5 * 1000 (half a second, or 500ms)
125-
this.duration = (this.frames.length / this.frameRate) * 1000;
126-
}
127-
128107
/**
129108
* How many ms per frame, not including frame specific modifiers.
130109
*
131110
* @name Phaser.Animations.Animation#msPerFrame
132111
* @type {integer}
133112
* @since 3.0.0
134113
*/
135-
this.msPerFrame = 1000 / this.frameRate;
114+
this.msPerFrame;
136115

137116
/**
138117
* Skip frames if the time lags, or always advanced anyway?
@@ -214,10 +193,64 @@ var Animation = new Class({
214193
*/
215194
this.paused = false;
216195

196+
this.calculateDuration(this, this.getTotalFrames(), this.duration, this.frameRate);
197+
217198
this.manager.on(Events.PAUSE_ALL, this.pause, this);
218199
this.manager.on(Events.RESUME_ALL, this.resume, this);
219200
},
220201

202+
/**
203+
* Gets the total number of frames in this animation.
204+
*
205+
* @method Phaser.Animations.Animation#getTotalFrames
206+
* @since 3.50.0
207+
*
208+
* @return {number} The total number of frames in this animation.
209+
*/
210+
getTotalFrames: function ()
211+
{
212+
return this.frames.length;
213+
},
214+
215+
/**
216+
* Calculates the duration, frame rate and msPerFrame values.
217+
*
218+
* @method Phaser.Animations.Animation#calculateDuration
219+
* @since 3.50.0
220+
*
221+
* @param {(Phaser.Animations.Animation|Phaser.GameObjects.Components.Animation)} target - The target to set the values on.
222+
* @param {number} totalFrames - The total number of frames in the animation.
223+
* @param {number} duration - The duration to calculate the frame rate from.
224+
* @param {number} frameRate - The frame ate to calculate the duration from.
225+
*/
226+
calculateDuration: function (target, totalFrames, duration, frameRate)
227+
{
228+
if (duration === null && frameRate === null)
229+
{
230+
// No duration or frameRate given, use default frameRate of 24fps
231+
target.frameRate = 24;
232+
target.duration = (24 / totalFrames) * 1000;
233+
}
234+
else if (duration && frameRate === null)
235+
{
236+
// Duration given but no frameRate, so set the frameRate based on duration
237+
// I.e. 12 frames in the animation, duration = 4000 ms
238+
// So frameRate is 12 / (4000 / 1000) = 3 fps
239+
target.duration = duration;
240+
target.frameRate = totalFrames / (duration / 1000);
241+
}
242+
else
243+
{
244+
// frameRate given, derive duration from it (even if duration also specified)
245+
// I.e. 15 frames in the animation, frameRate = 30 fps
246+
// So duration is 15 / 30 = 0.5 * 1000 (half a second, or 500ms)
247+
target.frameRate = frameRate;
248+
target.duration = (totalFrames / frameRate) * 1000;
249+
}
250+
251+
target.msPerFrame = 1000 / target.frameRate;
252+
},
253+
221254
/**
222255
* Add frames to the end of the animation.
223256
*
@@ -299,7 +332,7 @@ var Animation = new Class({
299332
*/
300333
completeAnimation: function (component)
301334
{
302-
if (this.hideOnComplete)
335+
if (component.hideOnComplete)
303336
{
304337
component.parent.visible = false;
305338
}
@@ -494,6 +527,8 @@ var Animation = new Class({
494527
component.duration = this.duration;
495528
component.msPerFrame = this.msPerFrame;
496529
component.skipMissedFrames = this.skipMissedFrames;
530+
component.showOnStart = this.showOnStart;
531+
component.hideOnComplete = this.hideOnComplete;
497532

498533
component._delay = this.delay;
499534
component._repeat = this.repeat;

0 commit comments

Comments
 (0)