Skip to content

Commit 02ef155

Browse files
committed
A TweenData wouldn't take into account the repeatDelay property when repeating the tween, but now does. A TweenData also has a new property yoyoDelay which controls the delay before the yoyo will start, allowing you to set both independently (thanks @DreadKnight phaserjs#1469)
Tween.updateTweenData allows you to set a property to the given value across one or all of the current tweens. All of the Tween methods like Tween.delay and Tween.repeat have been updated to use this. Tween.repeat has a new parameter `repeatDelay` which allows you to set the delay (in ms) before a tween will repeat itself. Tween.yoyo has a new parameter `yoyoDelay` which allows you to set the delay (in ms) before a tween will start a yoyo. Tween.interpolation has a new parameter `context` which allows you to define the context in which the interpolation function will run.
1 parent 35b4926 commit 02ef155

3 files changed

Lines changed: 107 additions & 79 deletions

File tree

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ We've also removed functions and properties from Pixi classes that Phaser doesn'
153153
* If you load an image and provide a key that was already in-use in the Cache, then the old image is now destroyed (via `Cache.removeImage`) and the new image takes its place.
154154
* BitmapText has a new `maxWidth` property that will attempt to wrap the text if it exceeds the width specified.
155155
* Group.cursorIndex is the index of the item the Group cursor points to. This replaces Group._cache[8].
156+
* Tween.updateTweenData allows you to set a property to the given value across one or all of the current tweens. All of the Tween methods like Tween.delay and Tween.repeat have been updated to use this.
157+
* Tween.repeat has a new parameter `repeatDelay` which allows you to set the delay (in ms) before a tween will repeat itself.
158+
* Tween.yoyo has a new parameter `yoyoDelay` which allows you to set the delay (in ms) before a tween will start a yoyo.
159+
* Tween.interpolation has a new parameter `context` which allows you to define the context in which the interpolation function will run.
156160

157161
### Bug Fixes
158162

@@ -180,8 +184,9 @@ We've also removed functions and properties from Pixi classes that Phaser doesn'
180184
* World.setBounds if called after you had already started P2 Physics would incorrectly create a new collision group for the wall objects. P2.World now remembers the settings you provide for each wall and the collision group, and re-applies these settings should the world dimensions ever change (thanks @nextht #1455)
181185
* InputHandler was using the wrong property in `checkBoundsSprite` when fixedToCamera (thanks @yig #1613)
182186
* Tween.to now correctly accepts arrays are destination values, which makes the Tween interpolate through each value specified in the array using the defined Tween.interpolation method (see new example, thanks @FridayMarch26th #1619)
183-
* Tween.interpolationFunction was using the incorrect context to invoke the function. This is now defined in `TweenData.interpolationFunctionContext` and defaults to `Phaser.Math`. If you provide your own interpolation function then please adjust the context accordingly (thanks @FridayMarch26th #1618)
187+
* Tween.interpolationFunction was using the incorrect context to invoke the function. This is now defined in `TweenData.interpolationContext` and defaults to `Phaser.Math`. If you provide your own interpolation function then please adjust the context accordingly (thanks @FridayMarch26th #1618)
184188
* Graphics.drawEllipse method was missing (thanks @jackrugile #1574)
189+
* A TweenData wouldn't take into account the `repeatDelay` property when repeating the tween, but now does. A TweenData also has a new property `yoyoDelay` which controls the delay before the yoyo will start, allowing you to set both independently (thanks @DreadKnight #1469)
185190

186191
For changes in previous releases please see the extensive [Version History](https://github.com/photonstorm/phaser/blob/master/CHANGELOG.md).
187192

src/tween/Tween.js

Lines changed: 74 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,6 @@ Phaser.Tween = function (target, game, manager) {
6262
*/
6363
this.repeatCounter = 0;
6464

65-
/**
66-
* @property {number} repeatDelay - The amount of time in ms between repeats of this tween and any child tweens.
67-
*/
68-
this.repeatDelay = 0;
69-
7065
/**
7166
* @property {boolean} pendingDelete - True if this Tween is ready to be deleted by the TweenManager.
7267
* @default
@@ -357,17 +352,17 @@ Phaser.Tween.prototype = {
357352
},
358353

359354
/**
360-
* Sets the delay in milliseconds before this tween will start. If there are child tweens it sets the delay before the first child starts.
361-
* The delay is invoked as soon as you call `Tween.start`. If the tween is already running this method doesn't do anything for the current active tween.
362-
* If you have not yet called `Tween.to` or `Tween.from` at least once then this method will do nothing, as there are no tweens to delay.
363-
* If you have child tweens and pass -1 as the index value it sets the delay across all of them.
355+
* Updates either a single TweenData or all TweenData objects properties to the given value.
356+
* Used internally by methods like Tween.delay, Tween.yoyo, etc. but can also be called directly if you know which property you want to tweak.
357+
* The property is not checked, so if you pass an invalid one you'll generate a run-time error.
364358
*
365-
* @method Phaser.Tween#delay
366-
* @param {number} duration - The amount of time in ms that the Tween should wait until it begins once started is called. Set to zero to remove any active delay.
359+
* @method Phaser.Tween#updateTweenData
360+
* @param {string} property - The property to update.
361+
* @param {number|function} value - The value to set the property to.
367362
* @param {number} [index=0] - If this tween has more than one child this allows you to target a specific child. If set to -1 it will set the delay on all the children.
368363
* @return {Phaser.Tween} This tween. Useful for method chaining.
369364
*/
370-
delay: function (duration, index) {
365+
updateTweenData: function (property, value, index) {
371366

372367
if (this.timeline.length === 0) { return this; }
373368

@@ -377,18 +372,35 @@ Phaser.Tween.prototype = {
377372
{
378373
for (var i = 0; i < this.timeline.length; i++)
379374
{
380-
this.timeline[i].delay = duration;
375+
this.timeline[i][property] = value;
381376
}
382377
}
383378
else
384379
{
385-
this.timeline[index].delay = duration;
380+
this.timeline[index][property] = value;
386381
}
387382

388383
return this;
389384

390385
},
391386

387+
/**
388+
* Sets the delay in milliseconds before this tween will start. If there are child tweens it sets the delay before the first child starts.
389+
* The delay is invoked as soon as you call `Tween.start`. If the tween is already running this method doesn't do anything for the current active tween.
390+
* If you have not yet called `Tween.to` or `Tween.from` at least once then this method will do nothing, as there are no tweens to delay.
391+
* If you have child tweens and pass -1 as the index value it sets the delay across all of them.
392+
*
393+
* @method Phaser.Tween#delay
394+
* @param {number} duration - The amount of time in ms that the Tween should wait until it begins once started is called. Set to zero to remove any active delay.
395+
* @param {number} [index=0] - If this tween has more than one child this allows you to target a specific child. If set to -1 it will set the delay on all the children.
396+
* @return {Phaser.Tween} This tween. Useful for method chaining.
397+
*/
398+
delay: function (duration, index) {
399+
400+
return this.updateTweenData('delay', duration, index);
401+
402+
},
403+
392404
/**
393405
* Sets the number of times this tween will repeat.
394406
* If you have not yet called `Tween.to` or `Tween.from` at least once then this method will do nothing, as there are no tweens to repeat.
@@ -397,28 +409,34 @@ Phaser.Tween.prototype = {
397409
*
398410
* @method Phaser.Tween#repeat
399411
* @param {number} total - How many times a tween should repeat before completing. Set to zero to remove an active repeat. Set to -1 to repeat forever.
412+
* @param {number} [repeat=0] - This is the amount of time to pause (in ms) before the repeat will start.
400413
* @param {number} [index=0] - If this tween has more than one child this allows you to target a specific child. If set to -1 it will set the repeat value on all the children.
401414
* @return {Phaser.Tween} This tween. Useful for method chaining.
402415
*/
403-
repeat: function (total, index) {
416+
repeat: function (total, repeatDelay, index) {
404417

405-
if (this.timeline.length === 0) { return this; }
418+
if (typeof repeatDelay === 'undefined') { repeatDelay = 0; }
406419

407-
if (typeof index === 'undefined') { index = 0; }
420+
this.updateTweenData('repeatCounter', total, index);
408421

409-
if (index === -1)
410-
{
411-
for (var i = 0; i < this.timeline.length; i++)
412-
{
413-
this.timeline[i].repeatCounter = total;
414-
}
415-
}
416-
else
417-
{
418-
this.timeline[index].repeatCounter = total;
419-
}
422+
return this.updateTweenData('repeatDelay', repeatDelay, index);
420423

421-
return this;
424+
},
425+
426+
/**
427+
* Sets the delay in milliseconds before this tween will repeat itself.
428+
* The repeatDelay is invoked as soon as you call `Tween.start`. If the tween is already running this method doesn't do anything for the current active tween.
429+
* If you have not yet called `Tween.to` or `Tween.from` at least once then this method will do nothing, as there are no tweens to set repeatDelay on.
430+
* If you have child tweens and pass -1 as the index value it sets the repeatDelay across all of them.
431+
*
432+
* @method Phaser.Tween#repeatDelay
433+
* @param {number} duration - The amount of time in ms that the Tween should wait until it repeats or yoyos once start is called. Set to zero to remove any active repeatDelay.
434+
* @param {number} [index=0] - If this tween has more than one child this allows you to target a specific child. If set to -1 it will set the repeatDelay on all the children.
435+
* @return {Phaser.Tween} This tween. Useful for method chaining.
436+
*/
437+
repeatDelay: function (duration, index) {
438+
439+
return this.updateTweenData('repeatDelay', duration, index);
422440

423441
},
424442

@@ -431,28 +449,34 @@ Phaser.Tween.prototype = {
431449
*
432450
* @method Phaser.Tween#yoyo
433451
* @param {boolean} enable - Set to true to yoyo this tween, or false to disable an already active yoyo.
452+
* @param {number} [yoyoDelay=0] - This is the amount of time to pause (in ms) before the yoyo will start.
434453
* @param {number} [index=0] - If this tween has more than one child this allows you to target a specific child. If set to -1 it will set yoyo on all the children.
435454
* @return {Phaser.Tween} This tween. Useful for method chaining.
436455
*/
437-
yoyo: function(enable, index) {
456+
yoyo: function(enable, yoyoDelay, index) {
438457

439-
if (this.timeline.length === 0) { return this; }
458+
if (typeof yoyoDelay === 'undefined') { yoyoDelay = 0; }
440459

441-
if (typeof index === 'undefined') { index = 0; }
460+
this.updateTweenData('yoyo', enable, index);
442461

443-
if (index === -1)
444-
{
445-
for (var i = 0; i < this.timeline.length; i++)
446-
{
447-
this.timeline[i].yoyo = enable;
448-
}
449-
}
450-
else
451-
{
452-
this.timeline[index].yoyo = enable;
453-
}
462+
return this.updateTweenData('yoyoDelay', yoyoDelay, index);
454463

455-
return this;
464+
},
465+
466+
/**
467+
* Sets the delay in milliseconds before this tween will run a yoyo (only applies if yoyo is enabled).
468+
* The repeatDelay is invoked as soon as you call `Tween.start`. If the tween is already running this method doesn't do anything for the current active tween.
469+
* If you have not yet called `Tween.to` or `Tween.from` at least once then this method will do nothing, as there are no tweens to set repeatDelay on.
470+
* If you have child tweens and pass -1 as the index value it sets the repeatDelay across all of them.
471+
*
472+
* @method Phaser.Tween#yoyoDelay
473+
* @param {number} duration - The amount of time in ms that the Tween should wait until it repeats or yoyos once start is called. Set to zero to remove any active yoyoDelay.
474+
* @param {number} [index=0] - If this tween has more than one child this allows you to target a specific child. If set to -1 it will set the yoyoDelay on all the children.
475+
* @return {Phaser.Tween} This tween. Useful for method chaining.
476+
*/
477+
yoyoDelay: function (duration, index) {
478+
479+
return this.updateTweenData('yoyoDelay', duration, index);
456480

457481
},
458482

@@ -469,26 +493,12 @@ Phaser.Tween.prototype = {
469493
*/
470494
easing: function (ease, index) {
471495

472-
if (typeof index === 'undefined') { index = 0; }
473-
474496
if (typeof ease === 'string' && this.manager.easeMap[ease])
475497
{
476498
ease = this.manager.easeMap[ease];
477499
}
478500

479-
if (index === -1)
480-
{
481-
for (var i = 0; i < this.timeline.length; i++)
482-
{
483-
this.timeline[i].easingFunction = ease;
484-
}
485-
}
486-
else
487-
{
488-
this.timeline[index].easingFunction = ease;
489-
}
490-
491-
return this;
501+
return this.updateTweenData('easingFunction', ease, index);
492502

493503
},
494504

@@ -500,26 +510,17 @@ Phaser.Tween.prototype = {
500510
*
501511
* @method Phaser.Tween#interpolation
502512
* @param {function} interpolation - The interpolation function to use (Phaser.Math.linearInterpolation by default)
513+
* @param {object} [context] - The context under which the interpolation function will be run.
503514
* @param {number} [index=0] - If this tween has more than one child this allows you to target a specific child. If set to -1 it will set the interpolation function on all children.
504515
* @return {Phaser.Tween} This tween. Useful for method chaining.
505516
*/
506-
interpolation: function (interpolation, index) {
517+
interpolation: function (interpolation, context, index) {
507518

508-
if (typeof index === 'undefined') { index = 0; }
519+
if (typeof context === 'undefined') { context = Phaser.Math; }
509520

510-
if (index === -1)
511-
{
512-
for (var i = 0; i < this.timeline.length; i++)
513-
{
514-
this.timeline[i].interpolationFunction = interpolation;
515-
}
516-
}
517-
else
518-
{
519-
this.timeline[index].interpolationFunction = interpolation;
520-
}
521+
this.updateTweenData('interpolationFunction', interpolation, index);
521522

522-
return this;
523+
return this.updateTweenData('interpolationContext', context, index);
523524

524525
},
525526

src/tween/TweenData.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,19 @@ Phaser.TweenData = function (parent) {
8989
*/
9090
this.yoyo = false;
9191

92+
/**
93+
* @property {number} yoyoDelay - The amount of time in ms between yoyos of this tween.
94+
*/
95+
this.yoyoDelay = 0;
96+
9297
/**
9398
* @property {boolean} inReverse - When a Tween is yoyoing this value holds if it's currently playing forwards (false) or in reverse (true).
9499
* @default
95100
*/
96101
this.inReverse = false;
97102

98103
/**
99-
* @property {number} delay - The amount to delay by until the Tween starts (in ms).
104+
* @property {number} delay - The amount to delay by until the Tween starts (in ms). Only applies to the start, use repeatDelay to handle repeats.
100105
* @default
101106
*/
102107
this.delay = 0;
@@ -124,10 +129,10 @@ Phaser.TweenData = function (parent) {
124129
this.interpolationFunction = Phaser.Math.linearInterpolation;
125130

126131
/**
127-
* @property {object} interpolationFunctionContext - The interpolation function context used for the Tween.
132+
* @property {object} interpolationContext - The interpolation function context used for the Tween.
128133
* @default Phaser.Math
129134
*/
130-
this.interpolationFunctionContext = Phaser.Math;
135+
this.interpolationContext = Phaser.Math;
131136

132137
/**
133138
* @property {boolean} isRunning - If the tween is running this is set to `true`. Unless Phaser.Tween a TweenData that is waiting for a delay to expire is *not* considered as running.
@@ -341,6 +346,14 @@ Phaser.TweenData.prototype = {
341346
return Phaser.TweenData.PENDING;
342347
}
343348
}
349+
else
350+
{
351+
// Is Running, but is waiting to repeat
352+
if (this.game.time.time < this.startTime)
353+
{
354+
return Phaser.TweenData.RUNNING;
355+
}
356+
}
344357

345358
if (this.parent.reverse)
346359
{
@@ -364,7 +377,7 @@ Phaser.TweenData.prototype = {
364377

365378
if (Array.isArray(end))
366379
{
367-
this.parent.target[property] = this.interpolationFunction.call(this.interpolationFunctionContext, end, this.value);
380+
this.parent.target[property] = this.interpolationFunction.call(this.interpolationContext, end, this.value);
368381
}
369382
else
370383
{
@@ -513,7 +526,16 @@ Phaser.TweenData.prototype = {
513526
}
514527
}
515528

516-
this.startTime = this.game.time.time + this.delay;
529+
this.startTime = this.game.time.time;
530+
531+
if (this.yoyo && this.inReverse)
532+
{
533+
this.startTime += this.yoyoDelay;
534+
}
535+
else if (!this.inReverse)
536+
{
537+
this.startTime += this.repeatDelay;
538+
}
517539

518540
if (this.parent.reverse)
519541
{

0 commit comments

Comments
 (0)