Merge pull request #2505 from gotenxds/ReverseAnimation

Added a reverse functionality to animations.
This commit is contained in:
Richard Davey 2016-06-02 15:01:57 +01:00
commit 837d3cba46
4 changed files with 2770 additions and 2682 deletions

View file

@ -6,7 +6,7 @@
/**
* An Animation instance contains a single animation and the controls to play it.
*
*
* It is created by the AnimationManager, consists of Animation.Frame objects and belongs to a single Game Object such as a Sprite.
*
* @class Phaser.Animation
@ -130,10 +130,10 @@ Phaser.Animation = function (game, parent, name, frameData, frames, frameRate, l
this.onStart = new Phaser.Signal();
/**
* This event is dispatched when the Animation changes frame.
* This event is dispatched when the Animation changes frame.
* By default this event is disabled due to its intensive nature. Enable it with: `Animation.enableUpdate = true`.
* Note that the event is only dispatched with the current frame. In a low-FPS environment Animations
* will automatically frame-skip to try and claw back time, so do not base your code on expecting to
* will automatically frame-skip to try and claw back time, so do not base your code on expecting to
* receive a perfectly sequential set of frames from this event.
* @property {Phaser.Signal|null} onUpdate
* @default
@ -150,6 +150,12 @@ Phaser.Animation = function (game, parent, name, frameData, frames, frameRate, l
*/
this.onLoop = new Phaser.Signal();
/**
* @property {boolean} isReversed - Indicates if the animation will play backwards.
* @default
*/
this.isReversed = false;
// Set-up some event listeners
this.game.onPause.add(this.onPause, this);
this.game.onResume.add(this.onResume, this);
@ -195,7 +201,7 @@ Phaser.Animation.prototype = {
this._timeLastFrame = this.game.time.time;
this._timeNextFrame = this.game.time.time + this.delay;
this._frameIndex = 0;
this._frameIndex = this.isReversed ? this._frames.length - 1 : 0;
this.updateCurrentFrame(false, true);
this._parent.events.onAnimationStart$dispatch(this._parent, this);
@ -237,6 +243,32 @@ Phaser.Animation.prototype = {
},
/**
* Reverses the animation direction
*
* @method Phaser.Animation#reverse
* @return {Phaser.Animation} The animation instance.
* */
reverse: function () {
this.reversed = !this.reversed;
return this;
},
/**
* Reverses the animation direction for the current/next animation only
* Once the onComplete event is called this method will be called again and revert
* the reversed state.
*
* @method Phaser.Animation#reverseOnce
* @return {Phaser.Animation} The animation instance.
* */
reverseOnce: function () {
this.onComplete.addOnce(this.reverse.bind(this));
return this.reverse();
},
/**
* Sets this animations playback to a given frame with the given ID.
*
@ -385,14 +417,23 @@ Phaser.Animation.prototype = {
// And what's left now?
this._timeNextFrame = this.game.time.time + (this.delay - this._frameDiff);
this._frameIndex += this._frameSkip;
if (this.isReversed){
this._frameIndex -= this._frameSkip;
}else{
this._frameIndex += this._frameSkip;
}
if (this._frameIndex >= this._frames.length)
if (!this.isReversed && this._frameIndex >= this._frames.length || this.isReversed && this._frameIndex <= -1)
{
if (this.loop)
{
// Update current state before event callback
this._frameIndex %= this._frames.length;
this._frameIndex = Math.abs(this._frameIndex) % this._frames.length;
if (this.isReversed){
this._frameIndex = this._frames.length - 1 - this._frameIndex;
}
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
// Instead of calling updateCurrentFrame we do it here instead
@ -454,7 +495,7 @@ Phaser.Animation.prototype = {
// The animation is already destroyed, probably from a callback
return false;
}
// Previous index
var idx = this.currentFrame.index;
@ -654,6 +695,26 @@ Object.defineProperty(Phaser.Animation.prototype, 'paused', {
});
/**
* @name Phaser.Animation#reversed
* @property {boolean} reversed - Gets and sets the isReversed state of this Animation.
*/
Object.defineProperty(Phaser.Animation.prototype, 'reversed', {
get: function () {
return this.isReversed;
},
set: function (value) {
this.isReversed = value;
}
});
/**
* @name Phaser.Animation#frameTotal
* @property {number} frameTotal - The total number of frames in the currently loaded FrameData, or -1 if no FrameData is loaded.

View file

@ -6,7 +6,7 @@
/**
* Creates a new Line object with a start and an end point.
*
*
* @class Phaser.Line
* @constructor
* @param {number} [x1=0] - The x coordinate of the start of the line.
@ -43,7 +43,7 @@ Phaser.Line.prototype = {
/**
* Sets the components of the Line to the specified values.
*
*
* @method Phaser.Line#setTo
* @param {number} [x1=0] - The x coordinate of the start of the line.
* @param {number} [y1=0] - The y coordinate of the start of the line.
@ -63,7 +63,7 @@ Phaser.Line.prototype = {
/**
* Sets the line to match the x/y coordinates of the two given sprites.
* Can optionally be calculated from their center coordinates.
*
*
* @method Phaser.Line#fromSprite
* @param {Phaser.Sprite} startSprite - The coordinates of this Sprite will be set to the Line.start point.
* @param {Phaser.Sprite} endSprite - The coordinates of this Sprite will be set to the Line.start point.
@ -85,7 +85,7 @@ Phaser.Line.prototype = {
/**
* Sets this line to start at the given `x` and `y` coordinates and for the segment to extend at `angle` for the given `length`.
*
*
* @method Phaser.Line#fromAngle
* @param {number} x - The x coordinate of the start of the line.
* @param {number} y - The y coordinate of the start of the line.
@ -104,12 +104,12 @@ Phaser.Line.prototype = {
/**
* Rotates the line by the amount specified in `angle`.
*
*
* Rotation takes place from the center of the line.
* If you wish to rotate around a different point see Line.rotateAround.
*
*
* If you wish to rotate the ends of the Line then see Line.start.rotate or Line.end.rotate.
*
*
* @method Phaser.Line#rotate
* @param {number} angle - The angle in radians (unless asDegrees is true) to rotate the line by.
* @param {boolean} [asDegrees=false] - Is the given angle in radians (false) or degrees (true)?
@ -129,9 +129,9 @@ Phaser.Line.prototype = {
/**
* Rotates the line by the amount specified in `angle`.
*
*
* Rotation takes place around the coordinates given.
*
*
* @method Phaser.Line#rotateAround
* @param {number} x - The x coordinate to offset the rotation from.
* @param {number} y - The y coordinate to offset the rotation from.
@ -181,7 +181,7 @@ Phaser.Line.prototype = {
/**
* Returns a Point object where the x and y values correspond to the center (or midpoint) of the Line segment.
*
*
* @method Phaser.Line#midPoint
* @param {Phaser.Point} [out] - A Phaser.Point object into which the result will be populated. If not given a new Point object is created.
* @return {Phaser.Point} A Phaser.Point object with the x and y values set to the center of the line segment.
@ -199,10 +199,10 @@ Phaser.Line.prototype = {
/**
* Centers this Line on the given coordinates.
*
*
* The line is centered by positioning the start and end points so that the lines midpoint matches
* the coordinates given.
*
*
* @method Phaser.Line#centerOn
* @param {number} x - The x position to center the line on.
* @param {number} y - The y position to center the line on.
@ -223,7 +223,7 @@ Phaser.Line.prototype = {
/**
* Tests if the given coordinates fall on this line. See pointOnSegment to test against just the line segment.
*
*
* @method Phaser.Line#pointOnLine
* @param {number} x - The line to check against this one.
* @param {number} y - The line to check against this one.
@ -237,7 +237,7 @@ Phaser.Line.prototype = {
/**
* Tests if the given coordinates fall on this line and within the segment. See pointOnLine to test against just the line.
*
*
* @method Phaser.Line#pointOnSegment
* @param {number} x - The line to check against this one.
* @param {number} y - The line to check against this one.
@ -256,7 +256,7 @@ Phaser.Line.prototype = {
/**
* Picks a random point from anywhere on the Line segment and returns it.
*
*
* @method Phaser.Line#random
* @param {Phaser.Point|object} [out] - A Phaser.Point, or any object with public x/y properties, that the values will be set in.
* If no object is provided a new Phaser.Point object will be created. In high performance areas avoid this by re-using an object.
@ -630,12 +630,12 @@ Phaser.Line.intersects = function (a, b, asSegment, result) {
/**
* Checks for intersection between the Line and a Rectangle shape, or a rectangle-like
* object, with public `x`, `y`, `right` and `bottom` properties, such as a Sprite or Body.
*
*
* An intersection is considered valid if:
*
*
* The line starts within, or ends within, the Rectangle.
* The line segment intersects one of the 4 rectangle edges.
*
*
* The for the purposes of this function rectangles are considered 'solid'.
*
* @method intersectsRectangle
@ -667,7 +667,7 @@ Phaser.Line.intersectsRectangle = function (line, rect) {
// If the start or end of the line is inside the rect then we assume
// collision, as rects are solid for our use-case.
if ((x1 >= bx1 && x1 <= bx2 && y1 >= by1 && y1 <= by2) ||
if ((x1 >= bx1 && x1 <= bx2 && y1 >= by1 && y1 <= by2) ||
(x2 >= bx1 && x2 <= bx2 && y2 >= by1 && y2 <= by2))
{
return true;

File diff suppressed because it is too large Load diff

View file

@ -79,6 +79,7 @@ declare module "phaser" {
onStart: Phaser.Signal;
onUpdate: Phaser.Signal;
paused: boolean;
reversed: boolean;
speed: number;
complete(): void;
@ -90,6 +91,8 @@ declare module "phaser" {
play(frameRate?: number, loop?: boolean, killOnComplete?: boolean): Phaser.Animation;
previous(quantity?: number): void;
restart(): void;
reverse(): Animation;
reverseOnce(): Animation;
setFrame(frameId?: string | number, useLocalFrameIndex?: boolean): void;
stop(resetFrame?: boolean, dispatchComplete?: boolean): void;
update(): boolean;
@ -5459,4 +5462,4 @@ declare module "phaser" {
}
}
}