Skip to content

Commit e02efcf

Browse files
committed
Merge branch 'master' into containers
2 parents c232642 + 9f9c7de commit e02efcf

4 files changed

Lines changed: 87 additions & 13 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ being passed to the simulation. The default value is 1 to remain consistent with
4949
* The ComputedSize Component now has `setSize` and `setDisplaySize` methods. This component is used for Game Objects that have a non-texture based size.
5050
* The GamepadManager now extends EventEmitter directly, just like the KeyboardManager does.
5151
* The Gamepad Axis threshold has been increased from 0.05 to 0.1.
52+
* Utils.Array.FindClosestInSorted has a new optional argument `key` which will allow you to scan a top-level property of any object in the given sorted array and get the closest match to it.
5253

5354
### Animation Component Updates
5455

@@ -67,6 +68,7 @@ We have refactored the Animation API to make it more consistent with the rest of
6768
* `play` now returns the parent Game Object instead of the component.
6869
* `progress` method has been removed.
6970
* `getProgress` returns the animation progress value.
71+
* `setProgress` lets you jump the animation to a specific progress point.
7072
* `repeat` method has been removed.
7173
* `getRepeat` returns the animation repeat value.
7274
* `setRepeat` sets the number of times the current animation will repeat.
@@ -88,6 +90,8 @@ We have refactored the Animation API to make it more consistent with the rest of
8890
* `updateFrame` now supports animation frames with custom pivot points and injects these into the Game Object origin.
8991
* `destroy` now removes events, references to the Animation Manager and parent Game Object, clears the current animation and frame and empties internal arrays.
9092
* Changing the `yoyo` property on an Animation Component would have no effect as it only ever checked the global property, it now checks the local one properly allowing you to specify a `yoyo` on a per Game Object basis.
93+
* Animation.destroy now properly clears the global animation object.
94+
* Animation.getFrameByProgress will return the Animation Frame that is closest to the given progress value. For example, in a 5 frame animation calling this method with a value of 0.5 would return the middle frame.
9195

9296
### Examples, Documentation and TypeScript
9397

src/animations/Animation.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
55
*/
66

7+
var Clamp = require('../math/Clamp');
78
var Class = require('../utils/Class');
9+
var FindClosestInSorted = require('../utils/array/FindClosestInSorted');
810
var Frame = require('./AnimationFrame');
911
var GetValue = require('../utils/object/GetValue');
1012

@@ -340,8 +342,8 @@ var Animation = new Class({
340342
*/
341343
this.paused = false;
342344

343-
this.manager.on('pauseall', this.pause.bind(this));
344-
this.manager.on('resumeall', this.resume.bind(this));
345+
this.manager.on('pauseall', this.pause, this);
346+
this.manager.on('resumeall', this.resume, this);
345347
},
346348

347349
/**
@@ -632,6 +634,23 @@ var Animation = new Class({
632634
component.updateFrame(this.frames[startFrame]);
633635
},
634636

637+
/**
638+
* Returns the frame closest to the given progress value between 0 and 1.
639+
*
640+
* @method Phaser.Animations.Animation#getFrameByProgress
641+
* @since 3.4.0
642+
*
643+
* @param {float} value - A value between 0 and 1.
644+
*
645+
* @return {Phaser.Animations.AnimationFrame} [description]
646+
*/
647+
getFrameByProgress: function (value)
648+
{
649+
value = Clamp(value, 0, 1);
650+
651+
return FindClosestInSorted(value, this.frames, 'progress');
652+
},
653+
635654
/**
636655
* [description]
637656
*
@@ -927,7 +946,19 @@ var Animation = new Class({
927946
*/
928947
destroy: function ()
929948
{
930-
// TODO
949+
this.manager.off('pauseall', this.pause, this);
950+
this.manager.off('resumeall', this.resume, this);
951+
952+
this.manager.remove(this.key);
953+
954+
for (var i = 0; i < this.frames.length; i++)
955+
{
956+
this.frames[i].destroy();
957+
}
958+
959+
this.frames = [];
960+
961+
this.manager = null;
931962
}
932963

933964
});

src/gameobjects/components/Animation.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,10 +507,9 @@ var Animation = new Class({
507507

508508
/**
509509
* Takes a value between 0 and 1 and uses it to set how far this animation is through playback.
510-
* Does not factor in repeats or yoyos.
510+
* Does not factor in repeats or yoyos, but does handle playing forwards or backwards.
511511
*
512512
* @method Phaser.GameObjects.Components.Animation#setProgress
513-
* @todo
514513
* @since 3.4.0
515514
*
516515
* @param {float} [value=0] - [description]
@@ -519,6 +518,13 @@ var Animation = new Class({
519518
*/
520519
setProgress: function (value)
521520
{
521+
if (!this.forward)
522+
{
523+
value = 1 - value;
524+
}
525+
526+
this.setCurrentFrame(this.animationManager.getFrameByProgress(value));
527+
522528
return this.parent;
523529
},
524530

src/utils/array/FindClosestInSorted.js

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,64 @@
1212
*
1313
* @param {number} value - The value to search for in the array.
1414
* @param {array} array - The array to search, which must be sorted.
15+
* @param {string} [key] - An optional property key. If specified the array elements property will be checked against value.
1516
*
16-
* @return {number} The nearest value found in the array.
17+
* @return {number|object} The nearest value found in the array, or if a `key` was given, the nearest object with the matching property value.
1718
*/
18-
var FindClosestInSorted = function (value, array)
19+
var FindClosestInSorted = function (value, array, key)
1920
{
2021
if (!array.length)
2122
{
2223
return NaN;
2324
}
24-
else if (array.length === 1 || value < array[0])
25+
else if (array.length === 1)
2526
{
2627
return array[0];
2728
}
2829

2930
var i = 1;
31+
var low;
32+
var high;
3033

31-
while (array[i] < value)
34+
if (key)
3235
{
33-
i++;
36+
if (value < array[0][key])
37+
{
38+
return array[0];
39+
}
40+
41+
while (array[i][key] < value)
42+
{
43+
i++;
44+
}
45+
}
46+
else
47+
{
48+
while (array[i] < value)
49+
{
50+
i++;
51+
}
3452
}
3553

36-
var low = array[i - 1];
37-
var high = (i < array.length) ? array[i] : Number.POSITIVE_INFINITY;
54+
if (i > array.length)
55+
{
56+
i = array.length;
57+
}
3858

39-
return ((high - value) <= (value - low)) ? high : low;
59+
if (key)
60+
{
61+
low = array[i - 1][key];
62+
high = array[i][key];
63+
64+
return ((high - value) <= (value - low)) ? array[i] : array[i - 1];
65+
}
66+
else
67+
{
68+
low = array[i - 1];
69+
high = array[i];
70+
71+
return ((high - value) <= (value - low)) ? high : low;
72+
}
4073
};
4174

4275
module.exports = FindClosestInSorted;

0 commit comments

Comments
 (0)