phaser/docs/Tween.js.html

1132 lines
26 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Phaser Source: tween/Tween.js</title>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/sunlight.default.css">
<link type="text/css" rel="stylesheet" href="styles/site.cerulean.css">
</head>
<body>
<div class="container-fluid">
<div class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<a class="brand" href="index.html">Phaser</a>
<ul class="nav">
<li class="dropdown">
<a href="namespaces.list.html" class="dropdown-toggle" data-toggle="dropdown">Namespaces<b
class="caret"></b></a>
<ul class="dropdown-menu ">
<li>
<a href="Phaser.html">Phaser</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b
class="caret"></b></a>
<ul class="dropdown-menu ">
<li>
<a href="Phaser.Animation.html">Animation</a>
</li>
<li>
<a href="Phaser.AnimationManager.html">AnimationManager</a>
</li>
<li>
<a href="Phaser.AnimationParser.html">AnimationParser</a>
</li>
<li>
<a href="Phaser.BitmapData.html">BitmapData</a>
</li>
<li>
<a href="Phaser.BitmapFont.html">BitmapFont</a>
</li>
<li>
<a href="Phaser.BitmapText.html">BitmapText</a>
</li>
<li>
<a href="Phaser.Button.html">Button</a>
</li>
<li>
<a href="Phaser.Cache.html">Cache</a>
</li>
<li>
<a href="Phaser.Camera.html">Camera</a>
</li>
<li>
<a href="Phaser.Canvas.html">Canvas</a>
</li>
<li>
<a href="Phaser.Circle.html">Circle</a>
</li>
<li>
<a href="Phaser.Color.html">Color</a>
</li>
<li>
<a href="Phaser.Device.html">Device</a>
</li>
<li>
<a href="Phaser.Easing.html">Easing</a>
</li>
<li>
<a href="Phaser.Easing.Back.html">Back</a>
</li>
<li>
<a href="Phaser.Easing.Bounce.html">Bounce</a>
</li>
<li>
<a href="Phaser.Easing.Circular.html">Circular</a>
</li>
<li>
<a href="Phaser.Easing.Cubic.html">Cubic</a>
</li>
<li>
<a href="Phaser.Easing.Elastic.html">Elastic</a>
</li>
<li>
<a href="Phaser.Easing.Exponential.html">Exponential</a>
</li>
<li>
<a href="Phaser.Easing.Linear.html">Linear</a>
</li>
<li>
<a href="Phaser.Easing.Quadratic.html">Quadratic</a>
</li>
<li>
<a href="Phaser.Easing.Quartic.html">Quartic</a>
</li>
<li>
<a href="Phaser.Easing.Quintic.html">Quintic</a>
</li>
<li>
<a href="Phaser.Easing.Sinusoidal.html">Sinusoidal</a>
</li>
<li>
<a href="Phaser.Ellipse.html">Ellipse</a>
</li>
<li>
<a href="Phaser.Events.html">Events</a>
</li>
<li>
<a href="Phaser.Filter.html">Filter</a>
</li>
<li>
<a href="Phaser.Frame.html">Frame</a>
</li>
<li>
<a href="Phaser.FrameData.html">FrameData</a>
</li>
<li>
<a href="Phaser.Game.html">Game</a>
</li>
<li>
<a href="Phaser.GameObjectCreator.html">GameObjectCreator</a>
</li>
<li>
<a href="Phaser.GameObjectFactory.html">GameObjectFactory</a>
</li>
<li>
<a href="Phaser.Gamepad.html">Gamepad</a>
</li>
<li>
<a href="Phaser.GamepadButton.html">GamepadButton</a>
</li>
<li>
<a href="Phaser.Graphics.html">Graphics</a>
</li>
<li>
<a href="Phaser.Group.html">Group</a>
</li>
<li>
<a href="Phaser.Image.html">Image</a>
</li>
<li>
<a href="Phaser.Input.html">Input</a>
</li>
<li>
<a href="Phaser.InputHandler.html">InputHandler</a>
</li>
<li>
<a href="Phaser.Key.html">Key</a>
</li>
<li>
<a href="Phaser.Keyboard.html">Keyboard</a>
</li>
<li>
<a href="Phaser.Line.html">Line</a>
</li>
<li>
<a href="Phaser.LinkedList.html">LinkedList</a>
</li>
<li>
<a href="Phaser.Loader.html">Loader</a>
</li>
<li>
<a href="Phaser.LoaderParser.html">LoaderParser</a>
</li>
<li>
<a href="Phaser.Math.html">Math</a>
</li>
<li>
<a href="Phaser.Mouse.html">Mouse</a>
</li>
<li>
<a href="Phaser.MSPointer.html">MSPointer</a>
</li>
<li>
<a href="Phaser.Net.html">Net</a>
</li>
<li>
<a href="Phaser.Particles.html">Particles</a>
</li>
<li>
<a href="Phaser.Particles.Arcade.Emitter.html">Emitter</a>
</li>
<li>
<a href="Phaser.Physics.html">Physics</a>
</li>
<li>
<a href="Phaser.Physics.Arcade.html">Arcade</a>
</li>
<li>
<a href="Phaser.Physics.Body.html">Body</a>
</li>
<li>
<a href="Phaser.Physics.CollisionGroup.html">CollisionGroup</a>
</li>
<li>
<a href="Phaser.Physics.ContactMaterial.html">ContactMaterial</a>
</li>
<li>
<a href="Phaser.Physics.InversePointProxy.html">InversePointProxy</a>
</li>
<li>
<a href="Phaser.Physics.Material.html">Material</a>
</li>
<li>
<a href="Phaser.Physics.PointProxy.html">PointProxy</a>
</li>
<li>
<a href="Phaser.Physics.Spring.html">Spring</a>
</li>
<li>
<a href="Phaser.Physics.World.html">World</a>
</li>
<li>
<a href="Phaser.Plugin.html">Plugin</a>
</li>
<li>
<a href="Phaser.PluginManager.html">PluginManager</a>
</li>
<li>
<a href="Phaser.Point.html">Point</a>
</li>
<li>
<a href="Phaser.Pointer.html">Pointer</a>
</li>
<li>
<a href="Phaser.Polygon.html">Polygon</a>
</li>
<li>
<a href="Phaser.RandomDataGenerator.html">RandomDataGenerator</a>
</li>
<li>
<a href="Phaser.Rectangle.html">Rectangle</a>
</li>
<li>
<a href="Phaser.RenderTexture.html">RenderTexture</a>
</li>
<li>
<a href="Phaser.RequestAnimationFrame.html">RequestAnimationFrame</a>
</li>
<li>
<a href="Phaser.Signal.html">Signal</a>
</li>
<li>
<a href="Phaser.SinglePad.html">SinglePad</a>
</li>
<li>
<a href="Phaser.Sound.html">Sound</a>
</li>
<li>
<a href="Phaser.SoundManager.html">SoundManager</a>
</li>
<li>
<a href="Phaser.Sprite.html">Sprite</a>
</li>
<li>
<a href="Phaser.SpriteBatch.html">SpriteBatch</a>
</li>
<li>
<a href="Phaser.Stage.html">Stage</a>
</li>
<li>
<a href="Phaser.StageScaleMode.html">StageScaleMode</a>
</li>
<li>
<a href="Phaser.State.html">State</a>
</li>
<li>
<a href="Phaser.StateManager.html">StateManager</a>
</li>
<li>
<a href="Phaser.Text.html">Text</a>
</li>
<li>
<a href="Phaser.Tile.html">Tile</a>
</li>
<li>
<a href="Phaser.Tilemap.html">Tilemap</a>
</li>
<li>
<a href="Phaser.TilemapLayer.html">TilemapLayer</a>
</li>
<li>
<a href="Phaser.TilemapParser.html">TilemapParser</a>
</li>
<li>
<a href="Phaser.Tileset.html">Tileset</a>
</li>
<li>
<a href="Phaser.TileSprite.html">TileSprite</a>
</li>
<li>
<a href="Phaser.Time.html">Time</a>
</li>
<li>
<a href="Phaser.Timer.html">Timer</a>
</li>
<li>
<a href="Phaser.TimerEvent.html">TimerEvent</a>
</li>
<li>
<a href="Phaser.Touch.html">Touch</a>
</li>
<li>
<a href="Phaser.Tween.html">Tween</a>
</li>
<li>
<a href="Phaser.TweenManager.html">TweenManager</a>
</li>
<li>
<a href="Phaser.Utils.html">Utils</a>
</li>
<li>
<a href="Phaser.Utils.Debug.html">Debug</a>
</li>
<li>
<a href="Phaser.World.html">World</a>
</li>
<li>
<a href="SignalBinding.html">SignalBinding</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="global.html" class="dropdown-toggle" data-toggle="dropdown">Global<b
class="caret"></b></a>
<ul class="dropdown-menu ">
<li>
<a href="global.html#canUseNewCanvasBlendModes">canUseNewCanvasBlendModes</a>
</li>
<li>
<a href="global.html#getBounds">getBounds</a>
</li>
<li>
<a href="global.html#getNextPowerOfTwo">getNextPowerOfTwo</a>
</li>
<li>
<a href="global.html#hex2rgb">hex2rgb</a>
</li>
<li>
<a href="global.html#hitTest">hitTest</a>
</li>
<li>
<a href="global.html#rgb2hex">rgb2hex</a>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="row-fluid">
<div class="span12">
<div id="main">
<h1 class="page-title">Source: tween/Tween.js</h1>
<section>
<article>
<pre class="sunlight-highlight-javascript linenums">/**
* @author Richard Davey &lt;rich@photonstorm.com>
* @copyright 2014 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* Tween constructor
* Create a new &lt;code>Tween&lt;/code>.
*
* @class Phaser.Tween
* @constructor
* @param {object} object - Target object will be affected by this tween.
* @param {Phaser.Game} game - Current game instance.
*/
Phaser.Tween = function (object, game) {
/**
* Reference to the target object.
* @property {object} _object
* @private
*/
this._object = object;
/**
* @property {Phaser.Game} game - A reference to the currently running Game.
*/
this.game = game;
/**
* @property {Phaser.TweenManager} _manager - Reference to the TweenManager.
* @private
*/
this._manager = this.game.tweens;
/**
* @property {object} _valuesStart - Private value object.
* @private
*/
this._valuesStart = {};
/**
* @property {object} _valuesEnd - Private value object.
* @private
*/
this._valuesEnd = {};
/**
* @property {object} _valuesStartRepeat - Private value object.
* @private
*/
this._valuesStartRepeat = {};
/**
* @property {number} _duration - Private duration counter.
* @private
* @default
*/
this._duration = 1000;
/**
* @property {number} _repeat - Private repeat counter.
* @private
* @default
*/
this._repeat = 0;
/**
* @property {boolean} _yoyo - Private yoyo flag.
* @private
* @default
*/
this._yoyo = false;
/**
* @property {boolean} _reversed - Private reversed flag.
* @private
* @default
*/
this._reversed = false;
/**
* @property {number} _delayTime - Private delay counter.
* @private
* @default
*/
this._delayTime = 0;
/**
* @property {number} _startTime - Private start time counter.
* @private
* @default null
*/
this._startTime = null;
/**
* @property {function} _easingFunction - The easing function used for the tween.
* @private
*/
this._easingFunction = Phaser.Easing.Linear.None;
/**
* @property {function} _interpolationFunction - The interpolation function used for the tween.
* @private
*/
this._interpolationFunction = Phaser.Math.linearInterpolation;
/**
* @property {array} _chainedTweens - A private array of chained tweens.
* @private
*/
this._chainedTweens = [];
/**
* @property {boolean} _onStartCallbackFired - Private flag.
* @private
* @default
*/
this._onStartCallbackFired = false;
/**
* @property {function} _onUpdateCallback - An onUpdate callback.
* @private
* @default null
*/
this._onUpdateCallback = null;
/**
* @property {object} _onUpdateCallbackContext - The context in which to call the onUpdate callback.
* @private
* @default null
*/
this._onUpdateCallbackContext = null;
/**
* @property {number} _pausedTime - Private pause timer.
* @private
* @default
*/
this._pausedTime = 0;
/**
* @property {boolean} pendingDelete - If this tween is ready to be deleted by the TweenManager.
* @default
*/
this.pendingDelete = false;
// Set all starting values present on the target object
for (var field in object)
{
this._valuesStart[field] = parseFloat(object[field], 10);
}
/**
* @property {Phaser.Signal} onStart - The onStart event is fired when the Tween begins.
*/
this.onStart = new Phaser.Signal();
/**
* @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.
*/
this.onComplete = new Phaser.Signal();
/**
* @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.
* @default
*/
this.isRunning = false;
};
Phaser.Tween.prototype = {
/**
* Configure the Tween
*
* @method Phaser.Tween#to
* @param {object} properties - Properties you want to tween.
* @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.
* @param {boolean} [repeat=0] - Should the tween automatically restart once complete? (ignores any chained tweens).
* @param {boolean} [yoyo=false] - A tween that yoyos will reverse itself when it completes.
* @return {Phaser.Tween} This Tween object.
*/
to: function (properties, duration, ease, autoStart, delay, repeat, yoyo) {
duration = duration || 1000;
ease = ease || null;
autoStart = autoStart || false;
delay = delay || 0;
repeat = repeat || 0;
yoyo = yoyo || false;
var self;
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;
self._duration = duration;
self._valuesEnd = properties;
if (ease !== null)
{
self._easingFunction = ease;
}
if (delay > 0)
{
self._delayTime = delay;
}
self._yoyo = yoyo;
if (autoStart)
{
return this.start();
}
else
{
return this;
}
},
/**
* 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 () {
if (this.game === null || this._object === null)
{
return;
}
this._manager.add(this);
this.isRunning = true;
this._onStartCallbackFired = false;
this._startTime = this.game.time.now + this._delayTime;
for (var property in this._valuesEnd)
{
// check if an Array was provided as property value
if (this._valuesEnd[property] instanceof Array)
{
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 ((this._valuesStart[property] instanceof Array) === false)
{
this._valuesStart[property] *= 1.0; // Ensures we're using numbers, not strings
}
this._valuesStartRepeat[property] = this._valuesStart[property] || 0;
}
return this;
},
/**
* 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 () {
this.isRunning = false;
this._onUpdateCallback = null;
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.
*/
delay: function (amount) {
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.
*/
repeat: function (times) {
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.
*/
yoyo: function(yoyo) {
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.
*/
easing: function (easing) {
this._easingFunction = easing;
return this;
},
/**
* Set interpolation function the tween will use, by default it uses Phaser.Math.linearInterpolation.
* Also available: Phaser.Math.bezierInterpolation and Phaser.Math.catmullRomInterpolation.
*
* @method Phaser.Tween#interpolation
* @param {function} interpolation - The interpolation function to use (Phaser.Math.linearInterpolation by default)
* @return {Phaser.Tween} Itself.
*/
interpolation: function (interpolation) {
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.
*/
onUpdateCallback: function (callback, callbackContext) {
this._onUpdateCallback = callback;
this._onUpdateCallbackContext = callbackContext;
return this;
},
/**
* Pauses the tween.
*
* @method Phaser.Tween#pause
*/
pause: function () {
this._paused = true;
this._pausedTime = this.game.time.now;
},
/**
* Resumes a paused tween.
*
* @method Phaser.Tween#resume
*/
resume: function () {
this._paused = false;
this._startTime += (this.game.time.now - this._pausedTime);
},
/**
* 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).
*/
update: function (time) {
if (this.pendingDelete)
{
return false;
}
if (this._paused || time &lt; this._startTime)
{
return true;
}
var property;
if (time &lt; this._startTime)
{
return true;
}
if (this._onStartCallbackFired === false)
{
this.onStart.dispatch(this._object);
this._onStartCallbackFired = true;
}
var elapsed = (time - this._startTime) / this._duration;
elapsed = elapsed > 1 ? 1 : elapsed;
var value = this._easingFunction(elapsed);
for (property in this._valuesEnd)
{
var start = this._valuesStart[property] || 0;
var end = this._valuesEnd[property];
if (end instanceof Array)
{
this._object[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')
{
this._object[property] = start + ( end - start ) * value;
}
}
}
if (this._onUpdateCallback !== null)
{
this._onUpdateCallback.call(this._onUpdateCallbackContext, this, value);
}
if (elapsed == 1)
{
if (this._repeat > 0)
{
if (isFinite(this._repeat))
{
this._repeat--;
}
// reassign starting values, restart by making startTime = now
for (property in this._valuesStartRepeat)
{
if (typeof(this._valuesEnd[property]) === 'string')
{
this._valuesStartRepeat[property] = this._valuesStartRepeat[property] + parseFloat(this._valuesEnd[property], 10);
}
if (this._yoyo)
{
var tmp = this._valuesStartRepeat[property];
this._valuesStartRepeat[property] = this._valuesEnd[property];
this._valuesEnd[property] = tmp;
this._reversed = !this._reversed;
}
this._valuesStart[property] = this._valuesStartRepeat[property];
}
this._startTime = time + this._delayTime;
this.onLoop.dispatch(this._object);
return true;
}
else
{
this.isRunning = false;
this.onComplete.dispatch(this._object);
for (var i = 0, numChainedTweens = this._chainedTweens.length; i &lt; numChainedTweens; i ++)
{
this._chainedTweens[i].start(time);
}
return false;
}
}
return true;
}
};
Phaser.Tween.prototype.constructor = Phaser.Tween;
</pre>
</article>
</section>
</div>
<div class="clearfix"></div>
<footer>
<span class="copyright">
Phaser Copyright © 2012-2014 Photon Storm Ltd.
</span>
<br />
<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-dev</a>
on Fri Feb 21 2014 15:36:22 GMT-0000 (GMT) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
</div>
<br clear="both">
</div>
</div>
<script src="scripts/sunlight.js"></script>
<script src="scripts/sunlight.javascript.js"></script>
<script src="scripts/sunlight-plugin.doclinks.js"></script>
<script src="scripts/sunlight-plugin.linenumbers.js"></script>
<script src="scripts/sunlight-plugin.menu.js"></script>
<script src="scripts/jquery.min.js"></script>
<script src="scripts/jquery.scrollTo.js"></script>
<script src="scripts/jquery.localScroll.js"></script>
<script src="scripts/bootstrap-dropdown.js"></script>
<script src="scripts/toc.js"></script>
<script> Sunlight.highlightAll({lineNumbers:true, showMenu: true, enableDoclinks :true}); </script>
<script>
$( function () {
$( "#toc" ).toc( {
selectors : "h1,h2,h3,h4",
showAndHide : false,
scrollTo : 60
} );
$( "#toc>ul" ).addClass( "nav nav-pills nav-stacked" );
$( "#main span[id^='toc']" ).addClass( "toc-shim" );
} );
</script>
</body>
</html>