Skip to content

Commit bef2602

Browse files
committed
Updated to use TweenBuilder and modified update loop.
1 parent fd09dba commit bef2602

2 files changed

Lines changed: 150 additions & 10 deletions

File tree

v3/src/tween/TweenBuilder.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,114 @@ var TweenBuilder = function (manager, config)
194194
};
195195

196196
module.exports = TweenBuilder;
197+
198+
/*
199+
The following are all the same
200+
201+
var tween = this.tweens.add({
202+
targets: player,
203+
x: 200,
204+
duration: 2000,
205+
ease: 'Power1',
206+
yoyo: true
207+
});
208+
209+
var tween = this.tweens.add({
210+
targets: player,
211+
props: {
212+
x: 200
213+
}
214+
duration: 2000,
215+
ease: 'Power1',
216+
yoyo: true
217+
});
218+
219+
var tween = this.tweens.add({
220+
targets: player,
221+
x: { value: 200, duration: 2000, ease: 'Power1', yoyo: true }
222+
});
223+
224+
var tween = this.tweens.add({
225+
targets: player,
226+
props: {
227+
x: { value: 200, duration: 2000, ease: 'Power1', yoyo: true }
228+
}
229+
});
230+
231+
// Chained property tweens:
232+
// Each tween uses the same duration and ease because they've been 'globally' defined, except the middle one,
233+
// which uses its own duration as it overrides the global one
234+
235+
var tween = this.tweens.add({
236+
targets: player,
237+
x: [ { value: 200 }, { value: 300, duration: 50 }, { value: 400 } ],
238+
duration: 2000,
239+
ease: 'Power1',
240+
yoyo: true
241+
});
242+
243+
// Multiple property tweens:
244+
245+
var tween = this.tweens.add({
246+
targets: player,
247+
x: { value: 400, duration: 2000, ease: 'Power1' },
248+
y: { value: 300, duration: 1000, ease: 'Sine' }
249+
});
250+
251+
var tween = this.tweens.add({
252+
targets: player,
253+
props: {
254+
x: { value: 400, duration: 2000, ease: 'Power1' },
255+
y: { value: 300, duration: 1000, ease: 'Sine' }
256+
}
257+
});
258+
259+
// Multiple Targets + Multiple property tweens:
260+
261+
var tween = this.tweens.add({
262+
targets: [ alien1, alien2, alien3, alienBoss ],
263+
props: {
264+
x: { value: 400, duration: 2000 },
265+
y: { value: 300, duration: 1000 }
266+
},
267+
ease: 'Sine'
268+
});
269+
270+
// Multiple Targets + Multiple properties + Multi-state Property tweens:
271+
272+
var tween = this.tweens.add({
273+
targets: [ alien1, alien2, alien3, alienBoss ],
274+
props: {
275+
x: [ { value: 200, duration: 100 }, { value: 300, duration: 50 }, { value: 400 } ],
276+
y: { value: 300, duration: 1000 }
277+
},
278+
ease: 'Sine'
279+
});
280+
281+
// Multi-value Tween Property with static values
282+
283+
var tween = this.tweens.add({
284+
targets: [ alien1, alien2, alien3, alienBoss ],
285+
props: {
286+
x: [ 200, 300, 400 ],
287+
y: [ '+100', '-100', '+100' ]
288+
},
289+
duration: 1000,
290+
ease: 'Sine'
291+
});
292+
293+
// Timeline concept
294+
295+
var tween = this.tweens.add({
296+
targets: player,
297+
timeline: [
298+
{ x: 400 },
299+
{ y: 400 },
300+
{ x: 100 },
301+
{ y: 100 }
302+
],
303+
duration: 1000,
304+
ease: 'Sine'
305+
});
306+
307+
*/

v3/src/tween/TweenManager.js

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
var EventDispatcher = require('../events/EventDispatcher');
3-
var Tween = require('./Tween');
3+
var TweenBuilder = require('./TweenBuilder');
44

55
var TweenManager = function (state)
66
{
@@ -12,8 +12,10 @@ var TweenManager = function (state)
1212
*/
1313
this.events = new EventDispatcher(); // should use State event dispatcher?
1414

15-
this.pending = [];
16-
this.active = [];
15+
this._add = [];
16+
this._pending = [];
17+
this._active = [];
18+
this._destroy = [];
1719
};
1820

1921
TweenManager.prototype.constructor = TweenManager;
@@ -27,9 +29,21 @@ TweenManager.prototype = {
2729

2830
add: function (config)
2931
{
30-
var tween = new Tween(this, config);
32+
var tweens = TweenBuilder(this, config);
3133

32-
this.pending.push(tween);
34+
if (tweens.length === 1)
35+
{
36+
return tweens[0];
37+
}
38+
else
39+
{
40+
return tweens;
41+
}
42+
},
43+
44+
queue: function (tween)
45+
{
46+
this._add.push(tween);
3347

3448
return tween;
3549
},
@@ -46,27 +60,42 @@ TweenManager.prototype = {
4660

4761
update: function (timestamp, delta)
4862
{
49-
var list = this.pending;
63+
var list = this._add;
5064
var i;
65+
var tween;
5166

52-
// Process the pending list first
67+
// Process the addition list first
5368
// This stops callbacks and out of sync events from populating the active array mid-way during the update
5469
if (list.length)
5570
{
5671
for (i = 0; i < list.length; i++)
5772
{
58-
list[i].init(timestamp, delta);
73+
tween = list[i];
74+
75+
// Return true if the Tween should be started right away, otherwise false
76+
if (tween.init())
77+
{
78+
tween.start(timestamp);
79+
80+
this._active.push(tween);
81+
}
82+
else
83+
{
84+
this._pending.push(tween);
85+
}
5986
}
6087

6188
list.length = 0;
6289
}
6390

6491
// Process active tweens
65-
list = this.active;
92+
list = this._active;
6693

6794
for (i = 0; i < list.length; i++)
6895
{
69-
list[i].update(timestamp, delta);
96+
tween = list[i];
97+
98+
tween.update(timestamp, delta);
7099
}
71100
},
72101

0 commit comments

Comments
 (0)