Skip to content

Commit 101e4f5

Browse files
committed
Animations working :)
1 parent 0ed1de6 commit 101e4f5

6 files changed

Lines changed: 111 additions & 26 deletions

File tree

v3/src/animation/AnimationFrame.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var AnimationFrame = function (frame, duration, onUpdate)
99
// The frame that comes after this one in the animation (if any)
1010
this.nextFrame = null;
1111

12-
// Duration this frame should appear for (modifier to fps rate)
12+
// Additional time (in ms) this frame should appear for - added onto the msPerFrame
1313
this.duration = duration;
1414

1515
// Callback if this frame gets displayed

v3/src/animation/AnimationManager.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ AnimationManager.prototype = {
9797
}
9898

9999
return child;
100+
},
101+
102+
buildSpriteSheetFrames: function (key, startFrame, endFrame)
103+
{
104+
var out = [];
105+
106+
for (var i = startFrame; i <= endFrame; i++)
107+
{
108+
out.push({ key: key, frame: i });
109+
}
110+
111+
return out;
100112
}
101113

102114
};

v3/src/animation/frame/Animation.js

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,57 @@ var Animation = function (manager, key, config)
2424
// onComplete: function,
2525

2626
// Extract all the frame data into the frames array
27-
2827
this.frames = GetFrames(manager.textureManager, GetObjectValue(config, 'frames', []));
2928

30-
this.framerate = GetObjectValue(config, 'framerate', 24);
29+
// Scale the time (make it go faster / slower) >>> move to Animation Component?
30+
this.timeScale = 1;
31+
32+
// The frame rate of playback in frames per second (default 24 if duration is null)
33+
this.framerate = GetObjectValue(config, 'framerate', null);
3134

35+
// How long the animation should play for. If framerate is set it overrides this value
36+
// otherwise framerate is derived from duration
3237
this.duration = GetObjectValue(config, 'duration', null);
3338

34-
if (this.duration === null)
39+
if (this.duration === null && this.framerate === null)
3540
{
36-
this.duration = this.framerate * this.frames.length;
41+
this.framerate = 24;
42+
this.duration = this.framerate / this.frames.length;
43+
}
44+
else if (this.duration && this.framerate === null)
45+
{
46+
// Duration given but no framerate, so set the framerate based on duration
47+
// I.e. 12 frames in the animation, duration = 4 (4000 ms)
48+
// So framerate is 12 / 4 = 3 fps
49+
this.framerate = this.frames.length / this.duration;
3750
}
3851
else
3952
{
40-
// Duration controls framerate
53+
// No duration, so derive from the framerate
54+
// I.e. 15 frames in the animation, framerate = 30 fps
55+
// So duration is 15 / 30 = 0.5 (half a second)
56+
this.duration = this.frames.length / this.framerate;
4157
}
4258

59+
// ms per frame (without including frame specific modifiers)
60+
this.msPerFrame = 1000 / this.framerate;
61+
62+
// Skip frames if the time lags, or always advanced anyway?
4363
this.skipMissedFrames = GetObjectValue(config, 'skipMissedFrames', true);
4464

4565
// Delay before starting playback (in seconds)
4666
this.delay = GetObjectValue(config, 'delay', 0);
4767

68+
// Number of times to repeat the animation (-1 for infinity)
4869
this.repeat = GetObjectValue(config, 'repeat', 0);
4970

71+
// Delay before the repeat starts (in seconds)
5072
this.repeatDelay = GetObjectValue(config, 'repeatDelay', 0);
5173

74+
// Should the animation yoyo? (reverse back down to the start) before repeating?
5275
this.yoyo = GetObjectValue(config, 'yoyo', false);
5376

77+
// Callbacks (swap for Events?)
5478
this.onStart = GetObjectValue(config, 'onStart', false);
5579
this.onRepeat = GetObjectValue(config, 'onRepeat', false);
5680
this.onComplete = GetObjectValue(config, 'onComplete', false);
@@ -77,13 +101,41 @@ Animation.prototype = {
77101
return (index < this.frames.length);
78102
},
79103

80-
setFrame: function (timestamp, child)
104+
getNextTick: function (component)
105+
{
106+
// When is the next update due?
107+
var frame = component.currentFrame;
108+
109+
component.nextTick = this.msPerFrame + frame.duration;
110+
},
111+
112+
setFrame: function (component)
81113
{
114+
// Example data:
115+
// timestamp = 2356.534000020474
116+
// frameDelta = 17.632333353807383 (diff since last timestamp)
117+
82118
// Work out which frame should be set next on the child, and set it
83119

120+
var frame = component.currentFrame;
121+
122+
var diff = component.accumulator - component.nextTick;
123+
124+
if (frame.nextFrame)
125+
{
126+
component.currentFrame = frame.nextFrame;
127+
128+
component.updateFrame();
84129

130+
component.accumulator = diff;
85131

86-
return child;
132+
this.getNextTick(component);
133+
}
134+
else
135+
{
136+
// We're at the end of the animation
137+
component.stop();
138+
}
87139
},
88140

89141
destroy: function ()

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: '0b881830-1986-11e7-85f8-f3dbabc79515'
2+
build: 'fa396890-1994-11e7-861e-87bce096a6e5'
33
};
44
module.exports = CHECKSUM;

v3/src/components/Animation.js

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ var Animation = function (parent)
1313

1414
this.isPlaying = false;
1515

16-
// References to the data stored in the Phaser.Animation class
16+
// Reference to the Phaser.Animation object
1717
this.currentAnim = null;
18+
19+
// Reference to the Phaser.AnimationFrame object
1820
this.currentFrame = null;
1921

22+
// Timing playhead values
23+
this.accumulator = 0;
2024
this.nextTick = 0;
2125
};
2226

@@ -28,40 +32,51 @@ Animation.prototype = {
2832
{
2933
if (startFrame === undefined) { startFrame = 0; }
3034

35+
if (this.isPlaying)
36+
{
37+
this.stop();
38+
}
39+
3140
// Load the new animation in
32-
this.animationManager.loadAnimation(this, key, startFrame);
41+
this.animationManager.load(this, key, startFrame);
3342

34-
this.parent.texture = this.currentFrame.frame.texture;
35-
this.parent.frame = this.currentFrame.frame;
43+
this.updateFrame();
3644
},
3745

3846
play: function (key, startFrame)
3947
{
4048
if (startFrame === undefined) { startFrame = 0; }
4149

42-
if (this.isPlaying)
43-
{
44-
this.stop();
45-
}
46-
4750
this.load(key, startFrame);
4851

49-
this.isPlaying = true;
52+
this.accumulator = 0;
53+
54+
this.currentAnim.getNextTick(this);
5055

51-
// Calculate next tick
52-
// this.nextTick = (1000 / this.framerate) + this.currentFrame.
56+
this.isPlaying = true;
5357
},
5458

5559
stop: function ()
5660
{
5761
this.isPlaying = false;
5862
},
5963

64+
updateFrame: function ()
65+
{
66+
this.parent.texture = this.currentFrame.frame.texture;
67+
this.parent.frame = this.currentFrame.frame;
68+
},
69+
6070
update: function (timestamp, frameDelta)
6171
{
62-
if (!this.isPlaying)
72+
if (this.isPlaying)
6373
{
64-
return;
74+
this.accumulator += frameDelta;
75+
76+
if (this.accumulator >= this.nextTick)
77+
{
78+
this.currentAnim.setFrame(this);
79+
}
6580
}
6681
}
6782

v3/src/phaser.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@ module.exports = Phaser;
6565
global.Phaser = Phaser;
6666

6767
/*
68-
* “Sometimes, the elegant implementation is just a function.
69-
* Not a method. Not a class. Not a framework. Just a function.”
70-
* - John Carmack
68+
* "Documentation is like sex: when it is good, it is very, very good;
69+
* and when it is bad, it is better than nothing."
70+
* -- Dick Brandon
71+
*/
72+
73+
/*
74+
* "Sometimes, the elegant implementation is just a function.
75+
* Not a method. Not a class. Not a framework. Just a function."
76+
* -- John Carmack
7177
*/

0 commit comments

Comments
 (0)