phaser/docs/Body.js.html

1364 lines
37 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>
2013-10-25 15:54:40 +00:00
<!--[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>
2013-11-28 15:57:09 +00:00
<li>
<a href="Phaser.BitmapData.html">BitmapData</a>
</li>
2013-10-25 15:54:40 +00:00
<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>
2013-10-25 15:54:40 +00:00
<li>
<a href="Phaser.Events.html">Events</a>
</li>
2013-11-28 15:57:09 +00:00
<li>
<a href="Phaser.Filter.html">Filter</a>
</li>
2013-10-25 15:54:40 +00:00
<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>
2013-10-25 15:54:40 +00:00
<li>
<a href="Phaser.GameObjectFactory.html">GameObjectFactory</a>
</li>
2014-02-05 06:29:17 +00:00
<li>
<a href="Phaser.Gamepad.html">Gamepad</a>
</li>
<li>
<a href="Phaser.GamepadButton.html">GamepadButton</a>
</li>
2013-10-25 15:54:40 +00:00
<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>
2013-10-25 15:54:40 +00:00
<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>
2014-02-05 06:29:17 +00:00
<li>
<a href="Phaser.Line.html">Line</a>
</li>
2013-10-25 15:54:40 +00:00
<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>
<li>
<a href="Phaser.Physics.Ninja.html">Ninja</a>
</li>
<li>
<a href="Phaser.Physics.Ninja.AABB.html">AABB</a>
</li>
<li>
<a href="Phaser.Physics.Ninja.Body.html">Body</a>
</li>
<li>
<a href="Phaser.Physics.Ninja.Circle.html">Circle</a>
</li>
<li>
<a href="Phaser.Physics.Ninja.Tile.html">Tile</a>
</li>
<li>
<a href="Phaser.Physics.P2.html">P2</a>
</li>
<li>
<a href="Phaser.Physics.P2.Body.html">Body</a>
</li>
<li>
<a href="Phaser.Physics.P2.BodyDebug.html">BodyDebug</a>
</li>
<li>
<a href="Phaser.Physics.P2.CollisionGroup.html">CollisionGroup</a>
</li>
<li>
<a href="Phaser.Physics.P2.ContactMaterial.html">ContactMaterial</a>
</li>
<li>
<a href="Phaser.Physics.P2.DistanceConstraint.html">DistanceConstraint</a>
</li>
<li>
<a href="Phaser.Physics.P2.GearConstraint.html">GearConstraint</a>
</li>
<li>
<a href="Phaser.Physics.P2.InversePointProxy.html">InversePointProxy</a>
</li>
<li>
<a href="Phaser.Physics.P2.LockConstraint.html">LockConstraint</a>
</li>
<li>
<a href="Phaser.Physics.P2.Material.html">Material</a>
</li>
<li>
<a href="Phaser.Physics.P2.PointProxy.html">PointProxy</a>
</li>
<li>
<a href="Phaser.Physics.P2.PrismaticConstraint.html">PrismaticConstraint</a>
</li>
<li>
<a href="Phaser.Physics.P2.RevoluteConstraint.html">RevoluteConstraint</a>
</li>
<li>
<a href="Phaser.Physics.P2.Spring.html">Spring</a>
2013-10-25 16:30:37 +00:00
</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.Polygon.html">Polygon</a>
</li>
<li>
<a href="Phaser.QuadTree.html">QuadTree</a>
</li>
2013-10-25 15:54:40 +00:00
<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.RetroFont.html">RetroFont</a>
</li>
<li>
<a href="Phaser.ScaleManager.html">ScaleManager</a>
</li>
2013-10-25 15:54:40 +00:00
<li>
<a href="Phaser.Signal.html">Signal</a>
</li>
2014-02-05 06:29:17 +00:00
<li>
<a href="Phaser.SinglePad.html">SinglePad</a>
</li>
2013-10-25 15:54:40 +00:00
<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>
2013-10-25 15:54:40 +00:00
<li>
<a href="Phaser.Stage.html">Stage</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>
2013-11-28 15:57:09 +00:00
<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>
2013-10-25 15:54:40 +00:00
<li>
<a href="Phaser.TileSprite.html">TileSprite</a>
</li>
<li>
<a href="Phaser.Time.html">Time</a>
</li>
2013-11-28 15:57:09 +00:00
<li>
<a href="Phaser.Timer.html">Timer</a>
</li>
2014-02-05 06:29:17 +00:00
<li>
<a href="Phaser.TimerEvent.html">TimerEvent</a>
</li>
2013-10-25 15:54:40 +00:00
<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>
</ul>
</div>
</div>
<div class="row-fluid">
<div class="span12">
<div id="main">
<h1 class="page-title">Source: physics/arcade/Body.js</h1>
2013-10-25 15:54:40 +00:00
<section>
<article>
2013-10-25 16:30:37 +00:00
<pre class="sunlight-highlight-javascript linenums">/**
* @author Richard Davey &lt;rich@photonstorm.com>
2014-02-05 06:29:17 +00:00
* @copyright 2014 Photon Storm Ltd.
2013-10-25 16:30:37 +00:00
* @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.
2013-10-25 16:30:37 +00:00
*
* @class Phaser.Physics.Arcade.Body
* @classdesc Arcade Physics Body Constructor
2013-10-25 16:30:37 +00:00
* @constructor
* @param {Phaser.Sprite} sprite - The Sprite object this physics body belongs to.
2013-10-25 16:30:37 +00:00
*/
Phaser.Physics.Arcade.Body = function (sprite) {
/**
* @property {Phaser.Sprite} sprite - Reference to the parent Sprite.
*/
this.sprite = sprite;
2013-10-25 15:54:40 +00:00
2013-10-25 16:30:37 +00:00
/**
* @property {Phaser.Game} game - Local reference to game.
2013-10-25 16:30:37 +00:00
*/
this.game = sprite.game;
2013-10-25 16:30:37 +00:00
/**
* @property {number} type - The type of physics system this body belongs to.
2013-10-25 16:30:37 +00:00
*/
this.type = Phaser.Physics.ARCADE;
2013-10-25 15:54:40 +00:00
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-11-28 15:57:09 +00:00
this.offset = new Phaser.Point();
2013-10-25 15:54:40 +00:00
2013-10-25 16:30:37 +00:00
/**
* @property {Phaser.Point} position - The position of the physics body.
* @readonly
2013-10-25 16:30:37 +00:00
*/
this.position = new Phaser.Point(sprite.x, sprite.y);
2013-10-25 16:30:37 +00:00
/**
* @property {Phaser.Point} prev - The previous position of the physics body.
* @readonly
2013-10-25 16:30:37 +00:00
*/
this.prev = new Phaser.Point(this.position.x, this.position.y);
2013-10-25 16:30:37 +00:00
/**
* @property {boolean} allowRotation - Allow this Body to be rotated? (via angularVelocity, etc)
* @default
2013-10-25 16:30:37 +00:00
*/
this.allowRotation = true;
2013-10-25 16:30:37 +00:00
/**
* @property {number} rotation - The amount the Body is rotated.
2013-10-25 16:30:37 +00:00
*/
this.rotation = sprite.rotation;
2013-10-25 16:30:37 +00:00
/**
* @property {number} preRotation - The previous rotation of the physics body.
* @readonly
2013-10-25 16:30:37 +00:00
*/
this.preRotation = sprite.rotation;
2013-10-25 16:30:37 +00:00
/**
* @property {number} sourceWidth - The un-scaled original size.
* @readonly
2013-10-25 16:30:37 +00:00
*/
this.sourceWidth = sprite.texture.frame.width;
2013-10-25 16:30:37 +00:00
/**
* @property {number} sourceHeight - The un-scaled original size.
* @readonly
2013-10-25 16:30:37 +00:00
*/
this.sourceHeight = sprite.texture.frame.height;
2013-10-25 15:54:40 +00:00
2013-10-25 16:30:37 +00:00
/**
* @property {number} width - The calculated width of the physics body.
2013-10-25 16:30:37 +00:00
*/
this.width = sprite.width;
2013-10-25 16:30:37 +00:00
/**
* @property .numInternal ID cache
*/
this.height = sprite.height;
/**
* @property {number} halfWidth - The calculated width / 2 of the physics body.
*/
this.halfWidth = Math.abs(sprite.width / 2);
/**
* @property {number} halfHeight - The calculated height / 2 of the physics body.
*/
this.halfHeight = Math.abs(sprite.height / 2);
/**
* @property {Phaser.Point} center - The center coordinate of the Physics Body.
2013-10-25 16:30:37 +00:00
*/
this.center = new Phaser.Point(sprite.x + this.halfWidth, sprite.y + this.halfHeight);
2013-10-25 15:54:40 +00:00
/**
* @property {Phaser.Point} velocity - The velocity in pixels per second sq. of the Body.
*/
this.velocity = new Phaser.Point();
2013-10-25 16:30:37 +00:00
/**
* @property {Phaser.Point} newVelocity - New velocity.
* @readonly
*/
this.newVelocity = new Phaser.Point(0, 0);
2013-10-25 15:54:40 +00:00
2014-03-24 12:41:45 +00:00
/**
* @property {Phaser.Point} deltaMax - The Sprite position is updated based on the delta x/y values. You can set a cap on those (both +-) using deltaMax.
*/
this.deltaMax = new Phaser.Point(0, 0);
/**
* @property {Phaser.Point} acceleration - The velocity in pixels per second sq. of the Body.
*/
this.acceleration = new Phaser.Point();
2013-11-01 18:16:52 +00:00
/**
* @property {Phaser.Point} drag - The drag applied to the motion of the Body.
*/
this.drag = new Phaser.Point();
2013-10-25 16:30:37 +00:00
/**
* @property {boolean} allowGravity - Allow this Body to be influenced by gravity? Either world or local.
* @default
2013-10-25 16:30:37 +00:00
*/
this.allowGravity = true;
2013-10-25 15:54:40 +00:00
/**
* @property {Phaser.Point} gravity - A local gravity applied to this Body. If non-zero this over rides any world gravity, unless Body.allowGravity is set to false.
*/
this.gravity = new Phaser.Point(0, 0);
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.
*/
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 16:30:37 +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
*/
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
*/
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 16:30:37 +00:00
*/
this.angularDrag = 0;
2013-10-25 15:54:40 +00:00
/**
* @property {number} maxAngular - The maximum angular velocity in pixels per second sq. that the Body can reach.
* @default
*/
this.maxAngular = 1000;
2013-10-25 16:30:37 +00:00
/**
* @property {number} mass - The mass of the Body.
* @default
*/
this.mass = 1;
/**
* @property {number} angle - The angle of the Body in radians as calculated by its velocity, rather than its visual angle.
* @readonly
*/
this.angle = 0;
/**
* @property {number} speed - The speed of the Body as calculated by its velocity.
* @readonly
*/
this.speed = 0;
/**
* @property {number} facing - A const reference to the direction the Body is traveling or facing.
* @default
*/
this.facing = Phaser.NONE;
/**
* @property {boolean} immovable - An immovable Body will not receive any impacts from other bodies.
* @default
*/
this.immovable = false;
/**
* If you have a Body that is being moved around the world via a tween or a Group motion, but its local x/y position never
* actually changes, then you should set Body.moves = false. Otherwise it will most likely fly off the screen.
* If you want the physics system to move the body around, then set moves to true.
* @property {boolean} moves - Set to true to allow the Physics system to move this Body, other false to move it manually.
* @default
*/
this.moves = true;
/**
* 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
*/
this.customSeparateX = false;
/**
* 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
*/
this.customSeparateY = false;
/**
* When this body collides with another, the amount of overlap is stored here.
* @property {number} overlapX - The amount of horizontal overlap during the collision.
*/
this.overlapX = 0;
/**
* When this body collides with another, the amount of overlap is stored here.
* @property {number} overlapY - The amount of vertical overlap during the collision.
*/
this.overlapY = 0;
/**
* 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.
*/
this.embedded = false;
/**
* 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?
*/
this.collideWorldBounds = false;
/**
* Set the checkCollision properties to control which directions collision is processed for this Body.
* For example checkCollision.up = false means it won't collide when the collision happened while moving up.
* @property {object} checkCollision - An object containing allowed collision.
*/
this.checkCollision = { none: false, any: true, up: true, down: true, left: true, right: true };
/**
* 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.
*/
this.touching = { none: true, up: false, down: false, left: false, right: false };
/**
* This object is populated with previous touching values from the bodies previous collision.
* @property {object} wasTouching - An object containing previous touching results.
*/
this.wasTouching = { none: true, up: false, down: false, left: false, right: false };
/**
* This object is populated with boolean values when the Body collides with the World bounds or a Tile.
* For example if blocked.up is true then the Body cannot move up.
* @property {object} blocked - An object containing on which faces this Body is blocked from moving, if any.
*/
this.blocked = { up: false, down: false, left: false, right: false };
/**
* If this is an especially small or fast moving object then it can sometimes skip over tilemap collisions if it moves through a tile in a step.
* Set this padding value to add extra padding to its bounds. tilePadding.x applied to its width, y to its height.
* @property {Phaser.Point} tilePadding - Extra padding to be added to this sprites dimensions when checking for tile collision.
*/
this.tilePadding = new Phaser.Point();
/**
* @property {number} phaser - Is this Body in a preUpdate or postUpdate state?
*/
this.phase = 0;
/**
* @property {boolean} _reset - Internal cache var.
* @private
*/
this._reset = true;
2014-03-24 12:41:45 +00:00
/**
* @property {number} _sx - Internal cache var.
* @private
*/
this._sx = sprite.scale.x;
/**
* @property {number} _sy - Internal cache var.
* @private
*/
this._sy = sprite.scale.y;
/**
* @property {number} _dx - Internal cache var.
* @private
*/
this._dx = 0;
/**
* @property {number} _dy - Internal cache var.
* @private
*/
this._dy = 0;
};
Phaser.Physics.Arcade.Body.prototype = {
/**
* Internal method.
*
* @method Phaser.Physics.Arcade#updateBounds
* @protected
*/
2014-03-24 12:41:45 +00:00
updateBounds: function () {
2013-10-25 16:30:37 +00:00
2014-03-24 12:41:45 +00:00
var asx = Math.abs(this.sprite.scale.x);
var asy = Math.abs(this.sprite.scale.y);
if (asx !== this._sx || asy !== this._sy)
{
2014-03-24 12:41:45 +00:00
this.width = this.sourceWidth * asx;
this.height = this.sourceHeight * asy;
this.halfWidth = Math.floor(this.width / 2);
this.halfHeight = Math.floor(this.height / 2);
2014-03-24 12:41:45 +00:00
this._sx = asx;
this._sy = asy;
this.center.setTo(this.position.x + this.halfWidth, this.position.y + this.halfHeight);
this._reset = true;
}
2013-10-25 16:30:37 +00:00
},
2013-10-25 15:54:40 +00:00
2013-10-25 16:30:37 +00:00
/**
* Internal method.
*
* @method Phaser.Physics.Arcade#preUpdate
* @protected
2013-10-25 16:30:37 +00:00
*/
preUpdate: function () {
this.phase = 1;
// 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.blocked.up = false;
this.blocked.down = false;
this.blocked.left = false;
this.blocked.right = false;
2013-10-25 16:30:37 +00:00
2014-03-24 12:41:45 +00:00
this.embedded = false;
this.updateBounds();
2014-03-24 12:41:45 +00:00
this.position.x = (this.sprite.world.x - (this.sprite.anchor.x * this.width)) + this.offset.x;
this.position.y = (this.sprite.world.y - (this.sprite.anchor.y * this.height)) + this.offset.y;
this.rotation = this.sprite.angle;
this.preRotation = this.rotation;
if (this._reset || this.sprite._cache[4] === 1)
2014-03-24 12:41:45 +00:00
{
this.prev.x = this.position.x;
this.prev.y = this.position.y;
}
if (this.moves)
{
this.game.physics.arcade.updateMotion(this);
this.newVelocity.set(this.velocity.x * this.game.time.physicsElapsed, this.velocity.y * this.game.time.physicsElapsed);
this.position.x += this.newVelocity.x;
this.position.y += this.newVelocity.y;
if (this.position.x !== this.prev.x || this.position.y !== this.prev.y)
{
this.speed = Math.sqrt(this.velocity.x * this.velocity.x + this.velocity.y * this.velocity.y);
this.angle = Math.atan2(this.velocity.y, this.velocity.x);
}
// Now the State update will throw collision checks at the Body
// And finally we'll integrate the new position back to the Sprite in postUpdate
if (this.collideWorldBounds)
{
this.checkWorldBounds();
}
}
2013-10-25 15:54:40 +00:00
this._dx = this.deltaX();
this._dy = this.deltaY();
this._reset = false;
},
2013-10-25 16:30:37 +00:00
/**
* Internal method.
*
* @method Phaser.Physics.Arcade#postUpdate
* @protected
2013-10-25 16:30:37 +00:00
*/
postUpdate: function () {
2013-10-25 16:30:37 +00:00
this.phase = 2;
if (this.deltaX() &lt; 0)
{
this.facing = Phaser.LEFT;
}
else if (this.deltaX() > 0)
{
this.facing = Phaser.RIGHT;
}
2013-10-25 16:30:37 +00:00
if (this.deltaY() &lt; 0)
{
this.facing = Phaser.UP;
}
else if (this.deltaY() > 0)
{
this.facing = Phaser.DOWN;
}
2013-10-25 15:54:40 +00:00
if (this.moves)
{
2014-03-24 12:41:45 +00:00
this._dx = this.deltaX();
this._dy = this.deltaY();
if (this.deltaMax.x !== 0 && this._dx !== 0)
{
if (this._dx &lt; 0 && this._dx &lt; -this.deltaMax.x)
{
this._dx = -this.deltaMax.x;
}
else if (this._dx > 0 && this._dx > this.deltaMax.x)
{
this._dx = this.deltaMax.x;
}
}
if (this.deltaMax.y !== 0 && this._dy !== 0)
{
if (this._dy &lt; 0 && this._dy &lt; -this.deltaMax.y)
2014-03-24 12:41:45 +00:00
{
this._dy = -this.deltaMax.y;
}
else if (this._dy > 0 && this._dy > this.deltaMax.y)
{
this._dy = this.deltaMax.y;
}
}
this.sprite.x += this._dx;
this.sprite.y += this._dy;
}
2014-03-24 12:41:45 +00:00
this.center.setTo(this.position.x + this.halfWidth, this.position.y + this.halfHeight);
if (this.allowRotation)
{
this.sprite.angle += this.deltaZ();
}
2013-10-25 16:30:37 +00:00
2014-03-24 12:41:45 +00:00
this.prev.x = this.position.x;
this.prev.y = this.position.y;
},
/**
* Removes this bodies reference to its parent sprite, freeing it up for gc.
*
* @method Phaser.Physics.Arcade#destroy
*/
destroy: function () {
this.sprite = null;
},
2013-10-25 16:30:37 +00:00
/**
* Internal method.
*
* @method Phaser.Physics.Arcade#checkWorldBounds
* @protected
2013-10-25 16:30:37 +00:00
*/
checkWorldBounds: function () {
2013-10-25 16:30:37 +00:00
2014-03-24 12:41:45 +00:00
if (this.position.x &lt; this.game.physics.arcade.bounds.x && this.game.physics.arcade.checkCollision.left)
{
2014-03-24 12:41:45 +00:00
this.position.x = this.game.physics.arcade.bounds.x;
this.velocity.x *= -this.bounce.x;
this.blocked.left = true;
}
2014-03-24 12:41:45 +00:00
else if (this.right > this.game.physics.arcade.bounds.right && this.game.physics.arcade.checkCollision.right)
{
2014-03-24 12:41:45 +00:00
this.position.x = this.game.physics.arcade.bounds.right - this.width;
this.velocity.x *= -this.bounce.x;
this.blocked.right = true;
}
2013-10-25 16:30:37 +00:00
2014-03-24 12:41:45 +00:00
if (this.position.y &lt; this.game.physics.arcade.bounds.y && this.game.physics.arcade.checkCollision.up)
{
2014-03-24 12:41:45 +00:00
this.position.y = this.game.physics.arcade.bounds.y;
this.velocity.y *= -this.bounce.y;
this.blocked.up = true;
}
2014-03-24 12:41:45 +00:00
else if (this.bottom > this.game.physics.arcade.bounds.bottom && this.game.physics.arcade.checkCollision.down)
{
2014-03-24 12:41:45 +00:00
this.position.y = this.game.physics.arcade.bounds.bottom - this.height;
this.velocity.y *= -this.bounce.y;
this.blocked.down = true;
}
2013-10-25 16:30:37 +00:00
},
2013-10-25 15:54:40 +00:00
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 16:30:37 +00:00
*/
setSize: function (width, height, offsetX, offsetY) {
2013-10-25 16:30:37 +00:00
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);
2014-03-24 12:41:45 +00:00
this.center.setTo(this.position.x + this.halfWidth, this.position.y + this.halfHeight);
2013-10-25 15:54:40 +00:00
},
2013-10-25 15:54:40 +00:00
2013-10-25 16:30:37 +00:00
/**
* Resets all Body values (velocity, acceleration, rotation, etc)
*
* @method Phaser.Physics.Arcade#reset
* @param {number} x - The new x position of the Body.
* @param {number} y - The new x position of the Body.
2013-10-25 16:30:37 +00:00
*/
reset: function (x, y) {
2014-02-05 06:29:17 +00:00
2014-03-24 12:41:45 +00:00
this.velocity.set(0);
this.acceleration.set(0);
this.angularVelocity = 0;
this.angularAcceleration = 0;
2014-03-24 12:41:45 +00:00
this.position.x = (x - (this.sprite.anchor.x * this.width)) + this.offset.x;
this.position.y = (y - (this.sprite.anchor.y * this.height)) + this.offset.y;
this.prev.x = this.position.x;
this.prev.y = this.position.y;
this.rotation = this.sprite.angle;
this.preRotation = this.rotation;
2014-03-24 12:41:45 +00:00
this._sx = this.sprite.scale.x;
this._sy = this.sprite.scale.y;
2014-03-24 12:41:45 +00:00
this.center.setTo(this.position.x + this.halfWidth, this.position.y + this.halfHeight);
2014-02-05 06:29:17 +00:00
},
2014-02-05 06:29:17 +00:00
/**
* Returns true if the bottom of this Body is in contact with either the world bounds or a tile.
*
* @method Phaser.Physics.Arcade.Body#onFloor
* @return {boolean} True if in contact with either the world bounds or a tile.
2014-02-05 06:29:17 +00:00
*/
onFloor: function () {
return this.blocked.down;
},
2014-02-05 06:29:17 +00:00
/**
* Returns true if either side of this Body is in contact with either the world bounds or a tile.
*
* @method Phaser.Physics.Arcade.Body#onWall
* @return {boolean} True if in contact with either the world bounds or a tile.
2014-02-05 06:29:17 +00:00
*/
onWall: function () {
return (this.blocked.left || this.blocked.right);
},
2013-10-25 15:54:40 +00:00
/**
* Returns the absolute delta x value.
*
* @method Phaser.Physics.Arcade.Body#deltaAbsX
* @return {number} The absolute delta value.
*/
deltaAbsX: function () {
return (this.deltaX() > 0 ? this.deltaX() : -this.deltaX());
},
2013-10-25 15:54:40 +00:00
2013-10-25 16:30:37 +00:00
/**
* Returns the absolute delta y value.
2013-10-25 16:30:37 +00:00
*
* @method Phaser.Physics.Arcade.Body#deltaAbsY
* @return {number} The absolute delta value.
2013-10-25 16:30:37 +00:00
*/
deltaAbsY: function () {
return (this.deltaY() > 0 ? this.deltaY() : -this.deltaY());
2013-11-28 15:57:09 +00:00
},
2013-10-25 15:54:40 +00:00
2013-10-25 16:30:37 +00:00
/**
* Returns the delta x value. The difference between Body.x now and in the previous step.
2013-10-25 16:30:37 +00:00
*
* @method Phaser.Physics.Arcade.Body#deltaX
* @return {number} The delta value. Positive if the motion was to the right, negative if to the left.
2013-10-25 16:30:37 +00:00
*/
deltaX: function () {
return this.position.x - this.prev.x;
},
/**
* Returns the delta y value. The difference between Body.y now and in the previous step.
*
* @method Phaser.Physics.Arcade.Body#deltaY
* @return {number} The delta value. Positive if the motion was downwards, negative if upwards.
*/
deltaY: function () {
return this.position.y - this.prev.y;
},
2014-02-05 06:29:17 +00:00
/**
* Returns the delta z value. The difference between Body.rotation now and in the previous step.
*
* @method Phaser.Physics.Arcade.Body#deltaZ
* @return {number} The delta value. Positive if the motion was clockwise, negative if anti-clockwise.
*/
deltaZ: function () {
return this.rotation - this.preRotation;
}
2013-10-25 15:54:40 +00:00
};
2014-02-05 06:29:17 +00:00
/**
* @name Phaser.Physics.Arcade.Body#bottom
* @property {number} bottom - The bottom value of this Body (same as Body.y + Body.height)
* @readonly
*/
Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "bottom", {
get: function () {
return this.position.y + this.height;
}
});
/**
* @name Phaser.Physics.Arcade.Body#right
* @property {number} right - The right value of this Body (same as Body.x + Body.width)
* @readonly
*/
Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "right", {
get: function () {
return this.position.x + this.width;
}
});
/**
* @name Phaser.Physics.Arcade.Body#x
* @property {number} x - The x position.
*/
Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "x", {
get: function () {
return this.position.x;
},
set: function (value) {
this.position.x = value;
}
});
2014-02-05 06:29:17 +00:00
/**
* @name Phaser.Physics.Arcade.Body#y
* @property {number} y - The y position.
*/
Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "y", {
get: function () {
return this.position.y;
2014-02-05 06:29:17 +00:00
},
set: function (value) {
this.position.y = value;
2013-10-25 15:54:40 +00:00
}
});
/**
* Render Sprite Body.
*
* @method Phaser.Physics.Arcade.Body#renderDebug
* @param {object} context - The context to render to.
* @param {Phaser.Physics.Arcade.Body} body - The Body to render the info of.
* @param {string} [color='rgb(255,255,255)'] - color of the debug info to be rendered. (format is css color string).
* @param {boolean} [filled=true] - Render the objected as a filled (default, true) or a stroked (false)
*/
Phaser.Physics.Arcade.Body.render = function (context, body, filled, color) {
if (typeof filled === 'undefined') { filled = true; }
color = color || 'rgba(0,255,0,0.4)';
if (filled)
{
context.fillStyle = color;
context.fillRect(body.position.x - body.game.camera.x, body.position.y - body.game.camera.y, body.width, body.height);
}
else
{
context.strokeStyle = color;
context.strokeRect(body.position.x - body.game.camera.x, body.position.y - body.game.camera.y, body.width, body.height);
2013-10-25 15:54:40 +00:00
}
};
2013-10-25 15:54:40 +00:00
2013-10-25 16:30:37 +00:00
/**
* Render Sprite Body Physics Data as text.
*
* @method Phaser.Physics.Arcade.Body#renderBodyInfo
* @param {Phaser.Physics.Arcade.Body} body - The Body to render the info of.
* @param {number} x - X position of the debug info to be rendered.
* @param {number} y - Y position of the debug info to be rendered.
* @param {string} [color='rgb(255,255,255)'] - color of the debug info to be rendered. (format is css color string).
2013-10-25 16:30:37 +00:00
*/
Phaser.Physics.Arcade.Body.renderBodyInfo = function (debug, body) {
2013-10-25 15:54:40 +00:00
debug.line('x: ' + body.x.toFixed(2), 'y: ' + body.y.toFixed(2), 'width: ' + body.width, 'height: ' + body.height);
debug.line('velocity x: ' + body.velocity.x.toFixed(2), 'y: ' + body.velocity.y.toFixed(2), 'deltaX: ' + body._dx.toFixed(2), 'deltaY: ' + body._dy.toFixed(2));
debug.line('acceleration x: ' + body.acceleration.x.toFixed(2), 'y: ' + body.acceleration.y.toFixed(2), 'speed: ' + body.speed.toFixed(2), 'angle: ' + body.angle.toFixed(2));
debug.line('gravity x: ' + body.gravity.x, 'y: ' + body.gravity.y, 'bounce x: ' + body.bounce.x.toFixed(2), 'y: ' + body.bounce.y.toFixed(2));
debug.line('touching left: ' + body.touching.left, 'right: ' + body.touching.right, 'up: ' + body.touching.up, 'down: ' + body.touching.down);
debug.line('blocked left: ' + body.blocked.left, 'right: ' + body.blocked.right, 'up: ' + body.blocked.up, 'down: ' + body.blocked.down);
2013-10-25 15:54:40 +00:00
};
2013-10-25 15:54:40 +00:00
Phaser.Physics.Arcade.Body.prototype.constructor = Phaser.Physics.Arcade.Body;
2013-10-25 15:54:40 +00:00
</pre>
</article>
</section>
</div>
<div class="clearfix"></div>
<footer>
<span class="copyright">
2014-02-05 06:29:17 +00:00
Phaser Copyright © 2012-2014 Photon Storm Ltd.
2013-10-25 15:54:40 +00:00
</span>
<br />
<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-dev</a>
on Fri Mar 28 2014 01:41:23 GMT-0000 (GMT) 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( {
anchorName : function(i, heading, prefix) {
return $(heading).attr("id") || ( prefix + i );
},
2013-10-25 15:54:40 +00:00
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>