2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2014-02-05 05:54:25 +00:00
|
|
|
* @copyright 2014 Photon Storm Ltd.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2013-08-28 23:09:12 +00:00
|
|
|
/**
|
2014-11-18 20:58:25 +00:00
|
|
|
* Create a new Tween.
|
2013-08-28 23:09:12 +00:00
|
|
|
*
|
2013-10-01 12:54:29 +00:00
|
|
|
* @class Phaser.Tween
|
|
|
|
* @constructor
|
2014-11-19 21:11:07 +00:00
|
|
|
* @param {object} target - The target object, such as a Phaser.Sprite or property like Phaser.Sprite.scale.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {Phaser.Game} game - Current game instance.
|
2014-03-07 01:26:09 +00:00
|
|
|
* @param {Phaser.TweenManager} manager - The TweenManager responsible for looking after this Tween.
|
2013-08-28 23:09:12 +00:00
|
|
|
*/
|
2014-11-18 20:58:25 +00:00
|
|
|
Phaser.Tween = function (target, game, manager) {
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Game} game - A reference to the currently running Game.
|
|
|
|
*/
|
2013-08-28 23:09:12 +00:00
|
|
|
this.game = game;
|
2013-10-01 12:54:29 +00:00
|
|
|
|
|
|
|
/**
|
2014-11-19 21:11:07 +00:00
|
|
|
* @property {object} target - The target object, such as a Phaser.Sprite or property like Phaser.Sprite.scale.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2014-11-18 20:58:25 +00:00
|
|
|
this.target = target;
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2014-11-18 20:58:25 +00:00
|
|
|
* @property {object} parent - Reference to the parent tween if part of a chained tween.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2014-11-19 21:11:07 +00:00
|
|
|
// this.parent = null;
|
2013-10-01 12:54:29 +00:00
|
|
|
|
|
|
|
/**
|
2014-11-19 21:11:07 +00:00
|
|
|
* @property {Phaser.TweenManager} manager - Reference to the TweenManager responsible for updating this Tween.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2014-11-18 20:58:25 +00:00
|
|
|
this.manager = manager;
|
2013-10-01 12:54:29 +00:00
|
|
|
|
|
|
|
/**
|
2014-11-19 21:11:07 +00:00
|
|
|
* @property {Array} timeline - An Array of TweenData objects that comprise the different parts of this Tween.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2014-11-19 21:11:07 +00:00
|
|
|
this.timeline = [];
|
2013-10-01 12:54:29 +00:00
|
|
|
|
|
|
|
/**
|
2014-11-19 21:11:07 +00:00
|
|
|
* @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.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @default
|
|
|
|
*/
|
2014-11-19 21:11:07 +00:00
|
|
|
this.reverse = false;
|
2013-10-01 12:54:29 +00:00
|
|
|
|
|
|
|
/**
|
2014-11-19 21:11:07 +00:00
|
|
|
* @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.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @default
|
|
|
|
*/
|
2014-11-19 21:11:07 +00:00
|
|
|
this.speed = 1;
|
2013-10-01 12:54:29 +00:00
|
|
|
|
|
|
|
/**
|
2014-11-19 21:11:07 +00:00
|
|
|
* @property {number} repeatCounter - If the Tween and any child tweens are set to repeat this contains the current repeat count.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2014-11-19 21:11:07 +00:00
|
|
|
this.repeatCounter = 0;
|
2013-10-01 12:54:29 +00:00
|
|
|
|
|
|
|
/**
|
2013-12-30 16:54:00 +00:00
|
|
|
* @property {boolean} _onStartCallbackFired - Private flag.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @private
|
|
|
|
* @default
|
|
|
|
*/
|
2014-11-19 21:11:07 +00:00
|
|
|
// this._onStartCallbackFired = false;
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-12-18 13:01:45 +00:00
|
|
|
* @property {function} _onUpdateCallback - An onUpdate callback.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @private
|
|
|
|
* @default null
|
|
|
|
*/
|
|
|
|
this._onUpdateCallback = null;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-02-05 02:39:03 +00:00
|
|
|
/**
|
|
|
|
* @property {object} _onUpdateCallbackContext - The context in which to call the onUpdate callback.
|
|
|
|
* @private
|
|
|
|
* @default null
|
|
|
|
*/
|
|
|
|
this._onUpdateCallbackContext = null;
|
|
|
|
|
2014-02-26 23:27:22 +00:00
|
|
|
/**
|
|
|
|
* @property {boolean} _paused - Is this Tween paused or not?
|
|
|
|
* @private
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this._paused = false;
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-12-18 13:01:45 +00:00
|
|
|
* @property {number} _pausedTime - Private pause timer.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @private
|
|
|
|
* @default
|
|
|
|
*/
|
2013-08-28 23:09:12 +00:00
|
|
|
this._pausedTime = 0;
|
|
|
|
|
2014-02-26 23:27:22 +00:00
|
|
|
/**
|
|
|
|
* @property {boolean} _codePaused - Was the Tween paused by code or by Game focus loss?
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._codePaused = false;
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2014-11-18 20:58:25 +00:00
|
|
|
* @property {boolean} pendingDelete - True if this Tween is ready to be deleted by the TweenManager.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @default
|
2014-11-19 21:11:07 +00:00
|
|
|
* @readOnly
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-09-19 03:45:08 +00:00
|
|
|
this.pendingDelete = false;
|
|
|
|
|
2014-11-19 21:11:07 +00:00
|
|
|
// Move all of these to TweenManager?
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2014-11-19 21:11:07 +00:00
|
|
|
* @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.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-08-30 16:09:43 +00:00
|
|
|
this.onStart = new Phaser.Signal();
|
2013-10-01 12:54:29 +00:00
|
|
|
|
|
|
|
/**
|
2014-11-19 21:11:07 +00:00
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
this.onChildStart = new Phaser.Signal();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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.
|
2013-12-18 13:01:45 +00:00
|
|
|
*/
|
|
|
|
this.onLoop = new Phaser.Signal();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onComplete - The onComplete event is fired when the Tween completes. Does not fire if the Tween is set to loop.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-08-28 23:09:12 +00:00
|
|
|
this.onComplete = new Phaser.Signal();
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2014-11-18 20:58:25 +00:00
|
|
|
* @property {boolean} isRunning - If the tween is running this is set to true, otherwise false. Tweens that are in a delayed state or waiting to start are considered as being running.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @default
|
|
|
|
*/
|
2013-08-28 23:09:12 +00:00
|
|
|
this.isRunning = false;
|
|
|
|
|
2014-11-19 21:11:07 +00:00
|
|
|
/**
|
|
|
|
* @property {number} current - The current Tween child being run.
|
|
|
|
* @default
|
|
|
|
* @readOnly
|
|
|
|
*/
|
2014-11-18 20:58:25 +00:00
|
|
|
this.current = 0;
|
|
|
|
|
2013-08-28 23:09:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.Tween.prototype = {
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
2014-07-07 10:07:00 +00:00
|
|
|
* 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.
|
|
|
|
* For example a Sprite with an `x` coordinate of 100 could be tweened to `x` 200 by giving a properties object of `{ x: 200 }`.
|
2014-11-19 21:11:07 +00:00
|
|
|
* 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".
|
|
|
|
* ".easeIn", ".easeOut" and "easeInOut" variants are all supported for all ease types.
|
2013-11-25 04:40:04 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Tween#to
|
2014-11-19 21:11:07 +00:00
|
|
|
* @param {object} properties - An object containing the properties you want to tween., such as `Sprite.x` or `Sound.volume`. Given as a JavaScript object.
|
2013-12-18 13:01:45 +00:00
|
|
|
* @param {number} [duration=1000] - Duration of this tween in ms.
|
2014-11-19 21:11:07 +00:00
|
|
|
* @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.
|
|
|
|
* @param {boolean} [autoStart=false] - Set to `true` to allow this tween to start automatically. Otherwise call Tween.start().
|
|
|
|
* @param {number} [delay=0] - Delay before this tween will start in milliseconds. Defaults to 0, no delay.
|
|
|
|
* @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.
|
2014-04-08 23:59:53 +00:00
|
|
|
* @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.
|
2013-12-18 13:01:45 +00:00
|
|
|
* @return {Phaser.Tween} This Tween object.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2013-12-30 16:54:00 +00:00
|
|
|
to: function (properties, duration, ease, autoStart, delay, repeat, yoyo) {
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
if (typeof duration === 'undefined') { duration = 1000; }
|
2014-11-19 21:11:07 +00:00
|
|
|
if (typeof ease === 'undefined') { ease = Phaser.Easing.Default; }
|
2014-11-18 20:58:25 +00:00
|
|
|
if (typeof autoStart === 'undefined') { autoStart = false; }
|
|
|
|
if (typeof delay === 'undefined') { delay = 0; }
|
|
|
|
if (typeof repeat === 'undefined') { repeat = 0; }
|
|
|
|
if (typeof yoyo === 'undefined') { yoyo = false; }
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2014-11-19 21:11:07 +00:00
|
|
|
if (typeof ease === 'string' && this.manager.easeMap[ease])
|
2014-04-22 01:12:21 +00:00
|
|
|
{
|
2014-11-19 21:11:07 +00:00
|
|
|
ease = this.manager.easeMap[ease];
|
2014-04-22 01:12:21 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
this.timeline.push(new Phaser.TweenData(this).to(properties, duration, ease, autoStart, delay, repeat, yoyo));
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
if (autoStart)
|
|
|
|
{
|
2014-11-19 21:11:07 +00:00
|
|
|
this.start();
|
2013-08-28 23:09:12 +00:00
|
|
|
}
|
|
|
|
|
2014-11-19 21:11:07 +00:00
|
|
|
return this;
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
},
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2014-05-01 15:34:14 +00:00
|
|
|
/**
|
2014-07-07 10:07:00 +00:00
|
|
|
* 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.
|
|
|
|
* For example a Sprite with an `x` coordinate of 100 could be tweened from `x: 200` by giving a properties object of `{ x: 200 }`.
|
2014-11-19 21:11:07 +00:00
|
|
|
* 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".
|
|
|
|
* ".easeIn", ".easeOut" and "easeInOut" variants are all supported for all ease types.
|
2014-05-01 15:34:14 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Tween#from
|
2014-11-19 21:11:07 +00:00
|
|
|
* @param {object} properties - An object containing the properties you want to tween., such as `Sprite.x` or `Sound.volume`. Given as a JavaScript object.
|
2014-05-01 15:34:14 +00:00
|
|
|
* @param {number} [duration=1000] - Duration of this tween in ms.
|
2014-11-19 21:11:07 +00:00
|
|
|
* @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.
|
|
|
|
* @param {boolean} [autoStart=false] - Set to `true` to allow this tween to start automatically. Otherwise call Tween.start().
|
|
|
|
* @param {number} [delay=0] - Delay before this tween will start in milliseconds. Defaults to 0, no delay.
|
|
|
|
* @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.
|
2014-05-01 15:34:14 +00:00
|
|
|
* @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.
|
|
|
|
* @return {Phaser.Tween} This Tween object.
|
|
|
|
*/
|
|
|
|
from: function(properties, duration, ease, autoStart, delay, repeat, yoyo) {
|
2014-07-07 10:07:00 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
if (typeof duration === 'undefined') { duration = 1000; }
|
2014-11-19 21:11:07 +00:00
|
|
|
if (typeof ease === 'undefined') { ease = Phaser.Easing.Default; }
|
2014-11-18 20:58:25 +00:00
|
|
|
if (typeof autoStart === 'undefined') { autoStart = false; }
|
|
|
|
if (typeof delay === 'undefined') { delay = 0; }
|
|
|
|
if (typeof repeat === 'undefined') { repeat = 0; }
|
|
|
|
if (typeof yoyo === 'undefined') { yoyo = false; }
|
2014-07-07 10:07:00 +00:00
|
|
|
|
2014-11-19 21:11:07 +00:00
|
|
|
if (typeof ease === 'string' && this.manager.easeMap[ease])
|
2014-07-07 10:07:00 +00:00
|
|
|
{
|
2014-11-19 21:11:07 +00:00
|
|
|
ease = this.manager.easeMap[ease];
|
2014-05-01 15:34:14 +00:00
|
|
|
}
|
2014-07-07 10:07:00 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
this.timeline.push(new Phaser.TweenData(this).from(properties, duration, ease, autoStart, delay, repeat, yoyo));
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
if (autoStart)
|
2013-12-30 16:54:00 +00:00
|
|
|
{
|
2014-11-19 21:11:07 +00:00
|
|
|
this.start();
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2014-11-19 21:11:07 +00:00
|
|
|
return this;
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
},
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2014-03-03 22:43:35 +00:00
|
|
|
/**
|
2014-11-18 20:58:25 +00:00
|
|
|
* Starts the tween running. Can also be called by the autoStart parameter of `Tween.to.`
|
|
|
|
* This sets the Tween.isRunning property to true and fires the onStartCallback if one is defined.
|
|
|
|
* If the Tween has a delay set then nothing will start tweening until that delay has expired.
|
2014-03-03 22:43:35 +00:00
|
|
|
*
|
2014-11-18 20:58:25 +00:00
|
|
|
* @method Phaser.Tween#start
|
2014-11-19 21:11:07 +00:00
|
|
|
* @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.
|
2014-11-18 20:58:25 +00:00
|
|
|
* @return {Phaser.Tween} This tween. Useful for method chaining.
|
2014-03-03 22:43:35 +00:00
|
|
|
*/
|
2014-11-18 20:58:25 +00:00
|
|
|
start: function (index) {
|
2014-03-03 22:43:35 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
if (this.game === null || this.target === null || this.timeline.length === 0)
|
2014-03-03 22:43:35 +00:00
|
|
|
{
|
2014-11-18 20:58:25 +00:00
|
|
|
return this;
|
2014-03-03 22:43:35 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
if (typeof index === 'undefined') { index = 0; }
|
2014-03-03 22:43:35 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
this.manager.add(this);
|
2014-03-03 22:43:35 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
this.isRunning = true;
|
2014-03-03 22:43:35 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
this._onStartCallbackFired = false;
|
2014-03-03 22:43:35 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
// this.startTime = this.game.time.time + this._delay;
|
2014-09-02 14:45:52 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
if (index < 0 || index > this.timeline.length - 1)
|
2014-09-02 14:45:52 +00:00
|
|
|
{
|
2014-11-18 20:58:25 +00:00
|
|
|
index = 0;
|
2014-09-02 14:45:52 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
this.current = index;
|
2014-09-02 14:45:52 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
this.timeline[this.current].start();
|
2014-03-04 01:27:57 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
return this;
|
2014-03-03 22:43:35 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
2014-10-28 01:49:20 +00:00
|
|
|
* Stops the tween if running and removes it from the TweenManager.
|
|
|
|
* If called directly and there are any `onComplete` callbacks or events they are not dispatched.
|
2014-11-18 20:58:25 +00:00
|
|
|
* Any chained or child tweens will be ignored.
|
2013-11-25 04:40:04 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Tween#stop
|
2014-10-28 01:49:20 +00:00
|
|
|
* @return {Phaser.Tween} This tween. Useful for method chaining.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
|
|
|
stop: function () {
|
2013-08-28 23:09:12 +00:00
|
|
|
|
|
|
|
this.isRunning = false;
|
|
|
|
|
2014-02-05 02:39:03 +00:00
|
|
|
this._onUpdateCallback = null;
|
2014-10-28 01:49:20 +00:00
|
|
|
this._onStartCallbackFired = false;
|
2014-02-05 02:39:03 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
this.manager.remove(this);
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-11-18 20:58:25 +00:00
|
|
|
* Sets a delay time in ms before this tween will start.
|
|
|
|
* The delay is invoked as soon as you call `Tween.start`.
|
2013-11-25 04:40:04 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Tween#delay
|
2014-11-18 20:58:25 +00:00
|
|
|
* @param {number} time - The amount of time in ms that the Tween should wait until it begins, once `Tween.start` is called.
|
2014-10-28 01:49:20 +00:00
|
|
|
* @return {Phaser.Tween} This tween. Useful for method chaining.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2014-11-18 20:58:25 +00:00
|
|
|
delay: function (time) {
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
this._delay = time;
|
2014-10-28 01:49:20 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-11-18 20:58:25 +00:00
|
|
|
* Sets the number of times this Tween will repeat.
|
|
|
|
* If you have chained tweens this value sets the number of times *all* of the children will repeat before this Tween ends.
|
2013-11-25 04:40:04 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Tween#repeat
|
2014-11-18 20:58:25 +00:00
|
|
|
* @param {number} total - How many times to repeat. Set to zero to remove an active repeat. Set to -1 to repeat forever.
|
2014-10-28 01:49:20 +00:00
|
|
|
* @return {Phaser.Tween} This tween. Useful for method chaining.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2014-11-18 20:58:25 +00:00
|
|
|
repeat: function (total) {
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
this.repeatCounter = total;
|
2014-04-22 01:12:21 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-11-18 20:58:25 +00:00
|
|
|
* A Tween that has yoyo set to true will run through from its starting values to its end values and then play back in reverse from end to start.
|
2013-11-25 04:40:04 +00:00
|
|
|
* Used in combination with repeat you can create endless loops.
|
2014-11-18 20:58:25 +00:00
|
|
|
* If you have chained tweens this value sets the number of times *all* of the children
|
2013-11-25 04:40:04 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Tween#yoyo
|
|
|
|
* @param {boolean} yoyo - Set to true to yoyo this tween.
|
2014-10-28 01:49:20 +00:00
|
|
|
* @return {Phaser.Tween} This tween. Useful for method chaining.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2013-12-30 16:54:00 +00:00
|
|
|
yoyo: function(yoyo) {
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
this.yoyo = yoyo;
|
2014-04-22 01:12:21 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
if (yoyo && this.repeatCounter === 0)
|
2014-04-22 01:12:21 +00:00
|
|
|
{
|
2014-11-18 20:58:25 +00:00
|
|
|
this.repeatCounter = 1;
|
2014-04-22 01:12:21 +00:00
|
|
|
}
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-03-23 07:59:28 +00:00
|
|
|
* Set easing function this tween will use, i.e. Phaser.Easing.Linear.None.
|
2013-11-25 04:40:04 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Tween#easing
|
|
|
|
* @param {function} easing - The easing function this tween will use, i.e. Phaser.Easing.Linear.None.
|
2014-10-28 01:49:20 +00:00
|
|
|
* @return {Phaser.Tween} This tween. Useful for method chaining.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2013-12-30 16:54:00 +00:00
|
|
|
easing: function (easing) {
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
this.easingFunction = easing;
|
2013-11-25 04:40:04 +00:00
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set interpolation function the tween will use, by default it uses Phaser.Math.linearInterpolation.
|
2013-12-30 16:54:00 +00:00
|
|
|
* Also available: Phaser.Math.bezierInterpolation and Phaser.Math.catmullRomInterpolation.
|
2013-11-25 04:40:04 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Tween#interpolation
|
|
|
|
* @param {function} interpolation - The interpolation function to use (Phaser.Math.linearInterpolation by default)
|
2014-10-28 01:49:20 +00:00
|
|
|
* @return {Phaser.Tween} This tween. Useful for method chaining.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2013-12-30 16:54:00 +00:00
|
|
|
interpolation: function (interpolation) {
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
this.interpolationFunction = interpolation;
|
2013-11-25 04:40:04 +00:00
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* You can chain tweens together by passing a reference to the chain function. This enables one tween to call another on completion.
|
|
|
|
* You can pass as many tweens as you like to this function, they will each be chained in sequence.
|
|
|
|
*
|
|
|
|
* @method Phaser.Tween#chain
|
2014-10-28 01:49:20 +00:00
|
|
|
* @return {Phaser.Tween} This tween. Useful for method chaining.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
|
|
|
chain: function () {
|
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
this.chainedTweens = arguments;
|
2013-11-25 04:40:04 +00:00
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loop a chain of tweens
|
2014-03-23 07:59:28 +00:00
|
|
|
*
|
2013-11-25 04:40:04 +00:00
|
|
|
* Usage:
|
|
|
|
* game.add.tween(p).to({ x: 700 }, 1000, Phaser.Easing.Linear.None, true)
|
|
|
|
* .to({ y: 300 }, 1000, Phaser.Easing.Linear.None)
|
|
|
|
* .to({ x: 0 }, 1000, Phaser.Easing.Linear.None)
|
|
|
|
* .to({ y: 0 }, 1000, Phaser.Easing.Linear.None)
|
|
|
|
* .loop();
|
|
|
|
* @method Phaser.Tween#loop
|
2014-11-18 20:58:25 +00:00
|
|
|
* @param {boolean} [value=true] - If true this tween and any chained tweens will loop once they reach the end. Set to false to remove an active loop.
|
2014-10-28 01:49:20 +00:00
|
|
|
* @return {Phaser.Tween} This tween. Useful for method chaining.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2014-11-18 20:58:25 +00:00
|
|
|
loop: function(value) {
|
|
|
|
|
|
|
|
if (typeof value === 'undefined') { value = true; }
|
|
|
|
|
|
|
|
this._loop = true;
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-06-04 03:56:49 +00:00
|
|
|
* Sets a callback to be fired each time this tween updates.
|
2013-11-25 04:40:04 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Tween#onUpdateCallback
|
|
|
|
* @param {function} callback - The callback to invoke each time this tween is updated.
|
2014-06-04 03:56:49 +00:00
|
|
|
* @param {object} callbackContext - The context in which to call the onUpdate callback.
|
2014-10-28 01:49:20 +00:00
|
|
|
* @return {Phaser.Tween} This tween. Useful for method chaining.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2014-02-05 02:39:03 +00:00
|
|
|
onUpdateCallback: function (callback, callbackContext) {
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
this._onUpdateCallback = callback;
|
2014-02-05 02:39:03 +00:00
|
|
|
this._onUpdateCallbackContext = callbackContext;
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-03-23 07:59:28 +00:00
|
|
|
* Pauses the tween.
|
2013-11-25 04:40:04 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Tween#pause
|
|
|
|
*/
|
2013-10-03 01:38:35 +00:00
|
|
|
pause: function () {
|
2013-12-30 16:54:00 +00:00
|
|
|
|
2014-02-26 23:27:22 +00:00
|
|
|
this._codePaused = true;
|
2013-10-03 01:38:35 +00:00
|
|
|
this._paused = true;
|
2013-10-11 03:42:11 +00:00
|
|
|
this._pausedTime = this.game.time.now;
|
2013-12-30 16:54:00 +00:00
|
|
|
|
2013-10-03 01:38:35 +00:00
|
|
|
},
|
2013-09-30 10:15:50 +00:00
|
|
|
|
2014-02-26 23:27:22 +00:00
|
|
|
/**
|
|
|
|
* This is called by the core Game loop. Do not call it directly, instead use Tween.pause.
|
|
|
|
* @method Phaser.Tween#_pause
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_pause: function () {
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-02-26 23:27:22 +00:00
|
|
|
if (!this._codePaused)
|
|
|
|
{
|
|
|
|
this._paused = true;
|
|
|
|
this._pausedTime = this.game.time.now;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* Resumes a paused tween.
|
|
|
|
*
|
|
|
|
* @method Phaser.Tween#resume
|
|
|
|
*/
|
2014-02-26 23:27:22 +00:00
|
|
|
resume: function () {
|
2013-12-30 16:54:00 +00:00
|
|
|
|
2014-02-26 23:27:22 +00:00
|
|
|
if (this._paused)
|
2014-02-25 03:33:47 +00:00
|
|
|
{
|
2014-02-26 23:27:22 +00:00
|
|
|
this._paused = false;
|
|
|
|
this._codePaused = false;
|
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
this.startTime += (this.game.time.now - this._pausedTime);
|
2014-02-25 03:33:47 +00:00
|
|
|
}
|
2014-02-26 23:27:22 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is called by the core Game loop. Do not call it directly, instead use Tween.pause.
|
|
|
|
* @method Phaser.Tween#_resume
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_resume: function () {
|
|
|
|
|
|
|
|
if (this._codePaused)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-02-25 03:33:47 +00:00
|
|
|
else
|
|
|
|
{
|
2014-11-18 20:58:25 +00:00
|
|
|
this.startTime += this.game.time.pauseDuration;
|
2014-02-26 23:27:22 +00:00
|
|
|
this._paused = false;
|
2014-02-25 03:33:47 +00:00
|
|
|
}
|
2013-12-30 16:54:00 +00:00
|
|
|
|
2013-10-03 01:38:35 +00:00
|
|
|
},
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* Core tween update function called by the TweenManager. Does not need to be invoked directly.
|
|
|
|
*
|
|
|
|
* @method Phaser.Tween#update
|
|
|
|
* @param {number} time - A timestamp passed in by the TweenManager.
|
2014-11-18 20:58:25 +00:00
|
|
|
* @return {boolean} false if the tween and all chained tweens have completed and should be deleted from the manager, otherwise true (still active).
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2013-12-30 16:54:00 +00:00
|
|
|
update: function (time) {
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
if (this.pendingDelete)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-19 03:45:08 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
// if (this._paused || time < this.startTime)
|
|
|
|
if (this._paused)
|
2013-12-18 13:01:45 +00:00
|
|
|
{
|
2013-08-28 23:09:12 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-18 13:01:45 +00:00
|
|
|
if (this._onStartCallbackFired === false)
|
|
|
|
{
|
2014-11-18 20:58:25 +00:00
|
|
|
this.onStart.dispatch(this.target);
|
2013-11-25 04:40:04 +00:00
|
|
|
this._onStartCallbackFired = true;
|
|
|
|
}
|
2014-11-17 21:10:31 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
var status = this.timeline[this.current].update(time);
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
if (status === Phaser.TweenData.PENDING)
|
2013-12-30 16:54:00 +00:00
|
|
|
{
|
2014-11-18 20:58:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (status === Phaser.TweenData.RUNNING)
|
|
|
|
{
|
|
|
|
if (this._onUpdateCallback !== null)
|
|
|
|
{
|
|
|
|
this._onUpdateCallback.call(this._onUpdateCallbackContext, this, this.timeline[this.current].value, this.timeline[this.current]);
|
|
|
|
}
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
// In case the update callback modifies this tween
|
|
|
|
return this.isRunning;
|
|
|
|
}
|
|
|
|
else if (status === Phaser.TweenData.LOOPED)
|
|
|
|
{
|
|
|
|
this.onLoop.dispatch(this.target, this.timeline[this.current]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (status === Phaser.TweenData.COMPLETE)
|
|
|
|
{
|
|
|
|
if (this.current < this.timeline.length - 1)
|
2013-12-30 16:54:00 +00:00
|
|
|
{
|
2014-11-18 20:58:25 +00:00
|
|
|
this.current++;
|
|
|
|
this.timeline[this.current].start();
|
|
|
|
return true;
|
2013-12-30 16:54:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-18 20:58:25 +00:00
|
|
|
// No more tweens in the chain
|
|
|
|
this.isRunning = false;
|
|
|
|
this.onComplete.dispatch(this.target, this.timeline[this.current]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
/**
|
|
|
|
* This will generate an array populated with the tweened object values from start to end.
|
|
|
|
* It works by running the tween simulation at the given frame rate based on the values set-up in Tween.to and similar functions.
|
|
|
|
* It ignores delay and repeat counts and any chained tweens. Just one play through of tween data is returned, including yoyo if set.
|
|
|
|
*
|
|
|
|
* @method Phaser.Tween#generateData
|
|
|
|
* @param {number} [frameRate=60] - The speed in frames per second that the data should be generated at. The higher the value, the larger the array it creates.
|
|
|
|
* @param {array} [data] - If given the generated data will be appended to this array, otherwise a new array will be returned.
|
|
|
|
* @return {array} An array of tweened values.
|
|
|
|
*/
|
|
|
|
generateData: function (frameRate, data) {
|
|
|
|
|
|
|
|
if (this.game === null || this.target === null)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.startTime = 0;
|
|
|
|
|
|
|
|
for (var property in this._valuesEnd)
|
|
|
|
{
|
|
|
|
// Check if an Array was provided as property value
|
|
|
|
if (Array.isArray(this._valuesEnd[property]))
|
|
|
|
{
|
|
|
|
if (this._valuesEnd[property].length === 0)
|
2013-12-30 16:54:00 +00:00
|
|
|
{
|
2014-11-18 20:58:25 +00:00
|
|
|
continue;
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2014-11-18 20:58:25 +00:00
|
|
|
|
|
|
|
// create a local copy of the Array with the start value at the front
|
|
|
|
this._valuesEnd[property] = [this.target[property]].concat(this._valuesEnd[property]);
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
this._valuesStart[property] = this.target[property];
|
2014-07-02 04:47:38 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
if (!Array.isArray(this._valuesStart[property]))
|
2014-07-02 04:47:38 +00:00
|
|
|
{
|
2014-11-18 20:58:25 +00:00
|
|
|
this._valuesStart[property] *= 1.0; // Ensures we're using numbers, not strings
|
2014-07-02 04:47:38 +00:00
|
|
|
}
|
2014-11-18 20:58:25 +00:00
|
|
|
|
|
|
|
this._valuesStartRepeat[property] = this._valuesStart[property] || 0;
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
// Simulate the tween. We will run for frameRate * (this.duration / 1000) (ms)
|
|
|
|
var time = 0;
|
|
|
|
var total = Math.floor(frameRate * (this.duration / 1000));
|
|
|
|
var tick = this.duration / total;
|
|
|
|
|
|
|
|
var output = [];
|
|
|
|
|
|
|
|
while (total--)
|
2013-12-30 16:54:00 +00:00
|
|
|
{
|
2014-11-18 20:58:25 +00:00
|
|
|
var property;
|
|
|
|
|
|
|
|
var percent = (time - this.startTime) / this.duration;
|
|
|
|
percent = percent > 1 ? 1 : percent;
|
|
|
|
|
|
|
|
var value = this.easingFunction(percent);
|
|
|
|
var blob = {};
|
|
|
|
|
|
|
|
for (property in this._valuesEnd)
|
2013-12-30 16:54:00 +00:00
|
|
|
{
|
2014-11-18 20:58:25 +00:00
|
|
|
var start = this._valuesStart[property] || 0;
|
|
|
|
var end = this._valuesEnd[property];
|
|
|
|
|
|
|
|
if (end instanceof Array)
|
2013-12-30 16:54:00 +00:00
|
|
|
{
|
2014-11-18 20:58:25 +00:00
|
|
|
blob[property] = this.interpolationFunction(end, value);
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2014-11-18 20:58:25 +00:00
|
|
|
else
|
2013-12-30 16:54:00 +00:00
|
|
|
{
|
2014-11-18 20:58:25 +00:00
|
|
|
if (typeof end === 'string')
|
2013-12-18 13:01:45 +00:00
|
|
|
{
|
2014-11-18 20:58:25 +00:00
|
|
|
// Parses relative end values with start as base (e.g.: +10, -3)
|
|
|
|
end = start + parseFloat(end, 10);
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2014-11-18 20:58:25 +00:00
|
|
|
else if (typeof end === 'number')
|
2013-12-18 13:01:45 +00:00
|
|
|
{
|
2014-11-18 20:58:25 +00:00
|
|
|
// Protect against non numeric properties.
|
|
|
|
blob[property] = start + (end - start) * value;
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2014-05-01 01:40:36 +00:00
|
|
|
}
|
2014-11-18 20:58:25 +00:00
|
|
|
}
|
2014-05-01 01:40:36 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
output.push(blob);
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
time += tick;
|
|
|
|
}
|
2013-09-06 19:20:58 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
var blob = {};
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
for (property in this._valuesEnd)
|
|
|
|
{
|
|
|
|
blob[property] = this._valuesEnd[property];
|
|
|
|
}
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
output.push(blob);
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
if (this.yoyo)
|
|
|
|
{
|
|
|
|
var reversed = output.slice();
|
|
|
|
reversed.reverse();
|
|
|
|
output = output.concat(reversed);
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2014-11-18 20:58:25 +00:00
|
|
|
if (typeof data !== 'undefined')
|
|
|
|
{
|
|
|
|
data = data.concat(output);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return output;
|
|
|
|
}
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-08-28 23:09:12 +00:00
|
|
|
};
|
2013-12-30 16:54:00 +00:00
|
|
|
|
|
|
|
Phaser.Tween.prototype.constructor = Phaser.Tween;
|