Skip to content

Commit 3a3ad56

Browse files
committed
Added generateFrameNames method and reduced in size.
Fixed Pad bug. Fixed Animation framerate delta speed.
1 parent 4dae85e commit 3a3ad56

5 files changed

Lines changed: 109 additions & 50 deletions

File tree

v3/src/animation/AnimationManager.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
var Animation = require('./frame/Animation');
88
var Map = require('../structs/Map');
9+
var Pad = require('../utils/string/Pad');
910

1011
/**
1112
* Animations are managed by the global AnimationManager. This is a singleton class that is
@@ -95,16 +96,56 @@ AnimationManager.prototype = {
9596
return child;
9697
},
9798

98-
buildSpriteSheetFrames: function (key, startFrame, endFrame)
99+
generateFrameNumbers: function (key, startFrame, endFrame, firstFrame)
99100
{
100101
var out = [];
101102

103+
if (firstFrame)
104+
{
105+
out.push({ key: key, frame: firstFrame });
106+
}
107+
102108
for (var i = startFrame; i <= endFrame; i++)
103109
{
104110
out.push({ key: key, frame: i });
105111
}
106112

107113
return out;
114+
},
115+
116+
/**
117+
* Really handy function for when you are creating arrays of animation data but it's using frame names and not numbers.
118+
* For example imagine you've got 30 frames named: 'explosion_0001-large' to 'explosion_0030-large'
119+
* You could use this function to generate those by doing: Phaser.Animation.generateFrameNames('explosion_', 1, 30, '-large', 4);
120+
*
121+
* @method Phaser.Animation.generateFrameNames
122+
* @static
123+
* @param {string} prefix - The start of the filename. If the filename was 'explosion_0001-large' the prefix would be 'explosion_'.
124+
* @param {number} start - The number to start sequentially counting from. If your frames are named 'explosion_0001' to 'explosion_0034' the start is 1.
125+
* @param {number} stop - The number to count to. If your frames are named 'explosion_0001' to 'explosion_0034' the stop value is 34.
126+
* @param {string} [suffix=''] - The end of the filename. If the filename was 'explosion_0001-large' the prefix would be '-large'.
127+
* @param {number} [zeroPad=0] - The number of zeros to pad the min and max values with. If your frames are named 'explosion_0001' to 'explosion_0034' then the zeroPad is 4.
128+
* @return {string[]} An array of framenames.
129+
*/
130+
generateFrameNames: function (key, prefix, start, stop, suffix, zeroPad)
131+
{
132+
if (suffix === undefined) { suffix = ''; }
133+
if (zeroPad === undefined) { zeroPad = 0; }
134+
135+
var output = [];
136+
var diff = (start < stop) ? 1 : -1;
137+
138+
// Adjust because we use i !== stop in the for loop
139+
stop += diff;
140+
141+
for (var i = start; i !== stop; i += diff)
142+
{
143+
var frame = prefix + Pad(i, zeroPad, '0', 1) + suffix;
144+
145+
output.push({ key: key, frame: frame });
146+
}
147+
148+
return output;
108149
}
109150

110151
};

v3/src/animation/frame/Animation.js

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,18 @@ Animation.prototype = {
8282
return (index < this.frames.length);
8383
},
8484

85+
getFirstTick: function (component)
86+
{
87+
// When is the first update due?
88+
component.accumulator = 0;
89+
component.nextTick = this.msPerFrame + component.currentFrame.duration + (this.delay * 1000);
90+
},
91+
8592
getNextTick: function (component)
8693
{
8794
// When is the next update due?
8895
component.accumulator -= component.nextTick;
89-
component.nextTick = (this.msPerFrame + component.currentFrame.duration) * component.timescale;
96+
component.nextTick = this.msPerFrame + component.currentFrame.duration;
9097
},
9198

9299
nextFrame: function (component)
@@ -95,13 +102,7 @@ Animation.prototype = {
95102

96103
// TODO: Add frame skip support
97104

98-
if (!frame.isLast)
99-
{
100-
component.updateFrame(frame.nextFrame);
101-
102-
this.getNextTick(component);
103-
}
104-
else
105+
if (frame.isLast)
105106
{
106107
// We're at the end of the animation
107108

@@ -110,7 +111,7 @@ Animation.prototype = {
110111
{
111112
component.forward = false;
112113

113-
// Delay for the current frame?
114+
// Delay for the current frame
114115
this.getNextTick(component);
115116
}
116117
else if (component.repeatCounter > 0)
@@ -124,29 +125,11 @@ Animation.prototype = {
124125
component.stop();
125126
}
126127
}
127-
},
128-
129-
repeatAnimation: function (component)
130-
{
131-
if (this.repeatDelay > 0 && component.pendingRepeat === false)
132-
{
133-
component.pendingRepeat = true;
134-
component.accumulator -= component.nextTick;
135-
component.nextTick += (this.repeatDelay * 1000) * component.timescale;
136-
}
137128
else
138129
{
139-
component.repeatCounter--;
140-
141-
component.forward = true;
142-
143-
component.updateFrame(component.currentFrame.nextFrame);
130+
component.updateFrame(frame.nextFrame);
144131

145132
this.getNextTick(component);
146-
147-
component.pendingRepeat = false;
148-
149-
// OnRepeat
150133
}
151134
},
152135

@@ -156,13 +139,7 @@ Animation.prototype = {
156139

157140
// TODO: Add frame skip support
158141

159-
if (!frame.isFirst)
160-
{
161-
component.updateFrame(frame.prevFrame);
162-
163-
this.getNextTick(component);
164-
}
165-
else
142+
if (frame.isFirst)
166143
{
167144
// We're at the start of the animation
168145

@@ -177,6 +154,36 @@ Animation.prototype = {
177154
component.stop();
178155
}
179156
}
157+
else
158+
{
159+
component.updateFrame(frame.prevFrame);
160+
161+
this.getNextTick(component);
162+
}
163+
},
164+
165+
repeatAnimation: function (component)
166+
{
167+
if (this.repeatDelay > 0 && component.pendingRepeat === false)
168+
{
169+
component.pendingRepeat = true;
170+
component.accumulator -= component.nextTick;
171+
component.nextTick += (this.repeatDelay * 1000);
172+
}
173+
else
174+
{
175+
component.repeatCounter--;
176+
177+
component.forward = true;
178+
179+
component.updateFrame(component.currentFrame.nextFrame);
180+
181+
this.getNextTick(component);
182+
183+
component.pendingRepeat = false;
184+
185+
// OnRepeat
186+
}
180187
},
181188

182189
setFrame: function (component)

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: '78a4a0d0-19a6-11e7-94be-9b4d3e4cd847'
2+
build: '1c28cf90-19ae-11e7-ada8-a93dd71560f8'
33
};
44
module.exports = CHECKSUM;

v3/src/components/Animation.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ var Animation = function (parent)
2929

3030
this.accumulator = 0;
3131

32+
this.prevTick = 0;
3233
this.nextTick = 0;
3334

3435
this.repeatCounter = 0;
@@ -66,34 +67,43 @@ Animation.prototype = {
6667
this.animationManager.load(this, key, startFrame);
6768
},
6869

70+
delayedPlay: function (delay, key, startFrame)
71+
{
72+
this.play(key, startFrame);
73+
74+
this.nextTick += (delay * 1000);
75+
76+
return this.parent;
77+
},
78+
6979
play: function (key, startFrame)
7080
{
7181
if (startFrame === undefined) { startFrame = 0; }
7282

7383
this.load(key, startFrame);
7484

75-
// Move to reset?
76-
this.accumulator = 0;
77-
this.nextTick = 0;
78-
7985
// Should give us 9,007,199,254,740,991 safe repeats
8086
this.repeatCounter = (this.currentAnim.repeat === -1) ? Number.MAX_SAFE_INTEGER : this.currentAnim.repeat;
8187

82-
this.currentAnim.getNextTick(this);
88+
this.currentAnim.getFirstTick(this);
8389

8490
this.forward = true;
8591
this.isPlaying = true;
8692
this.pendingRepeat = false;
93+
94+
return this.parent;
8795
},
8896

8997
// Example data:
9098
// timestamp = 2356.534000020474
91-
// frameDelta = 17.632333353807383 (diff since last timestamp)
92-
update: function (timestamp, frameDelta)
99+
// frameDelta = 17.632333353807383 (diff since last timestamp?)
100+
update: function (timestamp)
93101
{
94102
if (this.isPlaying)
95103
{
96-
this.accumulator += frameDelta;
104+
this.accumulator += (timestamp - this.prevTick) * this.timescale;
105+
106+
this.prevTick = timestamp;
97107

98108
if (this.accumulator >= this.nextTick)
99109
{
@@ -107,6 +117,8 @@ Animation.prototype = {
107117
this.isPlaying = false;
108118

109119
console.log('Animation Stopped');
120+
121+
return this.parent;
110122
}
111123

112124
};

v3/src/utils/string/Pad.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
*/
2828
var Pad = function (str, len, pad, dir)
2929
{
30-
if (len === undefined) { var len = 0; }
31-
if (pad === undefined) { var pad = ' '; }
32-
if (dir === undefined) { var dir = 3; }
30+
if (len === undefined) { len = 0; }
31+
if (pad === undefined) { pad = ' '; }
32+
if (dir === undefined) { dir = 3; }
3333

3434
str = str.toString();
3535

@@ -46,7 +46,7 @@ var Pad = function (str, len, pad, dir)
4646
case 3:
4747
var right = Math.ceil((padlen = len - str.length) / 2);
4848
var left = padlen - right;
49-
str = new Array(left+1).join(pad) + str + new Array(right+1).join(pad);
49+
str = new Array(left + 1).join(pad) + str + new Array(right + 1).join(pad);
5050
break;
5151

5252
default:
@@ -56,7 +56,6 @@ var Pad = function (str, len, pad, dir)
5656
}
5757

5858
return str;
59-
6059
};
6160

6261
module.exports = Pad;

0 commit comments

Comments
 (0)