Skip to content

Commit 4220382

Browse files
committed
Evolving the Tween engine. TweenMax string syntax now supported.
1 parent ab114cc commit 4220382

5 files changed

Lines changed: 260 additions & 136 deletions

File tree

src/core/Game.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,8 @@ Phaser.Game.prototype = {
755755
this.debug.preUpdate();
756756
}
757757

758+
this.tweens.update(timeStep);
759+
758760
this.world.camera.preUpdate();
759761
this.physics.preUpdate();
760762
this.state.preUpdate(timeStep);
@@ -798,7 +800,7 @@ Phaser.Game.prototype = {
798800
// update tweens once every frame along with the render logic (to keep them smooth in slowMotion scenarios)
799801
if (!this._paused && !this.pendingStep)
800802
{
801-
this.tweens.update(elapsedTime);
803+
// this.tweens.update(elapsedTime);
802804
}
803805

804806
if (this.renderType !== Phaser.HEADLESS)

src/tween/Easing.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,3 +563,8 @@ Phaser.Easing = {
563563
};
564564

565565
Phaser.Easing.Default = Phaser.Easing.Linear.None;
566+
Phaser.Easing.Power0 = Phaser.Easing.Linear.None;
567+
Phaser.Easing.Power1 = Phaser.Easing.Quadratic.Out;
568+
Phaser.Easing.Power2 = Phaser.Easing.Cubic.Out;
569+
Phaser.Easing.Power3 = Phaser.Easing.Quartic.Out;
570+
Phaser.Easing.Power4 = Phaser.Easing.Quintic.Out;

src/tween/Tween.js

Lines changed: 55 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
* @class Phaser.Tween
1111
* @constructor
12-
* @param {object} target - The Target object that will be affected by this tween.
12+
* @param {object} target - The target object, such as a Phaser.Sprite or property like Phaser.Sprite.scale.
1313
* @param {Phaser.Game} game - Current game instance.
1414
* @param {Phaser.TweenManager} manager - The TweenManager responsible for looking after this Tween.
1515
*/
@@ -21,70 +21,48 @@ Phaser.Tween = function (target, game, manager) {
2121
this.game = game;
2222

2323
/**
24-
* @property {object} target - Reference to the target object.
24+
* @property {object} target - The target object, such as a Phaser.Sprite or property like Phaser.Sprite.scale.
2525
*/
2626
this.target = target;
2727

2828
/**
2929
* @property {object} parent - Reference to the parent tween if part of a chained tween.
3030
*/
31-
this.parent = null;
31+
// this.parent = null;
3232

3333
/**
34-
* @property {Phaser.TweenManager} manager - Reference to the TweenManager.
34+
* @property {Phaser.TweenManager} manager - Reference to the TweenManager responsible for updating this Tween.
3535
*/
3636
this.manager = manager;
3737

38-
this.timeline = [];
39-
40-
/**
41-
* @property {number} duration - The duration of the tween in ms.
42-
* @readOnly
43-
* @default
44-
*/
45-
this.duration = 1000;
46-
47-
/**
48-
* @property {number} percent - A value between 0 and 1 that represents how far through the duration this tween is.
49-
* @readOnly
50-
*/
51-
this.percent = 0;
52-
53-
/**
54-
* @property {number} repeatCounter - If the Tween is set to repeat this contains the current repeat count.
55-
*/
56-
this.repeatCounter = 0;
57-
5838
/**
59-
* @property {boolean} yoyo - True if the Tween is set to yoyo, otherwise false.
60-
* @default
39+
* @property {Array} timeline - An Array of TweenData objects that comprise the different parts of this Tween.
6140
*/
62-
this.yoyo = false;
41+
this.timeline = [];
6342

6443
/**
65-
* @property {boolean} inReverse - When a Tween is yoyoing this value holds if it's currently playing forwards (false) or in reverse (true).
44+
* @property {boolean} reverse - If set to `true` the current tween will play in reverse. If the tween hasn't yet started this has no effect. If there are child tweens then all child tweens will play in reverse from the current point.
6645
* @default
6746
*/
68-
this.inReverse = false;
47+
this.reverse = false;
6948

7049
/**
71-
* @property {number} _delay - Private delay counter.
72-
* @private
50+
* @property {number} speed - The speed at which the tweens will run. A value of 1 means it will match the game frame rate. 0.5 will run at half the frame rate. 2 at double the frame rate and so on.
7351
* @default
7452
*/
75-
this._delay = 0;
53+
this.speed = 1;
7654

7755
/**
78-
* @property {number} startTime - The time the Tween started or null if it hasn't yet started.
56+
* @property {number} repeatCounter - If the Tween and any child tweens are set to repeat this contains the current repeat count.
7957
*/
80-
this.startTime = null;
58+
this.repeatCounter = 0;
8159

8260
/**
8361
* @property {boolean} _onStartCallbackFired - Private flag.
8462
* @private
8563
* @default
8664
*/
87-
this._onStartCallbackFired = false;
65+
// this._onStartCallbackFired = false;
8866

8967
/**
9068
* @property {function} _onUpdateCallback - An onUpdate callback.
@@ -123,16 +101,24 @@ Phaser.Tween = function (target, game, manager) {
123101
/**
124102
* @property {boolean} pendingDelete - True if this Tween is ready to be deleted by the TweenManager.
125103
* @default
104+
* @readOnly
126105
*/
127106
this.pendingDelete = false;
128107

108+
// Move all of these to TweenManager?
109+
129110
/**
130-
* @property {Phaser.Signal} onStart - The onStart event is fired when the Tween begins.
111+
* @property {Phaser.Signal} onStart - The onStart event is fired when the Tween begins. If there is a delay before the tween starts then onStart fires after the delay is finished.
131112
*/
132113
this.onStart = new Phaser.Signal();
133114

134115
/**
135-
* @property {Phaser.Signal} onLoop - The onLoop event is fired if the Tween loops.
116+
* @property {Phaser.Signal} onStart - The onStart event is fired when the Tween begins. If there is a delay before the tween starts then onStart fires after the delay is finished.
117+
*/
118+
this.onChildStart = new Phaser.Signal();
119+
120+
/**
121+
* @property {Phaser.Signal} onLoop - The onLoop event is fired if the Tween loops. If there are chained tweens it fires after all the child tweens have completed.
136122
*/
137123
this.onLoop = new Phaser.Signal();
138124

@@ -147,6 +133,11 @@ Phaser.Tween = function (target, game, manager) {
147133
*/
148134
this.isRunning = false;
149135

136+
/**
137+
* @property {number} current - The current Tween child being run.
138+
* @default
139+
* @readOnly
140+
*/
150141
this.current = 0;
151142

152143
};
@@ -156,83 +147,83 @@ Phaser.Tween.prototype = {
156147
/**
157148
* Sets this tween to be a `to` tween on the properties given. A `to` tween starts at the current value and tweens to the destination value given.
158149
* For example a Sprite with an `x` coordinate of 100 could be tweened to `x` 200 by giving a properties object of `{ x: 200 }`.
150+
* The ease function allows you define the rate of change. You can pass either a function such as Phaser.Easing.Circular.Out or a string such as "Circ".
151+
* ".easeIn", ".easeOut" and "easeInOut" variants are all supported for all ease types.
159152
*
160153
* @method Phaser.Tween#to
161-
* @param {object} properties - The properties you want to tween, such as `Sprite.x` or `Sound.volume`. Given as a JavaScript object.
154+
* @param {object} properties - An object containing the properties you want to tween., such as `Sprite.x` or `Sound.volume`. Given as a JavaScript object.
162155
* @param {number} [duration=1000] - Duration of this tween in ms.
163-
* @param {function} [ease=null] - Easing function. If not set it will default to Phaser.Easing.Default, which is Phaser.Easing.Linear.None by default but can be over-ridden at will.
164-
* @param {boolean} [autoStart=false] - Whether this tween will start automatically or not.
165-
* @param {number} [delay=0] - Delay before this tween will start, defaults to 0 (no delay). Value given is in ms.
166-
* @param {number} [repeat=0] - Should the tween automatically restart once complete? If you want it to run forever set as -1. This ignores any chained tweens.
156+
* @param {function|string} [ease=null] - Easing function. If not set it will default to Phaser.Easing.Default, which is Phaser.Easing.Linear.None by default but can be over-ridden.
157+
* @param {boolean} [autoStart=false] - Set to `true` to allow this tween to start automatically. Otherwise call Tween.start().
158+
* @param {number} [delay=0] - Delay before this tween will start in milliseconds. Defaults to 0, no delay.
159+
* @param {number} [repeat=0] - Should the tween automatically restart once complete? If you want it to run forever set as -1. This only effects this induvidual tween, not any chained tweens.
167160
* @param {boolean} [yoyo=false] - A tween that yoyos will reverse itself and play backwards automatically. A yoyo'd tween doesn't fire the Tween.onComplete event, so listen for Tween.onLoop instead.
168161
* @return {Phaser.Tween} This Tween object.
169162
*/
170163
to: function (properties, duration, ease, autoStart, delay, repeat, yoyo) {
171164

172165
if (typeof duration === 'undefined') { duration = 1000; }
173-
if (typeof ease === 'undefined') { ease = this.easingFunction; }
166+
if (typeof ease === 'undefined') { ease = Phaser.Easing.Default; }
174167
if (typeof autoStart === 'undefined') { autoStart = false; }
175168
if (typeof delay === 'undefined') { delay = 0; }
176169
if (typeof repeat === 'undefined') { repeat = 0; }
177170
if (typeof yoyo === 'undefined') { yoyo = false; }
178171

179-
if (yoyo && repeat === 0)
172+
if (typeof ease === 'string' && this.manager.easeMap[ease])
180173
{
181-
repeat = 1;
174+
ease = this.manager.easeMap[ease];
182175
}
183176

184177
this.timeline.push(new Phaser.TweenData(this).to(properties, duration, ease, autoStart, delay, repeat, yoyo));
185178

186179
if (autoStart)
187180
{
188-
return this.start();
189-
}
190-
else
191-
{
192-
return this;
181+
this.start();
193182
}
194183

184+
return this;
185+
195186
},
196187

197188
/**
198189
* Sets this tween to be a `from` tween on the properties given. A `from` tween starts at the given value and tweens to the current values.
199190
* For example a Sprite with an `x` coordinate of 100 could be tweened from `x: 200` by giving a properties object of `{ x: 200 }`.
191+
* The ease function allows you define the rate of change. You can pass either a function such as Phaser.Easing.Circular.Out or a string such as "Circ".
192+
* ".easeIn", ".easeOut" and "easeInOut" variants are all supported for all ease types.
200193
*
201194
* @method Phaser.Tween#from
202-
* @param {object} properties - Properties you want to tween from.
195+
* @param {object} properties - An object containing the properties you want to tween., such as `Sprite.x` or `Sound.volume`. Given as a JavaScript object.
203196
* @param {number} [duration=1000] - Duration of this tween in ms.
204-
* @param {function} [ease=null] - Easing function. If not set it will default to Phaser.Easing.Linear.None.
205-
* @param {boolean} [autoStart=false] - Whether this tween will start automatically or not.
206-
* @param {number} [delay=0] - Delay before this tween will start, defaults to 0 (no delay). Value given is in ms.
207-
* @param {number} [repeat=0] - Should the tween automatically restart once complete? If you want it to run forever set as Number.MAX_VALUE. This ignores any chained tweens.
197+
* @param {function|string} [ease=null] - Easing function. If not set it will default to Phaser.Easing.Default, which is Phaser.Easing.Linear.None by default but can be over-ridden.
198+
* @param {boolean} [autoStart=false] - Set to `true` to allow this tween to start automatically. Otherwise call Tween.start().
199+
* @param {number} [delay=0] - Delay before this tween will start in milliseconds. Defaults to 0, no delay.
200+
* @param {number} [repeat=0] - Should the tween automatically restart once complete? If you want it to run forever set as -1. This only effects this induvidual tween, not any chained tweens.
208201
* @param {boolean} [yoyo=false] - A tween that yoyos will reverse itself and play backwards automatically. A yoyo'd tween doesn't fire the Tween.onComplete event, so listen for Tween.onLoop instead.
209202
* @return {Phaser.Tween} This Tween object.
210203
*/
211204
from: function(properties, duration, ease, autoStart, delay, repeat, yoyo) {
212205

213206
if (typeof duration === 'undefined') { duration = 1000; }
214-
if (typeof ease === 'undefined') { ease = this.easingFunction; }
207+
if (typeof ease === 'undefined') { ease = Phaser.Easing.Default; }
215208
if (typeof autoStart === 'undefined') { autoStart = false; }
216209
if (typeof delay === 'undefined') { delay = 0; }
217210
if (typeof repeat === 'undefined') { repeat = 0; }
218211
if (typeof yoyo === 'undefined') { yoyo = false; }
219212

220-
if (yoyo && repeat === 0)
213+
if (typeof ease === 'string' && this.manager.easeMap[ease])
221214
{
222-
repeat = 1;
215+
ease = this.manager.easeMap[ease];
223216
}
224217

225218
this.timeline.push(new Phaser.TweenData(this).from(properties, duration, ease, autoStart, delay, repeat, yoyo));
226219

227220
if (autoStart)
228221
{
229-
return this.start();
230-
}
231-
else
232-
{
233-
return this;
222+
this.start();
234223
}
235224

225+
return this;
226+
236227
},
237228

238229
/**
@@ -241,7 +232,7 @@ Phaser.Tween.prototype = {
241232
* If the Tween has a delay set then nothing will start tweening until that delay has expired.
242233
*
243234
* @method Phaser.Tween#start
244-
* @param {number} [index=0] - If this Tween contains chained child tweens you can specify which one to start from. The default is zero, i.e. the first tween created.
235+
* @param {number} [index=0] - If this Tween contains child tweens you can specify which one to start from. The default is zero, i.e. the first tween created.
245236
* @return {Phaser.Tween} This tween. Useful for method chaining.
246237
*/
247238
start: function (index) {

0 commit comments

Comments
 (0)