Skip to content

Commit c4924fb

Browse files
committed
Added Animation pause, resume and paused methods. Also remove animation, and includeDelay option to getFirstTick.
1 parent aa8f082 commit c4924fb

4 files changed

Lines changed: 121 additions & 14 deletions

File tree

v3/src/animation/AnimationManager.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ AnimationManager.prototype = {
4444
// { key: textureKey, frame: textureFrame },
4545
// { key: textureKey, frame: textureFrame, duration: float },
4646
// { key: textureKey, frame: textureFrame, onUpdate: function }
47+
// { key: textureKey, frame: textureFrame, flipX: boolean, flipY: boolean }
48+
// { key: textureKey, frame: textureFrame, alpha: float }
49+
// { key: textureKey, frame: textureFrame, visible: boolean }
4750
// ],
4851
// framerate: integer,
4952
// duration: float (seconds, optional, ignored if framerate is set),
@@ -64,6 +67,21 @@ AnimationManager.prototype = {
6467
// ]
6568
// }
6669

70+
add: function (key, animation)
71+
{
72+
if (this.anims.has(key))
73+
{
74+
console.error('Animation with key', key, 'already exists');
75+
return;
76+
}
77+
78+
animation.key = key;
79+
80+
this.anims.set(key, animation);
81+
82+
return this;
83+
},
84+
6785
create: function (key, config)
6886
{
6987
if (this.anims.has(key))
@@ -84,6 +102,18 @@ AnimationManager.prototype = {
84102
return this.anims.get(key);
85103
},
86104

105+
remove: function (key)
106+
{
107+
var anim = this.get(key);
108+
109+
if (anim)
110+
{
111+
this.anims.delete(key);
112+
}
113+
114+
return anim;
115+
},
116+
87117
// Load an Animation into a Game Objects Animation Component
88118
load: function (child, key, startFrame)
89119
{

v3/src/animation/frame/Animation.js

Lines changed: 9 additions & 2 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)
85+
getFirstTick: function (component, includeDelay)
8686
{
87+
if (includeDelay === undefined) { includeDelay = true; }
88+
8789
// When is the first update due?
8890
component.accumulator = 0;
89-
component.nextTick = this.msPerFrame + component.currentFrame.duration + (this.delay * 1000);
91+
component.nextTick = this.msPerFrame + component.currentFrame.duration;
92+
93+
if (includeDelay)
94+
{
95+
component.nextTick += (this.delay * 1000);
96+
}
9097
},
9198

9299
getNextTick: 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: '15430db0-1a19-11e7-90e9-c9fa246fdb12'
2+
build: '04051ec0-1a5b-11e7-b4b4-b34a3c03518b'
33
};
44
module.exports = CHECKSUM;

v3/src/components/Animation.js

Lines changed: 81 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ var Animation = function (parent)
1111

1212
this.animationManager = parent.state.sys.anims;
1313

14+
this.mainloop = parent.state.game.mainloop;
15+
1416
this.isPlaying = false;
1517

1618
// Reference to the Phaser.Animation object
@@ -35,6 +37,9 @@ var Animation = function (parent)
3537
this.repeatCounter = 0;
3638

3739
this.pendingRepeat = false;
40+
41+
this._paused = false;
42+
this._wasPlaying = false;
3843
};
3944

4045
Animation.prototype.constructor = Animation;
@@ -91,26 +96,25 @@ Animation.prototype = {
9196
this.isPlaying = true;
9297
this.pendingRepeat = false;
9398

94-
this.prevTick = this.parent.state.game.mainloop.lastFrameTimeMs;
99+
this.prevTick = this.mainloop.lastFrameTimeMs;
95100

96101
return this.parent;
97102
},
98103

99-
// Example data:
100-
// timestamp = 2356.534000020474
101-
// frameDelta = 17.632333353807383 (diff since last timestamp?)
102104
update: function (timestamp)
103105
{
104-
if (this.isPlaying)
106+
if (!this.isPlaying)
105107
{
106-
this.accumulator += (timestamp - this.prevTick) * this.timescale;
108+
return;
109+
}
107110

108-
this.prevTick = timestamp;
111+
this.accumulator += (timestamp - this.prevTick) * this.timescale;
109112

110-
if (this.accumulator >= this.nextTick)
111-
{
112-
this.currentAnim.setFrame(this);
113-
}
113+
this.prevTick = timestamp;
114+
115+
if (this.accumulator >= this.nextTick)
116+
{
117+
this.currentAnim.setFrame(this);
114118
}
115119
},
116120

@@ -121,6 +125,72 @@ Animation.prototype = {
121125
return this.parent;
122126
},
123127

128+
restart: function (includeDelay)
129+
{
130+
if (includeDelay === undefined) { includeDelay = false; }
131+
132+
this.currentAnim.getFirstTick(this, includeDelay);
133+
134+
this.forward = true;
135+
this.isPlaying = true;
136+
this.pendingRepeat = false;
137+
138+
this.prevTick = this.mainloop.lastFrameTimeMs;
139+
140+
// Set frame
141+
this.updateFrame(this.currentAnim.frames[0]);
142+
143+
return this.parent;
144+
},
145+
146+
paused: function (value)
147+
{
148+
if (value !== undefined)
149+
{
150+
// Setter
151+
if (value)
152+
{
153+
return this.pause();
154+
}
155+
else
156+
{
157+
return this.resume();
158+
}
159+
}
160+
else
161+
{
162+
return this._paused;
163+
}
164+
},
165+
166+
pause: function ()
167+
{
168+
if (!this._paused)
169+
{
170+
this._paused = true;
171+
this._wasPlaying = this.isPlaying;
172+
this.isPlaying = false;
173+
}
174+
175+
return this;
176+
},
177+
178+
resume: function ()
179+
{
180+
if (this._paused)
181+
{
182+
this._paused = false;
183+
this.isPlaying = this._wasPlaying;
184+
185+
if (this.isPlaying)
186+
{
187+
this.prevTick = this.mainloop.lastFrameTimeMs;
188+
}
189+
}
190+
191+
return this;
192+
},
193+
124194
// How far through the current animation are we?
125195
// Value between 0 and 1
126196
// I.e. [a,b,c,d,e,f] if on frame c progress would be 0.5

0 commit comments

Comments
 (0)