phaser/src/tween/TweenManager.js

189 lines
3.8 KiB
JavaScript
Raw Normal View History

2013-10-01 12:54:29 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
* @module Phaser.TweenManager
*/
2013-08-28 20:29:25 +00:00
/**
* Phaser - TweenManager
2013-10-01 12:54:29 +00:00
*
* @class Phaser.TweenManager
* @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.
*
2013-10-01 12:54:29 +00:00
* TweenManager is based heavily on tween.js by {@link http://soledadpenades.com|sole}.
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.
2013-10-01 12:54:29 +00:00
* Please see {@link https://github.com/sole/tween.js} for a full list of contributors.
* @constructor
*
* @param {Phaser.Game} game - A reference to the currently running game.
2013-08-28 20:29:25 +00:00
*/
Phaser.TweenManager = function (game) {
2013-10-01 12:54:29 +00:00
/**
* @property {Phaser.Game} game - Local reference to game.
*/
2013-08-28 20:29:25 +00:00
this.game = game;
2013-10-01 12:54:29 +00:00
/**
* @property {array} _tweens - Description.
* @private
*/
2013-08-28 20:29:25 +00:00
this._tweens = [];
2013-10-01 12:54:29 +00:00
/**
* @property {array} _add - Description.
* @private
*/
this._add = [];
2013-08-28 20:29:25 +00:00
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 = {
2013-10-01 12:54:29 +00:00
/**
* Description.
* @property {string} REVISION
* @default
*/
2013-08-28 20:29:25 +00:00
REVISION: '11dev',
/**
* Get all the tween objects in an array.
2013-10-01 12:54:29 +00:00
* @method getAll
* @returns {Phaser.Tween[]} Array with all tween objects.
2013-08-28 20:29:25 +00:00
*/
getAll: function () {
return this._tweens;
2013-08-28 20:29:25 +00:00
},
/**
* Remove all tween objects.
2013-10-01 12:54:29 +00:00
* @method removeAll
2013-08-28 20:29:25 +00:00
*/
removeAll: function () {
2013-10-01 12:54:29 +00:00
this._tweens = [];
2013-08-28 20:29:25 +00:00
},
/**
* Add a new tween into the TweenManager.
*
2013-10-01 12:54:29 +00:00
* @method add
* @param {Phaser.Tween} tween - The tween object you want to add.
* @returns {Phaser.Tween} The tween object you added to the manager.
2013-08-28 20:29:25 +00:00
*/
add: function ( tween ) {
this._add.push( tween );
2013-08-28 20:29:25 +00:00
},
/**
2013-10-01 12:54:29 +00:00
* Create a tween object for a specific object. The object can be any JavaScript object or Phaser object such as Sprite.
*
* @method 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);
2013-08-28 20:29:25 +00:00
},
/**
* Remove a tween from this manager.
*
2013-10-01 12:54:29 +00:00
* @method remove
* @param {Phaser.Tween} tween - The tween object you want to remove.
2013-08-28 20:29:25 +00:00
*/
remove: function ( tween ) {
var i = this._tweens.indexOf( tween );
2013-08-28 20:29:25 +00:00
if ( i !== -1 ) {
this._tweens[i].pendingDelete = true;
2013-08-28 20:29:25 +00:00
}
},
/**
* Update all the tween objects you added to this manager.
*
2013-10-01 12:54:29 +00:00
* @method update
2013-10-01 15:39:39 +00:00
* @returns {boolean} Return false if there's no tween to update, otherwise return true.
2013-08-28 20:29:25 +00:00
*/
2013-08-28 23:09:12 +00:00
update: function () {
2013-08-28 20:29:25 +00:00
if ( this._tweens.length === 0 && this._add.length === 0 ) return false;
2013-08-28 20:29:25 +00:00
var i = 0;
var numTweens = this._tweens.length;
2013-08-28 20:29:25 +00:00
while ( i < numTweens ) {
if ( this._tweens[ i ].update( this.game.time.now ) ) {
2013-08-28 20:29:25 +00:00
i++;
2013-08-28 20:29:25 +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 (this._add.length > 0)
{
this._tweens = this._tweens.concat(this._add);
this._add.length = 0;
}
2013-08-28 20:29:25 +00:00
return true;
},
/**
* Pauses all currently running tweens.
2013-10-01 12:54:29 +00:00
*
* @method update
2013-08-28 20:29:25 +00:00
*/
2013-10-01 12:54:29 +00:00
pauseAll: function () {
2013-08-28 20:29:25 +00:00
for (var i = this._tweens.length - 1; i >= 0; i--) {
this._tweens[i].pause();
};
},
/**
* Pauses all currently paused tweens.
2013-10-01 12:54:29 +00:00
*
* @method resumeAll
2013-08-28 20:29:25 +00:00
*/
2013-10-01 12:54:29 +00:00
resumeAll: function () {
2013-08-28 20:29:25 +00:00
for (var i = this._tweens.length - 1; i >= 0; i--) {
this._tweens[i].resume();
};
}
};