phaser/docs/Body.js.html

1086 lines
27 KiB
HTML
Raw Normal View History

2013-10-25 15:54:40 +00:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Phaser Source: physics/arcade/Body.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.BitmapText.html">BitmapText</a>
</li>
<li>
<a href="Phaser.Bullet.html">Bullet</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.Events.html">Events</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.GameObjectFactory.html">GameObjectFactory</a>
</li>
<li>
<a href="Phaser.Graphics.html">Graphics</a>
</li>
<li>
<a href="Phaser.Group.html">Group</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.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>
2013-10-25 16:30:37 +00:00
<li>
<a href="Phaser.Physics.Arcade.Body.html">Body</a>
</li>
2013-10-25 15:54:40 +00:00
<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.QuadTree.html">QuadTree</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.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.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.TileSprite.html">TileSprite</a>
</li>
<li>
<a href="Phaser.Time.html">Time</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#bottom">bottom</a>
</li>
<li>
<a href="global.html#HEXtoRGB">HEXtoRGB</a>
</li>
<li>
<a href="global.html#right">right</a>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="row-fluid">
<div class="span12">
<div id="main">
<h1 class="page-title">Source: physics/arcade/Body.js</h1>
<section>
<article>
2013-10-25 16:30:37 +00:00
<pre class="sunlight-highlight-javascript linenums">/**
* @author Richard Davey &lt;rich@photonstorm.com>
* @copyright 2013 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Physics Body is linked to a single Sprite. All physics operations should be performed against the body rather than
* the Sprite itself. For example you can set the velocity, acceleration, bounce values etc all on the Body.
*
* @class Phaser.Physics.Arcade.Body
* @classdesc Arcade Physics Body Constructor
* @constructor
* @param {Phaser.Sprite} sprite - The Sprite object this physics body belongs to.
*/
Phaser.Physics.Arcade.Body = function (sprite) {
2013-10-25 15:54:40 +00:00
2013-10-25 16:30:37 +00:00
/**
* @property {Phaser.Sprite} sprite - Reference to the parent Sprite.
*/
2013-10-25 15:54:40 +00:00
this.sprite = sprite;
2013-10-25 16:30:37 +00:00
/**
* @property {Phaser.Game} game - Local reference to game.
*/
2013-10-25 15:54:40 +00:00
this.game = sprite.game;
2013-10-25 16:30:37 +00:00
/**
* @property {Phaser.Point} offset - The offset of the Physics Body from the Sprite x/y position.
*/
2013-10-25 15:54:40 +00:00
this.offset = new Phaser.Point;
2013-10-25 16:30:37 +00:00
/**
* @property {number} x - The x position of the physics body.
* @readonly
*/
2013-10-25 15:54:40 +00:00
this.x = sprite.x;
2013-10-25 16:30:37 +00:00
/**
* @property {number} y - The y position of the physics body.
* @readonly
*/
2013-10-25 15:54:40 +00:00
this.y = sprite.y;
2013-10-25 16:30:37 +00:00
/**
* @property {number} preX - The previous x position of the physics body.
* @readonly
*/
2013-10-25 15:54:40 +00:00
this.preX = sprite.x;
2013-10-25 16:30:37 +00:00
/**
* @property {number} preY - The previous y position of the physics body.
* @readonly
*/
2013-10-25 15:54:40 +00:00
this.preY = sprite.y;
2013-10-25 16:30:37 +00:00
/**
* @property {number} preRotation - The previous rotation of the physics body.
* @readonly
*/
2013-10-25 15:54:40 +00:00
this.preRotation = sprite.angle;
2013-10-25 16:30:37 +00:00
/**
* @property {number} screenX - The x position of the physics body translated to screen space.
* @readonly
*/
2013-10-25 15:54:40 +00:00
this.screenX = sprite.x;
2013-10-25 16:30:37 +00:00
/**
* @property {number} screenY - The y position of the physics body translated to screen space.
* @readonly
*/
2013-10-25 15:54:40 +00:00
this.screenY = sprite.y;
2013-10-25 16:30:37 +00:00
/**
* @property {number} sourceWidth - The un-scaled original size.
* @readonly
*/
2013-10-25 15:54:40 +00:00
this.sourceWidth = sprite.currentFrame.sourceSizeW;
2013-10-25 16:30:37 +00:00
/**
* @property {number} sourceHeight - The un-scaled original size.
* @readonly
*/
2013-10-25 15:54:40 +00:00
this.sourceHeight = sprite.currentFrame.sourceSizeH;
2013-10-25 16:30:37 +00:00
/**
* @property {number} width - The calculated width of the physics body.
*/
2013-10-25 15:54:40 +00:00
this.width = sprite.currentFrame.sourceSizeW;
2013-10-25 16:30:37 +00:00
/**
* @property .numInternal ID cache
*/
2013-10-25 15:54:40 +00:00
this.height = sprite.currentFrame.sourceSizeH;
2013-10-25 16:30:37 +00:00
/**
* @property {number} halfWidth - The calculated width / 2 of the physics body.
*/
2013-10-25 15:54:40 +00:00
this.halfWidth = Math.floor(sprite.currentFrame.sourceSizeW / 2);
2013-10-25 16:30:37 +00:00
/**
* @property {number} halfHeight - The calculated height / 2 of the physics body.
*/
2013-10-25 15:54:40 +00:00
this.halfHeight = Math.floor(sprite.currentFrame.sourceSizeH / 2);
2013-10-25 16:30:37 +00:00
/**
* @property {number} _sx - Internal cache var.
* @private
*/
2013-10-25 15:54:40 +00:00
this._sx = sprite.scale.x;
2013-10-25 16:30:37 +00:00
/**
* @property {number} _sy - Internal cache var.
* @private
*/
2013-10-25 15:54:40 +00:00
this._sy = sprite.scale.y;
2013-10-25 16:30:37 +00:00
/**
* @property {Phaser.Point} velocity - The velocity in pixels per second sq. of the Body.
*/
2013-10-25 15:54:40 +00:00
this.velocity = new Phaser.Point;
2013-10-25 16:30:37 +00:00
/**
* @property {Phaser.Point} acceleration - The velocity in pixels per second sq. of the Body.
*/
2013-10-25 15:54:40 +00:00
this.acceleration = new Phaser.Point;
2013-10-25 16:30:37 +00:00
/**
* @property {Phaser.Point} drag - The drag applied to the motion of the Body.
*/
2013-10-25 15:54:40 +00:00
this.drag = new Phaser.Point;
2013-10-25 16:30:37 +00:00
/**
* @property {Phaser.Point} gravity - A private Gravity setting for the Body.
*/
2013-10-25 15:54:40 +00:00
this.gravity = new Phaser.Point;
2013-10-25 16:30:37 +00:00
/**
* @property {Phaser.Point} bounce - The elasticitiy of the Body when colliding. bounce.x/y = 1 means full rebound, bounce.x/y = 0.5 means 50% rebound velocity.
*/
2013-10-25 15:54:40 +00:00
this.bounce = new Phaser.Point;
2013-10-25 16:30:37 +00:00
/**
* @property {Phaser.Point} maxVelocity - The maximum velocity in pixels per second sq. that the Body can reach.
* @default
*/
2013-10-25 15:54:40 +00:00
this.maxVelocity = new Phaser.Point(10000, 10000);
2013-10-25 16:30:37 +00:00
/**
* @property {number} angularVelocity - The angular velocity in pixels per second sq. of the Body.
* @default
*/
2013-10-25 15:54:40 +00:00
this.angularVelocity = 0;
2013-10-25 16:30:37 +00:00
/**
* @property {number} angularAcceleration - The angular acceleration in pixels per second sq. of the Body.
* @default
*/
2013-10-25 15:54:40 +00:00
this.angularAcceleration = 0;
2013-10-25 16:30:37 +00:00
/**
* @property {number} angularDrag - The angular drag applied to the rotation of the Body.
* @default
*/
2013-10-25 15:54:40 +00:00
this.angularDrag = 0;
2013-10-25 16:30:37 +00:00
/**
* @property {number} maxAngular - The maximum angular velocity in pixels per second sq. that the Body can reach.
* @default
*/
2013-10-25 15:54:40 +00:00
this.maxAngular = 1000;
2013-10-25 16:30:37 +00:00
/**
* @property {number} mass - The mass of the Body.
* @default
*/
2013-10-25 15:54:40 +00:00
this.mass = 1;
2013-10-25 16:30:37 +00:00
/**
* @property {boolean} skipQuadTree - If the Body is an irregular shape you can set this to true to avoid it being added to the World quad tree.
* @default
*/
2013-10-25 15:54:40 +00:00
this.skipQuadTree = false;
2013-10-25 16:30:37 +00:00
/**
* @property {Array} quadTreeIDs - Internal ID cache.
* @protected
*/
2013-10-25 15:54:40 +00:00
this.quadTreeIDs = [];
2013-10-25 16:30:37 +00:00
/**
* @property {number} quadTreeIndex - Internal ID cache.
* @protected
*/
2013-10-25 15:54:40 +00:00
this.quadTreeIndex = -1;
// Allow collision
2013-10-25 16:30:37 +00:00
/**
* Set the allowCollision properties to control which directions collision is processed for this Body.
* For example allowCollision.up = false means it won't collide when the collision happened while moving up.
* @property {object} allowCollision - An object containing allowed collision.
*/
2013-10-25 15:54:40 +00:00
this.allowCollision = { none: false, any: true, up: true, down: true, left: true, right: true };
2013-10-25 16:30:37 +00:00
/**
* This object is populated with boolean values when the Body collides with another.
* touching.up = true means the collision happened to the top of this Body for example.
* @property {object} touching - An object containing touching results.
*/
2013-10-25 15:54:40 +00:00
this.touching = { none: true, up: false, down: false, left: false, right: false };
2013-10-25 16:30:37 +00:00
/**
* This object is populated with previous touching values from the bodies previous collision.
* @property {object} wasTouching - An object containing previous touching results.
*/
2013-10-25 15:54:40 +00:00
this.wasTouching = { none: true, up: false, down: false, left: false, right: false };
2013-10-25 16:30:37 +00:00
/**
* @property {number} facing - A const reference to the direction the Body is traveling or facing.
* @default
*/
2013-10-25 15:54:40 +00:00
this.facing = Phaser.NONE;
2013-10-25 16:30:37 +00:00
/**
* @property {boolean} immovable - An immovable Body will not receive any impacts from other bodies.
* @default
*/
2013-10-25 15:54:40 +00:00
this.immovable = false;
2013-10-25 16:30:37 +00:00
/**
* @property {boolean} moves - Set to true to allow the Physics system to move this Body, other false to move it manually.
* @default
*/
2013-10-25 15:54:40 +00:00
this.moves = true;
2013-10-25 16:30:37 +00:00
/**
* @property {number} rotation - The amount the Body is rotated.
* @default
*/
2013-10-25 15:54:40 +00:00
this.rotation = 0;
2013-10-25 16:30:37 +00:00
/**
* @property {boolean} allowRotation - Allow this Body to be rotated? (via angularVelocity, etc)
* @default
*/
2013-10-25 15:54:40 +00:00
this.allowRotation = true;
2013-10-25 16:30:37 +00:00
/**
* @property {boolean} allowGravity - Allow this Body to be influenced by the global Gravity?
* @default
*/
2013-10-25 15:54:40 +00:00
this.allowGravity = true;
2013-10-25 16:30:37 +00:00
/**
* This flag allows you to disable the custom x separation that takes place by Physics.Arcade.separate.
* Used in combination with your own collision processHandler you can create whatever type of collision response you need.
* @property {boolean} customSeparateX - Use a custom separation system or the built-in one?
* @default
*/
2013-10-25 15:54:40 +00:00
this.customSeparateX = false;
2013-10-25 16:30:37 +00:00
/**
* This flag allows you to disable the custom y separation that takes place by Physics.Arcade.separate.
* Used in combination with your own collision processHandler you can create whatever type of collision response you need.
* @property {boolean} customSeparateY - Use a custom separation system or the built-in one?
* @default
*/
2013-10-25 15:54:40 +00:00
this.customSeparateY = false;
2013-10-25 16:30:37 +00:00
/**
* When this body collides with another, the amount of overlap is stored here.
* @property {number} overlapX - The amount of horizontal overlap during the collision.
*/
2013-10-25 15:54:40 +00:00
this.overlapX = 0;
2013-10-25 16:30:37 +00:00
/**
* When this body collides with another, the amount of overlap is stored here.
* @property {number} overlapY - The amount of vertical overlap during the collision.
*/
2013-10-25 15:54:40 +00:00
this.overlapY = 0;
2013-10-25 16:30:37 +00:00
/**
* @property {Phaser.Rectangle} hullX - The dynamically calculated hull used during collision.
*/
2013-10-25 15:54:40 +00:00
this.hullX = new Phaser.Rectangle();
2013-10-25 16:30:37 +00:00
/**
* @property {Phaser.Rectangle} hullY - The dynamically calculated hull used during collision.
*/
2013-10-25 15:54:40 +00:00
this.hullY = new Phaser.Rectangle();
2013-10-25 16:30:37 +00:00
/**
* If a body is overlapping with another body, but neither of them are moving (maybe they spawned on-top of each other?) this is set to true.
* @property {boolean} embedded - Body embed value.
*/
2013-10-25 15:54:40 +00:00
this.embedded = false;
2013-10-25 16:30:37 +00:00
/**
* A Body can be set to collide against the World bounds automatically and rebound back into the World if this is set to true. Otherwise it will leave the World.
* @property {boolean} collideWorldBounds - Should the Body collide with the World bounds?
*/
2013-10-25 15:54:40 +00:00
this.collideWorldBounds = false;
};
Phaser.Physics.Arcade.Body.prototype = {
2013-10-25 16:30:37 +00:00
/**
* Internal method.
*
* @method Phaser.Physics.Arcade#updateBounds
* @protected
*/
2013-10-25 15:54:40 +00:00
updateBounds: function (centerX, centerY, scaleX, scaleY) {
if (scaleX != this._sx || scaleY != this._sy)
{
this.width = this.sourceWidth * scaleX;
this.height = this.sourceHeight * scaleY;
this.halfWidth = Math.floor(this.width / 2);
this.halfHeight = Math.floor(this.height / 2);
this._sx = scaleX;
this._sy = scaleY;
}
},
2013-10-25 16:30:37 +00:00
/**
* Internal method.
*
* @method Phaser.Physics.Arcade#preUpdate
* @protected
*/
2013-10-25 15:54:40 +00:00
preUpdate: function () {
// Store and reset collision flags
this.wasTouching.none = this.touching.none;
this.wasTouching.up = this.touching.up;
this.wasTouching.down = this.touching.down;
this.wasTouching.left = this.touching.left;
this.wasTouching.right = this.touching.right;
this.touching.none = true;
this.touching.up = false;
this.touching.down = false;
this.touching.left = false;
this.touching.right = false;
this.embedded = false;
this.screenX = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
this.screenY = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
this.preX = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
this.preY = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
this.preRotation = this.sprite.angle;
this.x = this.preX;
this.y = this.preY;
this.rotation = this.preRotation;
if (this.moves)
{
this.game.physics.updateMotion(this);
if (this.collideWorldBounds)
{
this.checkWorldBounds();
}
2013-10-25 16:30:37 +00:00
this.updateHulls();Array }
2013-10-25 15:54:40 +00:00
if (this.skipQuadTree == false && this.allowCollision.none == false && this.sprite.visible && this.sprite.alive)
{
this.quadTreeIDs = [];
this.quadTreeIndex = -1;
this.game.physics.quadTree.insert(this);
}
},
2013-10-25 16:30:37 +00:00
/**
* Internal method.
*
* @method Phaser.Physics.Arcade#postUpdate
* @protected
*/
2013-10-25 15:54:40 +00:00
postUpdate: function () {
// Calculate forward-facing edge
if (this.deltaX() == 0 && this.deltaY() == 0)
{
// Can't work it out from the Body, how about from x position?
if (this.sprite.deltaX() == 0 && this.sprite.deltaY() == 0)
{
// still as a statue
}
}
if (this.deltaX() &lt; 0)
{
this.facing = Phaser.LEFT;
}
else if (this.deltaX() > 0)
{
this.facing = Phaser.RIGHT;
}
if (this.deltaY() &lt; 0)
{
this.facing = Phaser.UP;
}
else if (this.deltaY() > 0)
{
this.facing = Phaser.DOWN;
}
this.sprite.x += this.deltaX();
this.sprite.y += this.deltaY();
if (this.allowRotation)
{
this.sprite.angle += this.deltaZ();
}
},
2013-10-25 16:30:37 +00:00
/**
* Internal method.
*
* @method Phaser.Physics.Arcade#updateHulls
* @protected
*/
2013-10-25 15:54:40 +00:00
updateHulls: function () {
this.hullX.setTo(this.x, this.preY, this.width, this.height);
this.hullY.setTo(this.preX, this.y, this.width, this.height);
},
2013-10-25 16:30:37 +00:00
/**
* Internal method.
*
* @method Phaser.Physics.Arcade#checkWorldBounds
* @protected
*/
2013-10-25 15:54:40 +00:00
checkWorldBounds: function () {
if (this.x &lt; this.game.world.bounds.x)
{
this.x = this.game.world.bounds.x;
this.velocity.x *= -this.bounce.x;
}
else if (this.right > this.game.world.bounds.right)
{
this.x = this.game.world.bounds.right - this.width;
this.velocity.x *= -this.bounce.x;
}
if (this.y &lt; this.game.world.bounds.y)
{
this.y = this.game.world.bounds.y;
this.velocity.y *= -this.bounce.y;
}
else if (this.bottom > this.game.world.bounds.bottom)
{
this.y = this.game.world.bounds.bottom - this.height;
this.velocity.y *= -this.bounce.y;
}
},
2013-10-25 16:30:37 +00:00
/**
* You can modify the size of the physics Body to be any dimension you need.
* So it could be smaller or larger than the parent Sprite. You can also control the x and y offset, which
* is the position of the Body relative to the top-left of the Sprite.
*
* @method Phaser.Physics.Arcade#setSize
* @param {number} width - The width of the Body.
* @param {number} height - The height of the Body.
* @param {number} offsetX - The X offset of the Body from the Sprite position.
* @param {number} offsetY - The Y offset of the Body from the Sprite position.
*/
2013-10-25 15:54:40 +00:00
setSize: function (width, height, offsetX, offsetY) {
offsetX = offsetX || this.offset.x;
offsetY = offsetY || this.offset.y;
this.sourceWidth = width;
this.sourceHeight = height;
this.width = this.sourceWidth * this._sx;
this.height = this.sourceHeight * this._sy;
this.halfWidth = Math.floor(this.width / 2);
this.halfHeight = Math.floor(this.height / 2);
this.offset.setTo(offsetX, offsetY);
},
2013-10-25 16:30:37 +00:00
/**
* Resets all Body values (velocity, acceleration, rotation, etc)
*
* @method Phaser.Physics.Arcade#reset
*/
2013-10-25 15:54:40 +00:00
reset: function () {
this.velocity.setTo(0, 0);
this.acceleration.setTo(0, 0);
this.angularVelocity = 0;
this.angularAcceleration = 0;
this.preX = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
this.preY = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
this.preRotation = this.sprite.angle;
this.x = this.preX;
this.y = this.preY;
this.rotation = this.preRotation;
},
2013-10-25 16:30:37 +00:00
/**
* Returns the absolute delta x value.
*
* @method Phaser.Physics.Arcade.Body#deltaAbsX
* @return {number} The absolute delta value.
*/
2013-10-25 15:54:40 +00:00
deltaAbsX: function () {
return (this.deltaX() > 0 ? this.deltaX() : -this.deltaX());
},
2013-10-25 16:30:37 +00:00
/**
* Returns the absolute delta y value.
*
* @method Phaser.Physics.Arcade.Body#deltaAbsY
* @return {number} The absolute delta value.
*/
2013-10-25 15:54:40 +00:00
deltaAbsY: function () {
return (this.deltaY() > 0 ? this.deltaY() : -this.deltaY());
},
2013-10-25 16:30:37 +00:00
/**
* Returns the delta x value.
*
* @method Phaser.Physics.Arcade.Body#deltaX
* @return {number} The delta value.
*/
2013-10-25 15:54:40 +00:00
deltaX: function () {
return this.x - this.preX;
},
2013-10-25 16:30:37 +00:00
/**
* Returns the delta y value.
*
* @method Phaser.Physics.Arcade.Body#deltaY
* @return {number} The delta value.
*/
2013-10-25 15:54:40 +00:00
deltaY: function () {
return this.y - this.preY;
},
deltaZ: function () {
return this.rotation - this.preRotation;
}
};
2013-10-25 16:30:37 +00:00
/**
* @name Phaser.Physics.Arcade.Body#bottom
* @property {number} bottom - The bottom value of this Body (same as Body.y + Body.height)
*/
2013-10-25 15:54:40 +00:00
Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "bottom", {
/**
* The sum of the y and height properties. Changing the bottom property of a Rectangle object has no effect on the x, y and width properties, but does change the height property.
* @method bottom
* @return {number}
**/
get: function () {
return this.y + this.height;
},
/**
* The sum of the y and height properties. Changing the bottom property of a Rectangle object has no effect on the x, y and width properties, but does change the height property.
* @method bottom
* @param {number} value
**/
set: function (value) {
if (value &lt;= this.y)
{
this.height = 0;
}
else
{
this.height = (this.y - value);
}
}
});
2013-10-25 16:30:37 +00:00
/**
* @name Phaser.Physics.Arcade.Body#right
* @property {number} right - The right value of this Body (same as Body.x + Body.width)
*/
2013-10-25 15:54:40 +00:00
Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "right", {
/**
* The sum of the x and width properties. Changing the right property of a Rectangle object has no effect on the x, y and height properties.
* However it does affect the width property.
* @method right
* @return {number}
**/
get: function () {
return this.x + this.width;
},
/**
* The sum of the x and width properties. Changing the right property of a Rectangle object has no effect on the x, y and height properties.
* However it does affect the width property.
* @method right
* @param {number} value
**/
set: function (value) {
if (value &lt;= this.x)
{
this.width = 0;
}
else
{
this.width = this.x + value;
}
}
});
</pre>
</article>
</section>
</div>
<div class="clearfix"></div>
<footer>
<span class="copyright">
Phaser Copyright © 2012-2013 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>
2013-10-25 16:30:37 +00:00
on Fri Oct 25 2013 17:05:24 GMT+0100 (BST) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
2013-10-25 15:54:40 +00:00
</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>