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
|
|
|
/**
|
|
|
|
* Tween constructor
|
|
|
|
* Create a new <code>Tween</code>.
|
|
|
|
*
|
2013-10-01 12:54:29 +00:00
|
|
|
* @class Phaser.Tween
|
|
|
|
* @constructor
|
|
|
|
* @param {object} object - Target object will be affected by this tween.
|
|
|
|
* @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-03-07 01:26:09 +00:00
|
|
|
Phaser.Tween = function (object, game, manager) {
|
2013-08-28 23:09:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference to the target object.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @property {object} _object
|
|
|
|
* @private
|
2013-08-28 23:09:12 +00:00
|
|
|
*/
|
2013-11-25 04:40:04 +00:00
|
|
|
this._object = object;
|
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
|
|
|
|
|
|
|
/**
|
2013-12-18 13:01:45 +00:00
|
|
|
* @property {Phaser.TweenManager} _manager - Reference to the TweenManager.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2014-03-07 01:26:09 +00:00
|
|
|
this._manager = manager;
|
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 {object} _valuesStart - Private value object.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._valuesStart = {};
|
|
|
|
|
|
|
|
/**
|
2013-12-18 13:01:45 +00:00
|
|
|
* @property {object} _valuesEnd - Private value object.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._valuesEnd = {};
|
|
|
|
|
|
|
|
/**
|
2013-12-18 13:01:45 +00:00
|
|
|
* @property {object} _valuesStartRepeat - Private value object.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._valuesStartRepeat = {};
|
|
|
|
|
|
|
|
/**
|
2013-12-18 13:01:45 +00:00
|
|
|
* @property {number} _duration - Private duration counter.
|
2013-10-03 01:38:35 +00:00
|
|
|
* @private
|
2013-10-01 12:54:29 +00:00
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this._duration = 1000;
|
|
|
|
|
|
|
|
/**
|
2013-12-18 13:01:45 +00:00
|
|
|
* @property {number} _repeat - Private repeat counter.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @private
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this._repeat = 0;
|
|
|
|
|
|
|
|
/**
|
2013-12-18 13:01:45 +00:00
|
|
|
* @property {boolean} _yoyo - Private yoyo flag.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @private
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this._yoyo = false;
|
|
|
|
|
|
|
|
/**
|
2013-12-18 13:01:45 +00:00
|
|
|
* @property {boolean} _reversed - Private reversed flag.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @private
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this._reversed = false;
|
|
|
|
|
|
|
|
/**
|
2013-12-18 13:01:45 +00:00
|
|
|
* @property {number} _delayTime - Private delay counter.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @private
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this._delayTime = 0;
|
|
|
|
|
|
|
|
/**
|
2013-12-18 13:01:45 +00:00
|
|
|
* @property {number} _startTime - Private start time counter.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @private
|
|
|
|
* @default null
|
|
|
|
*/
|
|
|
|
this._startTime = null;
|
|
|
|
|
|
|
|
/**
|
2013-12-18 13:01:45 +00:00
|
|
|
* @property {function} _easingFunction - The easing function used for the tween.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._easingFunction = Phaser.Easing.Linear.None;
|
|
|
|
|
|
|
|
/**
|
2013-12-18 13:01:45 +00:00
|
|
|
* @property {function} _interpolationFunction - The interpolation function used for the tween.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._interpolationFunction = Phaser.Math.linearInterpolation;
|
|
|
|
|
|
|
|
/**
|
2013-12-18 13:01:45 +00:00
|
|
|
* @property {array} _chainedTweens - A private array of chained tweens.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._chainedTweens = [];
|
|
|
|
|
|
|
|
/**
|
2013-12-30 16:54:00 +00:00
|
|
|
* @property {boolean} _onStartCallbackFired - Private flag.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @private
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
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;
|
2013-12-30 16:54:00 +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
|
|
|
/**
|
2013-10-03 01:38:35 +00:00
|
|
|
* @property {boolean} pendingDelete - If this tween is ready to be deleted by the TweenManager.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @default
|
|
|
|
*/
|
2013-09-19 03:45:08 +00:00
|
|
|
this.pendingDelete = false;
|
|
|
|
|
2014-03-03 02:40:59 +00:00
|
|
|
// Set all starting values present on the target object - why? this will copy loads of properties we don't need - commenting out for now
|
|
|
|
// for (var field in object)
|
|
|
|
// {
|
|
|
|
// this._valuesStart[field] = parseFloat(object[field], 10);
|
|
|
|
// }
|
2013-10-01 12:54:29 +00:00
|
|
|
|
|
|
|
/**
|
2013-12-18 13:01:45 +00:00
|
|
|
* @property {Phaser.Signal} onStart - The onStart event is fired when the Tween begins.
|
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
|
|
|
|
|
|
|
/**
|
2013-12-18 13:01:45 +00:00
|
|
|
* @property {Phaser.Signal} onLoop - The onLoop event is fired if the Tween loops.
|
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
2013-12-18 13:01:45 +00:00
|
|
|
* @property {boolean} isRunning - If the tween is running this is set to true, otherwise false. Tweens that are in a delayed state, 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;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.Tween.prototype = {
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* Configure the Tween
|
|
|
|
*
|
|
|
|
* @method Phaser.Tween#to
|
|
|
|
* @param {object} properties - Properties you want to tween.
|
2013-12-18 13:01:45 +00:00
|
|
|
* @param {number} [duration=1000] - Duration of this tween in ms.
|
|
|
|
* @param {function} [ease=null] - Easing function. If not set it will default to Phaser.Easing.Linear.None.
|
|
|
|
* @param {boolean} [autoStart=false] - Whether this tween will start automatically or not.
|
|
|
|
* @param {number} [delay=0] - Delay before this tween will start, defaults to 0 (no delay). Value given is in ms.
|
2014-03-23 23:57:46 +00:00
|
|
|
* @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.
|
2013-12-18 13:01:45 +00:00
|
|
|
* @param {boolean} [yoyo=false] - A tween that yoyos will reverse itself when it completes.
|
|
|
|
* @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
|
|
|
|
|
|
|
duration = duration || 1000;
|
|
|
|
ease = ease || null;
|
|
|
|
autoStart = autoStart || false;
|
|
|
|
delay = delay || 0;
|
|
|
|
repeat = repeat || 0;
|
|
|
|
yoyo = yoyo || false;
|
|
|
|
|
|
|
|
var self;
|
2013-12-18 13:01:45 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
if (this._parent)
|
|
|
|
{
|
|
|
|
self = this._manager.create(this._object);
|
|
|
|
this._lastChild.chain(self);
|
|
|
|
this._lastChild = self;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
self = this;
|
|
|
|
this._parent = this;
|
|
|
|
this._lastChild = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
self._repeat = repeat;
|
2013-09-20 23:14:50 +00:00
|
|
|
self._duration = duration;
|
2013-11-25 04:40:04 +00:00
|
|
|
self._valuesEnd = properties;
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-09-03 16:07:05 +00:00
|
|
|
if (ease !== null)
|
|
|
|
{
|
2013-09-20 23:14:50 +00:00
|
|
|
self._easingFunction = ease;
|
2013-08-28 23:09:12 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 16:07:05 +00:00
|
|
|
if (delay > 0)
|
|
|
|
{
|
2013-09-20 23:14:50 +00:00
|
|
|
self._delayTime = delay;
|
2013-08-28 23:09:12 +00:00
|
|
|
}
|
|
|
|
|
2013-09-20 23:14:50 +00:00
|
|
|
self._yoyo = yoyo;
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
if (autoStart)
|
|
|
|
{
|
2013-10-11 03:42:11 +00:00
|
|
|
return this.start();
|
2013-12-30 16:54:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-11 03:42:11 +00:00
|
|
|
return this;
|
2013-08-28 23:09:12 +00:00
|
|
|
}
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
},
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* Starts the tween running. Can also be called by the autoStart parameter of Tween.to.
|
|
|
|
*
|
|
|
|
* @method Phaser.Tween#start
|
|
|
|
* @return {Phaser.Tween} Itself.
|
|
|
|
*/
|
|
|
|
start: function () {
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
if (this.game === null || this._object === null)
|
|
|
|
{
|
2013-08-28 23:09:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
this._manager.add(this);
|
2013-08-30 16:09:43 +00:00
|
|
|
|
2013-08-28 23:09:12 +00:00
|
|
|
this.isRunning = true;
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
this._onStartCallbackFired = false;
|
2013-08-28 23:09:12 +00:00
|
|
|
|
|
|
|
this._startTime = this.game.time.now + this._delayTime;
|
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
for (var property in this._valuesEnd)
|
|
|
|
{
|
2013-11-25 04:40:04 +00:00
|
|
|
// check if an Array was provided as property value
|
2014-03-03 02:40:59 +00:00
|
|
|
if (Array.isArray(this._valuesEnd[property]))
|
2013-12-30 16:54:00 +00:00
|
|
|
{
|
|
|
|
if (this._valuesEnd[property].length === 0)
|
|
|
|
{
|
2013-11-25 04:40:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
// create a local copy of the Array with the start value at the front
|
2013-12-30 16:54:00 +00:00
|
|
|
this._valuesEnd[property] = [this._object[property]].concat(this._valuesEnd[property]);
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
this._valuesStart[property] = this._object[property];
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2014-03-03 02:40:59 +00:00
|
|
|
if (!Array.isArray(this._valuesStart[property]))
|
2013-12-30 16:54:00 +00:00
|
|
|
{
|
|
|
|
this._valuesStart[property] *= 1.0; // Ensures we're using numbers, not strings
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
this._valuesStartRepeat[property] = this._valuesStart[property] || 0;
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
return this;
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
},
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2014-03-03 22:43:35 +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.
|
2014-03-04 01:27:57 +00:00
|
|
|
* @param {array} [data] - If given the generated data will be appended to this array, otherwise a new array will be returned.
|
2014-03-03 22:43:35 +00:00
|
|
|
* @return {array} An array of tweened values.
|
|
|
|
*/
|
2014-03-04 01:27:57 +00:00
|
|
|
generateData: function (frameRate, data) {
|
2014-03-03 22:43:35 +00:00
|
|
|
|
|
|
|
if (this.game === null || this._object === 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)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a local copy of the Array with the start value at the front
|
|
|
|
this._valuesEnd[property] = [this._object[property]].concat(this._valuesEnd[property]);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._valuesStart[property] = this._object[property];
|
|
|
|
|
|
|
|
if (!Array.isArray(this._valuesStart[property]))
|
|
|
|
{
|
|
|
|
this._valuesStart[property] *= 1.0; // Ensures we're using numbers, not strings
|
|
|
|
}
|
|
|
|
|
|
|
|
this._valuesStartRepeat[property] = this._valuesStart[property] || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simulate the tween. We will run for frameRate * (this._duration / 1000) (ms)
|
|
|
|
var time = 0;
|
2014-03-25 02:53:30 +00:00
|
|
|
var total = Math.floor(frameRate * (this._duration / 1000));
|
2014-03-03 22:43:35 +00:00
|
|
|
var tick = this._duration / total;
|
|
|
|
|
|
|
|
var output = [];
|
|
|
|
|
|
|
|
while (total--)
|
|
|
|
{
|
|
|
|
var property;
|
|
|
|
|
|
|
|
var elapsed = (time - this._startTime) / this._duration;
|
|
|
|
elapsed = elapsed > 1 ? 1 : elapsed;
|
|
|
|
|
|
|
|
var value = this._easingFunction(elapsed);
|
|
|
|
var blob = {};
|
|
|
|
|
|
|
|
for (property in this._valuesEnd)
|
|
|
|
{
|
|
|
|
var start = this._valuesStart[property] || 0;
|
|
|
|
var end = this._valuesEnd[property];
|
|
|
|
|
|
|
|
if (end instanceof Array)
|
|
|
|
{
|
|
|
|
blob[property] = this._interpolationFunction(end, value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Parses relative end values with start as base (e.g.: +10, -3)
|
|
|
|
if (typeof(end) === 'string')
|
|
|
|
{
|
|
|
|
end = start + parseFloat(end, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
// protect against non numeric properties.
|
|
|
|
if (typeof(end) === 'number')
|
|
|
|
{
|
|
|
|
blob[property] = start + ( end - start ) * value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
output.push(blob);
|
|
|
|
|
|
|
|
time += tick;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._yoyo)
|
|
|
|
{
|
|
|
|
var reversed = output.slice();
|
|
|
|
reversed.reverse();
|
|
|
|
output = output.concat(reversed);
|
|
|
|
}
|
|
|
|
|
2014-03-04 01:27:57 +00:00
|
|
|
if (typeof data !== 'undefined')
|
|
|
|
{
|
|
|
|
data = data.concat(output);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return output;
|
|
|
|
}
|
2014-03-03 22:43:35 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* Stops the tween if running and removes it from the TweenManager. If there are any onComplete callbacks or events they are not dispatched.
|
|
|
|
*
|
|
|
|
* @method Phaser.Tween#stop
|
|
|
|
* @return {Phaser.Tween} Itself.
|
|
|
|
*/
|
|
|
|
stop: function () {
|
2013-08-28 23:09:12 +00:00
|
|
|
|
|
|
|
this.isRunning = false;
|
|
|
|
|
2014-02-05 02:39:03 +00:00
|
|
|
this._onUpdateCallback = null;
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
this._manager.remove(this);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a delay time before this tween will start.
|
|
|
|
*
|
|
|
|
* @method Phaser.Tween#delay
|
|
|
|
* @param {number} amount - The amount of the delay in ms.
|
|
|
|
* @return {Phaser.Tween} Itself.
|
|
|
|
*/
|
2013-12-30 16:54:00 +00:00
|
|
|
delay: function (amount) {
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
this._delayTime = amount;
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the number of times this tween will repeat.
|
|
|
|
*
|
|
|
|
* @method Phaser.Tween#repeat
|
|
|
|
* @param {number} times - How many times to repeat.
|
|
|
|
* @return {Phaser.Tween} Itself.
|
|
|
|
*/
|
2013-12-30 16:54:00 +00:00
|
|
|
repeat: function (times) {
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
this._repeat = times;
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A tween that has yoyo set to true will run through from start to finish, then reverse from finish to start.
|
|
|
|
* Used in combination with repeat you can create endless loops.
|
|
|
|
*
|
|
|
|
* @method Phaser.Tween#yoyo
|
|
|
|
* @param {boolean} yoyo - Set to true to yoyo this tween.
|
|
|
|
* @return {Phaser.Tween} Itself.
|
|
|
|
*/
|
2013-12-30 16:54:00 +00:00
|
|
|
yoyo: function(yoyo) {
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
this._yoyo = yoyo;
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set easing function this tween will use, i.e. Phaser.Easing.Linear.None.
|
|
|
|
*
|
|
|
|
* @method Phaser.Tween#easing
|
|
|
|
* @param {function} easing - The easing function this tween will use, i.e. Phaser.Easing.Linear.None.
|
|
|
|
* @return {Phaser.Tween} Itself.
|
|
|
|
*/
|
2013-12-30 16:54:00 +00:00
|
|
|
easing: function (easing) {
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
this._easingFunction = easing;
|
|
|
|
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)
|
|
|
|
* @return {Phaser.Tween} Itself.
|
|
|
|
*/
|
2013-12-30 16:54:00 +00:00
|
|
|
interpolation: function (interpolation) {
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
this._interpolationFunction = interpolation;
|
|
|
|
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
|
|
|
|
* @return {Phaser.Tween} Itself.
|
|
|
|
*/
|
|
|
|
chain: function () {
|
|
|
|
|
|
|
|
this._chainedTweens = arguments;
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loop a chain of tweens
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* @return {Phaser.Tween} Itself.
|
|
|
|
*/
|
|
|
|
loop: function() {
|
|
|
|
|
|
|
|
this._lastChild.chain(this);
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a callback to be fired each time this tween updates. Note: callback will be called in the context of the global scope.
|
|
|
|
*
|
|
|
|
* @method Phaser.Tween#onUpdateCallback
|
|
|
|
* @param {function} callback - The callback to invoke each time this tween is updated.
|
|
|
|
* @return {Phaser.Tween} Itself.
|
|
|
|
*/
|
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;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pauses the tween.
|
|
|
|
*
|
|
|
|
* @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 () {
|
|
|
|
|
|
|
|
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-02-25 03:33:47 +00:00
|
|
|
this._startTime += (this.game.time.now - this._pausedTime);
|
|
|
|
}
|
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
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
* @return {boolean} false if the tween has completed and should be deleted from the manager, otherwise true (still active).
|
|
|
|
*/
|
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
|
|
|
|
2013-12-18 13:01:45 +00:00
|
|
|
if (this._paused || time < this._startTime)
|
|
|
|
{
|
2013-08-28 23:09:12 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
var property;
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-12-18 13:01:45 +00:00
|
|
|
if (time < this._startTime)
|
|
|
|
{
|
2013-11-25 04:40:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-12-18 13:01:45 +00:00
|
|
|
if (this._onStartCallbackFired === false)
|
|
|
|
{
|
|
|
|
this.onStart.dispatch(this._object);
|
2013-11-25 04:40:04 +00:00
|
|
|
this._onStartCallbackFired = true;
|
|
|
|
}
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
var elapsed = (time - this._startTime) / this._duration;
|
2013-11-25 04:40:04 +00:00
|
|
|
elapsed = elapsed > 1 ? 1 : elapsed;
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
var value = this._easingFunction(elapsed);
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
for (property in this._valuesEnd)
|
|
|
|
{
|
|
|
|
var start = this._valuesStart[property] || 0;
|
|
|
|
var end = this._valuesEnd[property];
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
if (end instanceof Array)
|
|
|
|
{
|
|
|
|
this._object[property] = this._interpolationFunction(end, value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-28 23:09:12 +00:00
|
|
|
// Parses relative end values with start as base (e.g.: +10, -3)
|
2013-12-30 16:54:00 +00:00
|
|
|
if (typeof(end) === 'string')
|
|
|
|
{
|
2013-11-25 04:40:04 +00:00
|
|
|
end = start + parseFloat(end, 10);
|
|
|
|
}
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
// protect against non numeric properties.
|
2013-12-30 16:54:00 +00:00
|
|
|
if (typeof(end) === 'number')
|
|
|
|
{
|
|
|
|
this._object[property] = start + ( end - start ) * value;
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
if (this._onUpdateCallback !== null)
|
|
|
|
{
|
2014-02-05 02:39:03 +00:00
|
|
|
this._onUpdateCallback.call(this._onUpdateCallbackContext, this, value);
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
if (elapsed == 1)
|
|
|
|
{
|
|
|
|
if (this._repeat > 0)
|
|
|
|
{
|
|
|
|
if (isFinite(this._repeat))
|
|
|
|
{
|
2013-11-25 04:40:04 +00:00
|
|
|
this._repeat--;
|
|
|
|
}
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
// reassign starting values, restart by making startTime = now
|
2013-12-30 16:54:00 +00:00
|
|
|
for (property in this._valuesStartRepeat)
|
|
|
|
{
|
|
|
|
if (typeof(this._valuesEnd[property]) === 'string')
|
2013-12-18 13:01:45 +00:00
|
|
|
{
|
2013-12-30 16:54:00 +00:00
|
|
|
this._valuesStartRepeat[property] = this._valuesStartRepeat[property] + parseFloat(this._valuesEnd[property], 10);
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-12-18 13:01:45 +00:00
|
|
|
if (this._yoyo)
|
|
|
|
{
|
2013-12-30 16:54:00 +00:00
|
|
|
var tmp = this._valuesStartRepeat[property];
|
|
|
|
this._valuesStartRepeat[property] = this._valuesEnd[property];
|
|
|
|
this._valuesEnd[property] = tmp;
|
2013-11-25 04:40:04 +00:00
|
|
|
this._reversed = !this._reversed;
|
|
|
|
}
|
2013-12-18 13:01:45 +00:00
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
this._valuesStart[property] = this._valuesStartRepeat[property];
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
this._startTime = time + this._delayTime;
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-12-18 13:01:45 +00:00
|
|
|
this.onLoop.dispatch(this._object);
|
2013-09-06 19:20:58 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
return true;
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-25 04:40:04 +00:00
|
|
|
this.isRunning = false;
|
|
|
|
this.onComplete.dispatch(this._object);
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
for (var i = 0, numChainedTweens = this._chainedTweens.length; i < numChainedTweens; i ++)
|
|
|
|
{
|
|
|
|
this._chainedTweens[i].start(time);
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
return true;
|
2013-08-28 23:09:12 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
|
2013-08-28 23:09:12 +00:00
|
|
|
};
|
2013-12-30 16:54:00 +00:00
|
|
|
|
|
|
|
Phaser.Tween.prototype.constructor = Phaser.Tween;
|