phaser/src/tween/TweenManager.js

247 lines
6 KiB
JavaScript
Raw Normal View History

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 20:29:25 +00:00
/**
* Phaser - TweenManager
2014-03-23 07:59:28 +00:00
*
2013-10-01 12:54:29 +00:00
* @class Phaser.TweenManager
2014-03-23 07:59:28 +00:00
* @classdesc
2013-08-28 20:29:25 +00:00
* Phaser.Game has a single instance of the TweenManager through which all Tween objects are created and updated.
* Tweens are hooked into the game clock and pause system, adjusting based on the game state.
*
* TweenManager is based heavily on tween.js by http://soledadpenades.com.
2013-08-28 20:29:25 +00:00
* The difference being that tweens belong to a games instance of TweenManager, rather than to a global TWEEN object.
* It also has callbacks swapped for Signals and a few issues patched with regard to properties and completion errors.
* Please see https://github.com/sole/tween.js for a full list of contributors.
2013-10-01 12:54:29 +00:00
* @constructor
*
* @param {Phaser.Game} game - A reference to the currently running game.
2013-08-28 20:29:25 +00:00
*/
Phaser.TweenManager = function (game) {
/**
* @property {Phaser.Game} game - Local reference to game.
*/
this.game = game;
2014-03-23 07:59:28 +00:00
/**
* @property {array<Phaser.Tween>} _tweens - All of the currently running tweens.
* @private
*/
this._tweens = [];
2014-03-23 07:59:28 +00:00
/**
* @property {array<Phaser.Tween>} _add - All of the tweens queued to be added in the next update.
* @private
*/
this._add = [];
this.game.onPause.add(this._pauseAll, this);
this.game.onResume.add(this._resumeAll, this);
2013-08-28 20:29:25 +00:00
};
Phaser.TweenManager.prototype = {
/**
* Get all the tween objects in an array.
* @method Phaser.TweenManager#getAll
* @returns {Phaser.Tween[]} Array with all tween objects.
*/
getAll: function () {
return this._tweens;
},
/**
* Remove all tweens running and in the queue. Doesn't call any of the tween onComplete events.
* @method Phaser.TweenManager#removeAll
*/
removeAll: function () {
for (var i = 0; i < this._tweens.length; i++)
{
this._tweens[i].pendingDelete = true;
}
this._add = [];
},
/**
* Add a new tween into the TweenManager.
*
* @method Phaser.TweenManager#add
* @param {Phaser.Tween} tween - The tween object you want to add.
* @returns {Phaser.Tween} The tween object you added to the manager.
*/
2014-01-04 02:53:32 +00:00
add: function (tween) {
tween._manager = this;
2014-01-04 02:53:32 +00:00
this._add.push(tween);
},
/**
2014-03-23 07:59:28 +00:00
* Create a tween object for a specific object. The object can be any JavaScript object or Phaser object such as Sprite.
*
* @method Phaser.TweenManager#create
* @param {Object} object - Object the tween will be run on.
* @returns {Phaser.Tween} The newly created tween object.
*/
create: function (object) {
return new Phaser.Tween(object, this.game, this);
2013-08-28 20:29:25 +00:00
},
/**
* Remove a tween from this manager.
*
* @method Phaser.TweenManager#remove
* @param {Phaser.Tween} tween - The tween object you want to remove.
*/
2014-01-04 02:53:32 +00:00
remove: function (tween) {
2013-08-28 20:29:25 +00:00
2014-01-04 02:53:32 +00:00
var i = this._tweens.indexOf(tween);
2013-08-28 20:29:25 +00:00
2014-01-04 02:53:32 +00:00
if (i !== -1)
{
this._tweens[i].pendingDelete = true;
}
else
{
i = this._add.indexOf(tween);
if (i !== -1)
{
this._add[i].pendingDelete = true;
}
}
2013-08-28 20:29:25 +00:00
},
2013-08-28 20:29:25 +00:00
/**
* Update all the tween objects you added to this manager.
*
* @method Phaser.TweenManager#update
* @returns {boolean} Return false if there's no tween to update, otherwise return true.
*/
update: function () {
2013-08-28 20:29:25 +00:00
var addTweens = this._add.length;
var numTweens = this._tweens.length;
if (numTweens === 0 && addTweens === 0)
{
return false;
}
2013-08-28 20:29:25 +00:00
var i = 0;
2013-08-28 20:29:25 +00:00
2014-01-04 02:53:32 +00:00
while (i < numTweens)
{
if (this._tweens[i].update(this.game.time.now))
{
i++;
2014-01-04 02:53:32 +00:00
}
else
{
this._tweens.splice(i, 1);
2013-08-28 20:29:25 +00:00
numTweens--;
}
}
2013-08-28 20:29:25 +00:00
// If there are any new tweens to be added, do so now - otherwise they can be spliced out of the array before ever running
if (addTweens > 0)
{
this._tweens = this._tweens.concat(this._add);
this._add.length = 0;
}
return true;
2013-08-28 20:29:25 +00:00
},
2013-08-28 20:29:25 +00:00
/**
* Checks to see if a particular Sprite is currently being tweened.
*
* @method Phaser.TweenManager#isTweening
* @param {object} object - The object to check for tweens against.
* @returns {boolean} Returns true if the object is currently being tweened, false if not.
*/
isTweening: function(object) {
return this._tweens.some(function(tween) {
return tween._object === object;
});
},
/**
* Private. Called by game focus loss. Pauses all currently running tweens.
*
* @method Phaser.TweenManager#_pauseAll
* @private
*/
_pauseAll: function () {
for (var i = this._tweens.length - 1; i >= 0; i--)
{
this._tweens[i]._pause();
}
},
/**
* Private. Called by game focus loss. Resumes all currently paused tweens.
*
* @method Phaser.TweenManager#_resumeAll
* @private
*/
_resumeAll: function () {
for (var i = this._tweens.length - 1; i >= 0; i--)
{
this._tweens[i]._resume();
}
},
/**
* Pauses all currently running tweens.
*
2014-02-20 03:44:44 +00:00
* @method Phaser.TweenManager#pauseAll
*/
pauseAll: function () {
2013-08-28 20:29:25 +00:00
2014-01-04 02:53:32 +00:00
for (var i = this._tweens.length - 1; i >= 0; i--)
{
this._tweens[i].pause();
}
2013-08-28 20:29:25 +00:00
},
/**
* Resumes all currently paused tweens.
*
* @method Phaser.TweenManager#resumeAll
*/
resumeAll: function () {
2013-08-28 20:29:25 +00:00
2014-01-04 02:53:32 +00:00
for (var i = this._tweens.length - 1; i >= 0; i--)
{
this._tweens[i].resume(true);
}
2013-08-28 20:29:25 +00:00
}
};
Phaser.TweenManager.prototype.constructor = Phaser.TweenManager;