Lots of new examples and updates.

This commit is contained in:
photonstorm 2013-10-09 04:31:08 +01:00
parent 1c577d3674
commit 83a00862be
42 changed files with 829 additions and 280 deletions

View file

@ -102,12 +102,15 @@ Version 1.0.7 (in progress in the dev branch)
* Group.create now sets the visible and alive properties of the Sprite to the same value as the 'exists' parameter.
* Added Group.total. Same as Group.length, but more in line with the rest of the Group naming.
* Added Sprite.outOfBoundsKill boolean flag. Will automatically kill a sprite that leaves the game World bounds (off by default).
* ArcadePhysics.moveTowardsMouse changed to ArcadePhysics.moveTowardsPointer and now lets you specify which pointer to move towards (defaults to Input.activePointer).
* ArcadePhysics.angleBetweenMouse changed to ArcadePhysics.angleBetweenPointer and now lets you specify which pointer to get the angle to (defaults to Input.activePointer).
* ArcadePhysics.velocityFromAngle and ArcadePhysics.velocityFromRotation added with examples created.
* Lots of changes and fixes in ArcadePhysics, including:
* Functions with "mouse" in the title have been updated to "pointer" to more accurately reflect what they do.
* New velocity functions: moveToObject, moveToPointer, moveToXY
* New acceleration functions: accelerateToObject, accelerateToPointer, accelerateToXY
* New distance functions: distanceBetween, distanceToXY, distanceToPointer
* New angle functions: angleBetween, angleToXY, angleToPointer
* velocityFromAngle and velocityFromRotation added with examples created.
* Fixed the RandomDataGenerator.sow method so if you give in the same seed you'll now get the same results (thanks Hsaka)
* TODO: look at Sprite.crop (http://www.html5gamedevs.com/topic/1617-error-in-spritecrop/)
* TODO: d-pad example (http://www.html5gamedevs.com/topic/1574-gameinputondown-question/)
* TODO: more touch input examples (http://www.html5gamedevs.com/topic/1556-mobile-touch-event/)

Binary file not shown.

After

Width:  |  Height:  |  Size: 233 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 232 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 250 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 245 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 296 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 299 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 248 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 251 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 270 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 678 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

View file

@ -0,0 +1,84 @@
{"frames": [
{
"filename": "shadow",
"frame": {"x":0,"y":0,"w":80,"h":80},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":80,"h":80},
"sourceSize": {"w":80,"h":80}
},
{
"filename": "tank1",
"frame": {"x":190,"y":52,"w":54,"h":52},
"rotated": false,
"trimmed": true,
"spriteSourceSize": {"x":3,"y":5,"w":54,"h":52},
"sourceSize": {"w":64,"h":64}
},
{
"filename": "tank2",
"frame": {"x":136,"y":52,"w":54,"h":52},
"rotated": false,
"trimmed": true,
"spriteSourceSize": {"x":3,"y":5,"w":54,"h":52},
"sourceSize": {"w":64,"h":64}
},
{
"filename": "tank3",
"frame": {"x":80,"y":54,"w":56,"h":54},
"rotated": false,
"trimmed": true,
"spriteSourceSize": {"x":2,"y":4,"w":56,"h":54},
"sourceSize": {"w":64,"h":64}
},
{
"filename": "tank4",
"frame": {"x":190,"y":0,"w":54,"h":52},
"rotated": false,
"trimmed": true,
"spriteSourceSize": {"x":3,"y":5,"w":54,"h":52},
"sourceSize": {"w":64,"h":64}
},
{
"filename": "tank5",
"frame": {"x":136,"y":0,"w":54,"h":52},
"rotated": false,
"trimmed": true,
"spriteSourceSize": {"x":3,"y":5,"w":54,"h":52},
"sourceSize": {"w":64,"h":64}
},
{
"filename": "tank6",
"frame": {"x":80,"y":0,"w":56,"h":54},
"rotated": false,
"trimmed": true,
"spriteSourceSize": {"x":2,"y":4,"w":56,"h":54},
"sourceSize": {"w":64,"h":64}
},
{
"filename": "tracks",
"frame": {"x":48,"y":80,"w":24,"h":12},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":24,"h":12},
"sourceSize": {"w":24,"h":12}
},
{
"filename": "turret",
"frame": {"x":0,"y":80,"w":48,"h":28},
"rotated": false,
"trimmed": true,
"spriteSourceSize": {"x":1,"y":2,"w":48,"h":28},
"sourceSize": {"w":50,"h":32}
}],
"meta": {
"app": "http://www.codeandweb.com/texturepacker ",
"version": "1.0",
"image": "tanks.png",
"format": "RGBA8888",
"size": {"w":244,"h":108},
"scale": "1",
"smartupdate": "$TexturePacker:SmartUpdate:99b2f74fa157c5b63e72b0d68be078b4:1/1$"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

View file

@ -0,0 +1,172 @@
<?xml version="1.0" encoding="UTF-8"?>
<data version="1.0">
<struct type="Settings">
<key>fileFormatVersion</key>
<int>1</int>
<key>variation</key>
<string>main</string>
<key>verbose</key>
<false/>
<key>autoSDSettings</key>
<array/>
<key>allowRotation</key>
<false/>
<key>quiet</key>
<false/>
<key>premultiplyAlpha</key>
<false/>
<key>shapeDebug</key>
<false/>
<key>dpi</key>
<uint>72</uint>
<key>dataFormat</key>
<string>json-array</string>
<key>textureFileName</key>
<filename>tanks.png</filename>
<key>flipPVR</key>
<false/>
<key>ditherType</key>
<enum type="SettingsBase::DitherType">NearestNeighbour</enum>
<key>backgroundColor</key>
<uint>0</uint>
<key>libGdx</key>
<struct type="LibGDX">
<key>filtering</key>
<struct type="LibGDXFiltering">
<key>x</key>
<enum type="LibGDXFiltering::Filtering">Linear</enum>
<key>y</key>
<enum type="LibGDXFiltering::Filtering">Linear</enum>
</struct>
</struct>
<key>shapePadding</key>
<uint>0</uint>
<key>jpgQuality</key>
<uint>80</uint>
<key>pngOptimizationLevel</key>
<uint>0</uint>
<key>textureSubPath</key>
<string></string>
<key>textureFormat</key>
<enum type="SettingsBase::TextureFormat">png</enum>
<key>borderPadding</key>
<uint>0</uint>
<key>maxTextureSize</key>
<QSize>
<key>width</key>
<int>512</int>
<key>height</key>
<int>512</int>
</QSize>
<key>fixedTextureSize</key>
<QSize>
<key>width</key>
<int>-1</int>
<key>height</key>
<int>-1</int>
</QSize>
<key>reduceBorderArtifacts</key>
<false/>
<key>algorithmSettings</key>
<struct type="AlgorithmSettings">
<key>algorithm</key>
<enum type="AlgorithmSettings::AlgorithmId">MaxRects</enum>
<key>freeSizeMode</key>
<enum type="AlgorithmSettings::AlgorithmFreeSizeMode">Best</enum>
<key>sizeConstraints</key>
<enum type="AlgorithmSettings::SizeConstraints">AnySize</enum>
<key>forceSquared</key>
<false/>
<key>forceWordAligned</key>
<false/>
<key>maxRects</key>
<struct type="AlgorithmMaxRectsSettings">
<key>heuristic</key>
<enum type="AlgorithmMaxRectsSettings::Heuristic">Best</enum>
</struct>
<key>basic</key>
<struct type="AlgorithmBasicSettings">
<key>sortBy</key>
<enum type="AlgorithmBasicSettings::SortBy">Name</enum>
<key>order</key>
<enum type="AlgorithmBasicSettings::Order">Ascending</enum>
</struct>
</struct>
<key>andEngine</key>
<struct type="AndEngine">
<key>minFilter</key>
<enum type="AndEngine::MinFilter">Linear</enum>
<key>packageName</key>
<string>Texture</string>
<key>javaFileName</key>
<filename>tanks.java</filename>
<key>wrap</key>
<struct type="AndEngineWrap">
<key>s</key>
<enum type="AndEngineWrap::Wrap">Clamp</enum>
<key>t</key>
<enum type="AndEngineWrap::Wrap">Clamp</enum>
</struct>
<key>magFilter</key>
<enum type="AndEngine::MagFilter">MagLinear</enum>
</struct>
<key>dataFileName</key>
<filename>tanks.json</filename>
<key>multiPack</key>
<false/>
<key>mainExtension</key>
<string></string>
<key>forceIdenticalLayout</key>
<false/>
<key>outputFormat</key>
<enum type="SettingsBase::OutputFormat">RGBA8888</enum>
<key>contentProtection</key>
<struct type="ContentProtection">
<key>key</key>
<string></string>
</struct>
<key>autoAliasEnabled</key>
<true/>
<key>trimSpriteNames</key>
<true/>
<key>globalSpriteSettings</key>
<struct type="SpriteSettings">
<key>scale</key>
<double>1</double>
<key>scaleMode</key>
<enum type="ScaleMode">Smooth</enum>
<key>innerPadding</key>
<uint>0</uint>
<key>extrude</key>
<uint>0</uint>
<key>trimThreshold</key>
<uint>1</uint>
<key>trimMode</key>
<enum type="SpriteSettings::TrimMode">Trim</enum>
<key>heuristicMask</key>
<false/>
</struct>
<key>fileList</key>
<array>
<filename>shadow.png</filename>
<filename>tank1.png</filename>
<filename>tank2.png</filename>
<filename>tank3.png</filename>
<filename>tank4.png</filename>
<filename>tank5.png</filename>
<filename>tank6.png</filename>
<filename>tracks.png</filename>
<filename>turret.png</filename>
</array>
<key>ignoreFileList</key>
<array/>
<key>replaceList</key>
<array/>
<key>ignoredWarnings</key>
<array/>
<key>commonDivisorX</key>
<uint>1</uint>
<key>commonDivisorY</key>
<uint>1</uint>
</struct>
</data>

Binary file not shown.

After

Width:  |  Height:  |  Size: 468 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 573 B

170
examples/games/tanks.php Normal file
View file

@ -0,0 +1,170 @@
<?php
$title = "Tanks";
require('../head.php');
?>
<script type="text/javascript">
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.atlas('tank', 'assets/games/tanks/tanks.png', 'assets/games/tanks/tanks.json');
game.load.image('bullet', 'assets/sprites/enemy-bullet.png');
game.load.image('earth', 'assets/games/tanks/scorched_earth.png');
}
var tank;
var turret;
var shadow;
var currentSpeed = 0;
var land;
var cursors;
var bullets;
var fireRate = 100;
var nextFire = 0;
function create() {
// Resize our game world to be a 2000x2000 square
// game.world.setBounds(-1000, -1000, 2000, 2000);
game.world.setBounds(0, 0, 1000, 1000);
console.log(game.world.bounds.right, 'bot', this.game.world.bounds.bottom);
console.log(game.camera.bounds.right, 'cbot', this.game.camera.bounds.bottom);
// Our tiled scrolling background
land = game.add.tileSprite(0, 0, 800, 600, 'earth');
land.fixedToCamera = true;
// A shadow below our tank
shadow = game.add.sprite(0, 0, 'tank', 'shadow');
shadow.anchor.setTo(0.5, 0.5);
// The base of our tank
tank = game.add.sprite(0, 0, 'tank', 'tank1');
tank.anchor.setTo(0.5, 0.5);
tank.animations.add('move', ['tank1', 'tank2', 'tank3', 'tank4', 'tank5', 'tank6'], 20, true);
tank.play('move');
// This will force it to decelerate and limit its speed
tank.body.drag.setTo(200, 200);
tank.body.maxVelocity.setTo(400, 400);
tank.body.collideWorldBounds = true;
// Our bullet group
bullets = game.add.group();
bullets.createMultiple(50, 'bullet');
bullets.setAll('anchor.x', 0.5);
bullets.setAll('anchor.y', 0.5);
bullets.setAll('outOfBoundsKill', true);
// Finally the turret that we place on-top of the tank body
turret = game.add.sprite(0, 0, 'tank', 'turret');
turret.anchor.setTo(0.5, 0.5);
game.camera.follow(tank);
// game.camera.deadzone = new Phaser.Rectangle(100, 100, 600, 400);
cursors = game.input.keyboard.createCursorKeys();
}
function update() {
if (cursors.left.isDown)
{
tank.angle -= 4;
}
else if (cursors.right.isDown)
{
tank.angle += 4;
}
if (cursors.up.isDown)
{
// The speed we'll travel at
currentSpeed = 300;
}
else
{
if (currentSpeed > 0)
{
currentSpeed -= 4;
}
}
if (currentSpeed > 0)
{
game.physics.velocityFromRotation(tank.rotation, currentSpeed, tank.body.velocity);
// Scroll the background (note the negative offset to ensure it moves the direction we're facing, not coming from)
// land.tilePosition.x -= (tank.body.velocity.x / 50);
// land.tilePosition.y -= (tank.body.velocity.y / 50);
}
land.tilePosition.x = -game.camera.x;
land.tilePosition.y = -game.camera.y;
// Position all the parts and align rotations
shadow.x = tank.x;
shadow.y = tank.y;
shadow.rotation = tank.rotation;
turret.x = tank.x;
turret.y = tank.y;
turret.rotation = game.physics.angleToPointer(turret);
if (game.input.activePointer.isDown)
{
// Boom!
fire();
}
}
function fire() {
if (game.time.now > nextFire && bullets.countDead() > 0)
{
nextFire = game.time.now + fireRate;
var bullet = bullets.getFirstDead();
bullet.reset(turret.x, turret.y);
game.physics.moveToPointer(bullet, 1000);
}
}
function render() {
// game.debug.renderText('Active Bullets: ' + bullets.countLiving() + ' / ' + bullets.total, 32, 32);
// game.debug.renderText('sr: ' + tank.body.right, 32, 100);
// game.debug.renderText('sb: ' + tank.body.bottom, 32, 132);
game.debug.renderSpriteCorners(tank, true, true);
game.debug.renderCameraInfo(game.camera, 500, 32);
game.debug.renderLocalTransformInfo(tank, 32, 32);
game.debug.renderWorldTransformInfo(tank, 32, 200);
// game.debug.renderSpriteInfo(sprite, 32, 450);
}
</script>
<?php
require('../foot.php');
?>

View file

@ -0,0 +1,41 @@
<?php
$title = "Accelerate to the Active Pointer";
require('../head.php');
?>
<script type="text/javascript">
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('arrow', 'assets/sprites/arrow.png');
}
var sprite;
function create() {
game.stage.backgroundColor = '#0072bc';
sprite = game.add.sprite(400, 300, 'arrow');
sprite.anchor.setTo(0.5, 0.5);
}
function update() {
sprite.rotation = game.physics.accelerateToPointer(sprite, this.game.input.activePointer, 500, 500, 500);
}
function render() {
game.debug.renderSpriteInfo(sprite, 32, 32);
}
</script>
<?php
require('../foot.php');
?>

View file

@ -0,0 +1,51 @@
<?php
$title = "Angle between two Sprites";
require('../head.php');
?>
<script type="text/javascript">
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('arrow', 'assets/sprites/longarrow.png');
game.load.image('ball', 'assets/sprites/pangball.png');
}
var arrow;
var target;
function create() {
game.stage.backgroundColor = '#0072bc';
arrow = game.add.sprite(200, 250, 'arrow');
arrow.anchor.setTo(0.1, 0.5);
target = game.add.sprite(600, 400, 'ball');
target.anchor.setTo(0.5, 0.5);
target.inputEnabled = true;
target.input.enableDrag(true);
}
function update() {
arrow.rotation = game.physics.angleBetween(arrow, target);
}
function render() {
game.debug.renderText("Drag the ball", 32, 32);
game.debug.renderSpriteInfo(arrow, 32, 100);
}
</script>
<?php
require('../foot.php');
?>

View file

@ -26,7 +26,7 @@
// This will update the sprite.rotation so that it points to the currently active pointer
// On a Desktop that is the mouse, on mobile the most recent finger press.
sprite.rotation = game.physics.angleBetweenPointer(sprite);
sprite.rotation = game.physics.angleToPointer(sprite);
}

View file

@ -5,7 +5,7 @@
<script type="text/javascript">
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update });
function preload() {
game.load.image('arrow', 'assets/sprites/longarrow.png');
@ -39,16 +39,10 @@
// This will update the sprite.rotation so that it points to the currently active pointer
// On a Desktop that is the mouse, on mobile the most recent finger press.
sprite1.rotation = game.physics.angleBetweenPointer(sprite1);
sprite2.rotation = game.physics.angleBetweenPointer(sprite2);
sprite3.rotation = game.physics.angleBetweenPointer(sprite3);
sprite4.rotation = game.physics.angleBetweenPointer(sprite4);
}
function render() {
// game.debug.renderSpriteInfo(sprite1, 32, 32);
sprite1.rotation = game.physics.angleToPointer(sprite1);
sprite2.rotation = game.physics.angleToPointer(sprite2);
sprite3.rotation = game.physics.angleToPointer(sprite3);
sprite4.rotation = game.physics.angleToPointer(sprite4);
}

View file

@ -1,5 +1,5 @@
<?php
$title = "Shoot at the Mouse";
$title = "Shoot the Pointer";
require('../head.php');
?>
@ -8,8 +8,10 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('arrow', 'assets/sprites/arrow.png');
game.load.image('bullet', 'assets/sprites/purple_ball.png');
}
var sprite;
@ -35,7 +37,7 @@
function update() {
sprite.rotation = game.physics.angleBetweenPointer(sprite);
sprite.rotation = game.physics.angleToPointer(sprite);
if (game.input.activePointer.isDown)
{
@ -54,8 +56,7 @@
bullet.reset(sprite.x, sprite.y);
bullet.rotation = game.physics.moveTowardsPointer(bullet, 300);
bullet.rotation = game.physics.moveToPointer(bullet, 300);
}
}
@ -63,9 +64,7 @@
function render() {
game.debug.renderText('Active Bullets: ' + bullets.countLiving() + ' / ' + bullets.total, 32, 32);
game.debug.renderSpriteInfo(sprite, 32, 450);
game.debug.renderSpriteCorners(sprite);
}

View file

@ -251,10 +251,10 @@ Phaser.Camera.prototype = {
this.atLimit.y = false;
// Make sure we didn't go outside the cameras bounds
if (this.view.x < this.bounds.left)
if (this.view.x < this.bounds.x)
{
this.atLimit.x = true;
this.view.x = this.bounds.left;
this.view.x = this.bounds.x;
}
if (this.view.x > this.bounds.right - this.width)

View file

@ -125,7 +125,9 @@ Phaser.World.prototype.postUpdate = function () {
/**
* Updates the size of this world. Note that this doesn't modify the world x/y coordinates, just the width and height.
* If you need to adjust the bounds of the world
* @method Phaser.World#setSize
* @method Phaser.World#setBounds
* @param {number} x - Top left most corner of the world.
* @param {number} y - Top left most corner of the world.
* @param {number} width - New width of the world.
* @param {number} height - New height of the world.
*/

View file

@ -58,9 +58,11 @@ Phaser.Point.prototype = {
* @return {Point} This Point object. Useful for chaining method calls.
**/
setTo: function (x, y) {
this.x = x;
this.y = y;
return this;
},
/**

View file

@ -687,13 +687,20 @@ Phaser.Math = {
* @return {number} The new angle value, returns the same as the input angle if it was within bounds
*/
angleLimit: function (angle, min, max) {
var result = angle;
if (angle > max) {
if (angle > max)
{
result = max;
} else if (angle < min) {
}
else if (angle < min)
{
result = min;
}
return result;
},
/**
@ -704,16 +711,23 @@ Phaser.Math = {
* @return {number}
*/
linearInterpolation: function (v, k) {
var m = v.length - 1;
var f = m * k;
var i = Math.floor(f);
if (k < 0) {
if (k < 0)
{
return this.linear(v[0], v[1], f);
}
if (k > 1) {
if (k > 1)
{
return this.linear(v[m], v[m - 1], m - f);
}
return this.linear(v[i], v[i + 1 > m ? m : i + 1], f - i);
},
/**
@ -724,12 +738,17 @@ Phaser.Math = {
* @return {number}
*/
bezierInterpolation: function (v, k) {
var b = 0;
var n = v.length - 1;
for (var i = 0; i <= n; i++) {
for (var i = 0; i <= n; i++)
{
b += Math.pow(1 - k, n - i) * Math.pow(k, i) * v[i] * this.bernstein(n, i);
}
return b;
},
/**
@ -745,20 +764,31 @@ Phaser.Math = {
var f = m * k;
var i = Math.floor(f);
if (v[0] === v[m]) {
if (k < 0) {
if (v[0] === v[m])
{
if (k < 0)
{
i = Math.floor(f = m * (1 + k));
}
return this.catmullRom(v[(i - 1 + m) % m], v[i], v[(i + 1) % m], v[(i + 2) % m], f - i);
} else {
if (k < 0) {
}
else
{
if (k < 0)
{
return v[0] - (this.catmullRom(v[0], v[0], v[1], v[1], -f) - v[0]);
}
if (k > 1) {
if (k > 1)
{
return v[m] - (this.catmullRom(v[m], v[m], v[m - 1], v[m - 1], f - m) - v[m]);
}
return this.catmullRom(v[i ? i - 1 : 0], v[i], v[m < i + 1 ? m : i + 1], v[m < i + 2 ? m : i + 2], f - i);
}
},
/**
@ -794,8 +824,11 @@ Phaser.Math = {
* @return {number}
*/
catmullRom: function (p0, p1, p2, p3, t) {
var v0 = (p2 - p0) * 0.5, v1 = (p3 - p1) * 0.5, t2 = t * t, t3 = t * t2;
return (2 * p1 - 2 * p2 + v0 + v1) * t3 + (-3 * p1 + 3 * p2 - 2 * v0 - v1) * t2 + v0 * t + p1;
},
/**
@ -1010,6 +1043,21 @@ Phaser.Math = {
return x < a ? a : x;
},
/**
* Checks if two values are within the given tolerance of each other.
*
* @method Phaser.Math#within
* @param {number} a - The first number to check
* @param {number} b - The second number to check
* @param {number} tolerance - The tolerance. Anything equal to or less than this is considered within the range.
* @return {boolean} True if a is <= tolerance of b.
*/
within: function ( a, b, tolerance ) {
return (Math.abs(a - b) <= tolerance);
},
/**
* Linear mapping from range <a1, a2> to range <b1, b2>

View file

@ -1,44 +1,46 @@
Phaser.Physics = {};
Phaser.Physics.Arcade = function (game) {
this.game = game;
this.game = game;
this.gravity = new Phaser.Point;
this.bounds = new Phaser.Rectangle(0, 0, game.world.width, game.world.height);
this.gravity = new Phaser.Point;
this.bounds = new Phaser.Rectangle(0, 0, game.world.width, game.world.height);
/**
* Used by the QuadTree to set the maximum number of objects
* @type {number}
*/
this.maxObjects = 10;
/**
* Used by the QuadTree to set the maximum number of objects
* @type {number}
*/
this.maxObjects = 10;
/**
* Used by the QuadTree to set the maximum number of levels
* @type {number}
*/
this.maxLevels = 4;
/**
* Used by the QuadTree to set the maximum number of levels
* @type {number}
*/
this.maxLevels = 4;
this.OVERLAP_BIAS = 4;
this.TILE_OVERLAP = false;
this.OVERLAP_BIAS = 4;
this.TILE_OVERLAP = false;
this.quadTree = new Phaser.QuadTree(this, this.game.world.bounds.x, this.game.world.bounds.y, this.game.world.bounds.width, this.game.world.bounds.height, this.maxObjects, this.maxLevels);
this.quadTreeID = 0;
this.quadTreeID = 0;
// Avoid gc spikes by caching these values for re-use
this._bounds1 = new Phaser.Rectangle;
this._bounds2 = new Phaser.Rectangle;
this._overlap = 0;
this._maxOverlap = 0;
this._velocity1 = 0;
this._velocity2 = 0;
this._newVelocity1 = 0;
this._newVelocity2 = 0;
this._average = 0;
// Avoid gc spikes by caching these values for re-use
this._bounds1 = new Phaser.Rectangle;
this._bounds2 = new Phaser.Rectangle;
this._overlap = 0;
this._maxOverlap = 0;
this._velocity1 = 0;
this._velocity2 = 0;
this._newVelocity1 = 0;
this._newVelocity2 = 0;
this._average = 0;
this._mapData = [];
this._result = false;
this._total = 0;
this._angle = 0;
this._dx = 0;
this._dy = 0;
};
@ -48,19 +50,19 @@ Phaser.Physics.Arcade.prototype = {
// If you're wondering why the velocity is halved and applied twice, read this: http://www.niksula.hut.fi/~hkankaan/Homepages/gravity.html
// Rotation
// Rotation
this._velocityDelta = (this.computeVelocity(0, body, body.angularVelocity, body.angularAcceleration, body.angularDrag, body.maxAngular) - body.angularVelocity) / 2;
body.angularVelocity += this._velocityDelta;
body.rotation += (body.angularVelocity * this.game.time.physicsElapsed);
body.angularVelocity += this._velocityDelta;
// Horizontal
// Horizontal
this._velocityDelta = (this.computeVelocity(1, body, body.velocity.x, body.acceleration.x, body.drag.x, body.maxVelocity.x) - body.velocity.x) / 2;
body.velocity.x += this._velocityDelta;
body.x += (body.velocity.x * this.game.time.physicsElapsed);
body.velocity.x += this._velocityDelta;
// Vertical
// Vertical
this._velocityDelta = (this.computeVelocity(2, body, body.velocity.y, body.acceleration.y, body.drag.y, body.maxVelocity.y) - body.velocity.y) / 2;
body.velocity.y += this._velocityDelta;
body.y += (body.velocity.y * this.game.time.physicsElapsed);
@ -68,7 +70,7 @@ Phaser.Physics.Arcade.prototype = {
},
/**
/**
* A tween-like function that takes a starting velocity and some other factors and returns an altered velocity.
*
* @param {number} Velocity Any component of velocity (e.g. 20).
@ -80,15 +82,15 @@ Phaser.Physics.Arcade.prototype = {
*/
computeVelocity: function (axis, body, velocity, acceleration, drag, max) {
max = max || 10000;
max = max || 10000;
if (axis == 1 && body.allowGravity)
if (axis == 1 && body.allowGravity)
{
velocity += this.gravity.x + body.gravity.x;
velocity += this.gravity.x + body.gravity.x;
}
else if (axis == 2 && body.allowGravity)
else if (axis == 2 && body.allowGravity)
{
velocity += this.gravity.y + body.gravity.y;
velocity += this.gravity.y + body.gravity.y;
}
if (acceleration !== 0)
@ -131,16 +133,16 @@ Phaser.Physics.Arcade.prototype = {
// Clear the tree
this.quadTree.clear();
// Create our tree which all of the Physics bodies will add themselves to
// Create our tree which all of the Physics bodies will add themselves to
this.quadTreeID = 0;
this.quadTree = new Phaser.QuadTree(this, this.game.world.bounds.x, this.game.world.bounds.y, this.game.world.bounds.width, this.game.world.bounds.height, this.maxObjects, this.maxLevels);
this.quadTree = new Phaser.QuadTree(this, this.game.world.bounds.x, this.game.world.bounds.y, this.game.world.bounds.width, this.game.world.bounds.height, this.maxObjects, this.maxLevels);
},
postUpdate: function () {
// Clear the tree ready for the next update
this.quadTree.clear();
// Clear the tree ready for the next update
this.quadTree.clear();
},
@ -386,7 +388,7 @@ Phaser.Physics.Arcade.prototype = {
},
/**
/**
* The core separation function to separate two physics bodies.
* @param body1 The first Sprite.Body to separate
* @param body2 The second Sprite.Body to separate
@ -985,17 +987,18 @@ Phaser.Physics.Arcade.prototype = {
* Move the given display object towards the pointer at a steady velocity. If no pointer is given it will use Phaser.Input.activePointer.
* If you specify a maxTime then it will adjust the speed (over-writing what you set) so it arrives at the destination in that number of seconds.
* Timings are approximate due to the way browser timers work. Allow for a variance of +- 50ms.
* Note: The display object does not continuously track the target. If the target changes location during transit the display object will not modify its course.
* Note: The display object doesn't stop moving once it reaches the destination coordinates.
* Note: Doesn't take into account acceleration, maxVelocity or drag (if you've set drag or acceleration too high this object may not move at all)
*
* @method Phaser.Physics.Arcade#moveTowardsObject
* @method Phaser.Physics.Arcade#moveToObject
* @param {any} displayObject - The display object to move.
* @param {any} destination - The display object to move towards. Can be any object but must have visible x/y properties.
* @param {number} [speed=60] - The speed it will move, in pixels per second (default is 60 pixels/sec)
* @param {number} [maxTime=0] - Time given in milliseconds (1000 = 1 sec). If set the speed is adjusted so the object will arrive at destination in the given number of ms.
* @return {number} The angle (in radians) that the object should be visually set to in order to match its new velocity.
*/
moveTowardsObject: function (displayObject, destination, speed, maxTime) {
moveToObject: function (displayObject, destination, speed, maxTime) {
speed = speed || 60;
maxTime = maxTime || 0;
@ -1005,7 +1008,7 @@ Phaser.Physics.Arcade.prototype = {
if (maxTime > 0)
{
// We know how many pixels we need to move, but how fast?
speed = this.distanceToMouse(displayObject) / (maxTime / 1000);
speed = this.distanceBetween(displayObject, destination) / (maxTime / 1000);
}
displayObject.body.velocity.x = Math.cos(this._angle) * speed;
@ -1019,27 +1022,28 @@ Phaser.Physics.Arcade.prototype = {
* Move the given display object towards the pointer at a steady velocity. If no pointer is given it will use Phaser.Input.activePointer.
* If you specify a maxTime then it will adjust the speed (over-writing what you set) so it arrives at the destination in that number of seconds.
* Timings are approximate due to the way browser timers work. Allow for a variance of +- 50ms.
* Note: The display object does not continuously track the target. If the target changes location during transit the display object will not modify its course.
* Note: The display object doesn't stop moving once it reaches the destination coordinates.
*
* @method Phaser.Physics.Arcade#moveTowardsPointer
* @method Phaser.Physics.Arcade#moveToPointer
* @param {any} displayObject - The display object to move.
* @param {number} [speed=60] - The speed it will move, in pixels per second (default is 60 pixels/sec)
* @param {Phaser.Pointer} [pointer] - The pointer to move towards. Defaults to Phaser.Input.activePointer.
* @param {number} [maxTime=0] - Time given in milliseconds (1000 = 1 sec). If set the speed is adjusted so the object will arrive at destination in the given number of ms.
* @return {number} The angle (in radians) that the object should be visually set to in order to match its new velocity.
*/
moveTowardsPointer: function (displayObject, speed, pointer, maxTime) {
moveToPointer: function (displayObject, speed, pointer, maxTime) {
speed = speed || 60;
pointer = pointer || this.game.input.activePointer;
maxTime = maxTime || 0;
this._angle = this.angleBetweenPointer(displayObject, pointer);
this._angle = this.angleToPointer(displayObject, pointer);
if (maxTime > 0)
{
// We know how many pixels we need to move, but how fast?
speed = this.distanceToMouse(displayObject) / (maxTime / 1000);
speed = this.distanceToPointer(displayObject, pointer) / (maxTime / 1000);
}
displayObject.body.velocity.x = Math.cos(this._angle) * speed;
@ -1050,30 +1054,32 @@ Phaser.Physics.Arcade.prototype = {
},
/**
* Move the given display object towards the pointer at a steady velocity. If no pointer is given it will use Phaser.Input.activePointer.
* Move the given display object towards the x/y coordinates at a steady velocity.
* If you specify a maxTime then it will adjust the speed (over-writing what you set) so it arrives at the destination in that number of seconds.
* Timings are approximate due to the way browser timers work. Allow for a variance of +- 50ms.
* Note: The display object does not continuously track the target. If the target changes location during transit the display object will not modify its course.
* Note: The display object doesn't stop moving once it reaches the destination coordinates.
* Note: Doesn't take into account acceleration, maxVelocity or drag (if you've set drag or acceleration too high this object may not move at all)
*
* @method Phaser.Physics.Arcade#moveTowardsObject
* @method Phaser.Physics.Arcade#moveToXY
* @param {any} displayObject - The display object to move.
* @param {any} destination - The display object to move towards. Can be any object but must have visible x/y properties.
* @param {number} x - The x coordinate to move towards.
* @param {number} y - The y coordinate to move towards.
* @param {number} [speed=60] - The speed it will move, in pixels per second (default is 60 pixels/sec)
* @param {number} [maxTime=0] - Time given in milliseconds (1000 = 1 sec). If set the speed is adjusted so the object will arrive at destination in the given number of ms.
* @return {number} The angle (in radians) that the object should be visually set to in order to match its new velocity.
*/
moveTowardsXY: function (displayObject, x, y, speed, maxTime) {
moveToXY: function (displayObject, x, y, speed, maxTime) {
speed = speed || 60;
maxTime = maxTime || 0;
this._angle = this.angleBetweenPointer(displayObject, destination);
this._angle = Math.atan2(y - displayObject.y, x - displayObject.x);
if (maxTime > 0)
{
// We know how many pixels we need to move, but how fast?
speed = this.distanceToMouse(displayObject) / (maxTime / 1000);
speed = this.distanceToXY(displayObject, x, y) / (maxTime / 1000);
}
displayObject.body.velocity.x = Math.cos(this._angle) * speed;
@ -1121,248 +1127,223 @@ Phaser.Physics.Arcade.prototype = {
},
/**
* Sets the x/y acceleration on the source Sprite so it will move towards the destination Sprite at the speed given (in pixels per second)<br>
* You must give a maximum speed value, beyond which the Sprite won't go any faster.<br>
* If you don't need acceleration look at moveTowardsObject() instead.
*
* @param source The Sprite on which the acceleration will be set
* @param dest The Sprite where the source object will move towards
* @param speed The speed it will accelerate in pixels per second
* @param xSpeedMax The maximum speed in pixels per second in which the sprite can move horizontally
* @param ySpeedMax The maximum speed in pixels per second in which the sprite can move vertically
*/
accelerateTowardsObject: function (source, dest, speed, xSpeedMax, ySpeedMax) {
* Given the rotation (in radians) and speed calculate the acceleration and return it as a Point object, or set it to the given point object.
* One way to use this is: velocityFromRotation(rotation, 200, sprite.velocity) which will set the values directly to the sprites velocity and not create a new Point object.
*
* @method Phaser.Physics.Arcade#accelerationFromRotation
* @param {number} rotation - The angle in radians.
* @param {number} [speed=60] - The speed it will move, in pixels per second sq.
* @param {Phaser.Point|object} [point] - The Point object in which the x and y properties will be set to the calculated acceleration.
* @return {Phaser.Point} - A Point where point.x contains the acceleration x value and point.y contains the acceleration y value.
*/
accelerationFromRotation: function (rotation, speed, point) {
xSpeedMax = xSpeedMax || 1000;
ySpeedMax = ySpeedMax || 1000;
speed = speed || 60;
point = point || new Phaser.Point;
var a = this.angleBetween(source, dest);
source.body.velocity.x = 0;
source.body.velocity.y = 0;
source.body.acceleration.x = Math.cos(a) * speed;
source.body.acceleration.y = Math.sin(a) * speed;
source.body.maxVelocity.x = xSpeedMax;
source.body.maxVelocity.y = ySpeedMax;
},
/**
* Sets the x/y acceleration on the source Sprite so it will move towards the mouse coordinates at the speed given (in pixels per second)<br>
* You must give a maximum speed value, beyond which the Sprite won't go any faster.<br>
* If you don't need acceleration look at moveTowardsMouse() instead.
*
* @param source The Sprite on which the acceleration will be set
* @param speed The speed it will accelerate in pixels per second
* @param xSpeedMax The maximum speed in pixels per second in which the sprite can move horizontally
* @param ySpeedMax The maximum speed in pixels per second in which the sprite can move vertically
*/
accelerateTowardsMouse: function (source, speed, xSpeedMax, ySpeedMax) {
xSpeedMax = xSpeedMax || 1000;
ySpeedMax = ySpeedMax || 1000;
var a = this.angleBetweenMouse(source, true);
source.body.velocity.x = 0;
source.body.velocity.y = 0;
source.body.acceleration.x = Math.cos(a) * speed;
source.body.acceleration.y = Math.sin(a) * speed;
source.body.maxVelocity.x = xSpeedMax;
source.body.maxVelocity.y = ySpeedMax;
},
/**
* Sets the x/y acceleration on the source Sprite so it will move towards the target coordinates at the speed given (in pixels per second)<br>
* You must give a maximum speed value, beyond which the Sprite won't go any faster.<br>
* If you don't need acceleration look at moveTowardsPoint() instead.
*
* @param source The Sprite on which the acceleration will be set
* @param target The Point coordinates to move the source Sprite towards
* @param speed The speed it will accelerate in pixels per second
* @param xSpeedMax The maximum speed in pixels per second in which the sprite can move horizontally
* @param ySpeedMax The maximum speed in pixels per second in which the sprite can move vertically
*/
accelerateTowardsPoint: function (source, target, speed, xSpeedMax, ySpeedMax) {
xSpeedMax = xSpeedMax || 1000;
ySpeedMax = ySpeedMax || 1000;
var a = this.angleBetweenPoint(source, target);
source.body.velocity.x = 0;
source.body.velocity.y = 0;
source.body.acceleration.x = Math.cos(a) * speed;
source.body.acceleration.y = Math.sin(a) * speed;
source.body.maxVelocity.x = xSpeedMax;
source.body.maxVelocity.y = ySpeedMax;
return point.setTo((Math.cos(rotation) * speed), (Math.sin(rotation) * speed));
},
/**
* Find the distance (in pixels, rounded) between two Sprites, taking their origin into account
*
* @param a The first Sprite
* @param b The second Sprite
* @return int Distance (in pixels)
*/
distanceBetween: function (a, b) {
* Sets the acceleration.x/y property on the display object so it will move towards the target at the given speed (in pixels per second sq.)
* You must give a maximum speed value, beyond which the display object won't go any faster.
* Note: The display object does not continuously track the target. If the target changes location during transit the display object will not modify its course.
* Note: The display object doesn't stop moving once it reaches the destination coordinates.
*
* @method Phaser.Physics.Arcade#accelerateToObject
* @param {any} displayObject - The display object to move.
* @param {any} destination - The display object to move towards. Can be any object but must have visible x/y properties.
* @param {number} [speed=60] - The speed it will accelerate in pixels per second.
* @param {number} [xSpeedMax=500] - The maximum x velocity the display object can reach.
* @param {number} [ySpeedMax=500] - The maximum y velocity the display object can reach.
* @return {number} The angle (in radians) that the object should be visually set to in order to match its new trajectory.
*/
accelerateToObject: function (displayObject, destination, speed, xSpeedMax, ySpeedMax) {
var dx = a.center.x - b.center.x;
var dy = a.center.y - b.center.y;
return Math.sqrt(dx * dx + dy * dy);
if (typeof speed === 'undefined') { speed = 60; }
if (typeof xSpeedMax === 'undefined') { xSpeedMax = 1000; }
if (typeof ySpeedMax === 'undefined') { ySpeedMax = 1000; }
this._angle = this.angleBetween(displayObject, destination);
displayObject.body.acceleration.setTo(Math.cos(this._angle) * speed, Math.sin(this._angle) * speed);
displayObject.body.maxVelocity.setTo(xSpeedMax, ySpeedMax);
return this._angle;
},
/**
* Find the distance (in pixels, rounded) from an Sprite to the given Point, taking the source origin into account
*
* @param a The Sprite
* @param target The Point
* @return int Distance (in pixels)
*/
distanceToPoint: function (a, target) {
* Sets the acceleration.x/y property on the display object so it will move towards the target at the given speed (in pixels per second sq.)
* You must give a maximum speed value, beyond which the display object won't go any faster.
* Note: The display object does not continuously track the target. If the target changes location during transit the display object will not modify its course.
* Note: The display object doesn't stop moving once it reaches the destination coordinates.
*
* @method Phaser.Physics.Arcade#accelerateToPointer
* @param {any} displayObject - The display object to move.
* @param {Phaser.Pointer} [pointer] - The pointer to move towards. Defaults to Phaser.Input.activePointer.
* @param {number} [speed=60] - The speed it will accelerate in pixels per second.
* @param {number} [xSpeedMax=500] - The maximum x velocity the display object can reach.
* @param {number} [ySpeedMax=500] - The maximum y velocity the display object can reach.
* @return {number} The angle (in radians) that the object should be visually set to in order to match its new trajectory.
*/
accelerateToPointer: function (displayObject, pointer, speed, xSpeedMax, ySpeedMax) {
var dx = a.center.x - target.x;
var dy = a.center.y - target.y;
if (typeof speed === 'undefined') { speed = 60; }
if (typeof pointer === 'undefined') { pointer = this.game.input.activePointer; }
if (typeof xSpeedMax === 'undefined') { xSpeedMax = 1000; }
if (typeof ySpeedMax === 'undefined') { ySpeedMax = 1000; }
this._angle = this.angleToPointer(displayObject, pointer);
return Math.sqrt(dx * dx + dy * dy);
displayObject.body.acceleration.setTo(Math.cos(this._angle) * speed, Math.sin(this._angle) * speed);
displayObject.body.maxVelocity.setTo(xSpeedMax, ySpeedMax);
return this._angle;
},
/**
* Find the distance (in pixels, rounded) from the object x/y and the mouse x/y
*
* @param a The Sprite to test against
* @return int The distance between the given sprite and the mouse coordinates
*/
distanceToMouse: function (a) {
* Sets the acceleration.x/y property on the display object so it will move towards the x/y coordinates at the given speed (in pixels per second sq.)
* You must give a maximum speed value, beyond which the display object won't go any faster.
* Note: The display object does not continuously track the target. If the target changes location during transit the display object will not modify its course.
* Note: The display object doesn't stop moving once it reaches the destination coordinates.
*
* @method Phaser.Physics.Arcade#accelerateToXY
* @param {any} displayObject - The display object to move.
* @param {number} x - The x coordinate to accelerate towards.
* @param {number} y - The y coordinate to accelerate towards.
* @param {number} [speed=60] - The speed it will accelerate in pixels per second.
* @param {number} [xSpeedMax=500] - The maximum x velocity the display object can reach.
* @param {number} [ySpeedMax=500] - The maximum y velocity the display object can reach.
* @return {number} The angle (in radians) that the object should be visually set to in order to match its new trajectory.
*/
accelerateToXY: function (displayObject, x, y, speed, xSpeedMax, ySpeedMax) {
var dx = a.center.x - this.game.input.x;
var dy = a.center.y - this.game.input.y;
return Math.sqrt(dx * dx + dy * dy);
if (typeof speed === 'undefined') { speed = 60; }
if (typeof xSpeedMax === 'undefined') { xSpeedMax = 1000; }
if (typeof ySpeedMax === 'undefined') { ySpeedMax = 1000; }
this._angle = this.angleToXY(displayObject, x, y);
displayObject.body.acceleration.setTo(Math.cos(this._angle) * speed, Math.sin(this._angle) * speed);
displayObject.body.maxVelocity.setTo(xSpeedMax, ySpeedMax);
return this._angle;
},
/**
* Find the angle (in radians) between an Sprite and an Point. The source sprite takes its x/y and origin into account.
* The angle is calculated in clockwise positive direction (down = 90 degrees positive, right = 0 degrees positive, up = 90 degrees negative)
*
* @param a The Sprite to test from
* @param target The Point to angle the Sprite towards
* @param asDegrees If you need the value in degrees instead of radians, set to true
*
* @return Number The angle (in radians unless asDegrees is true)
*/
angleBetweenPoint: function (a, target, asDegrees) {
* Find the distance between two display objects (like Sprites).
*
* @method Phaser.Physics.Arcade#distanceBetween
* @param {any} source - The Display Object to test from.
* @param {any} target - The Display Object to test to.
* @return {number} The distance between the source and target objects.
*/
distanceBetween: function (source, target) {
asDegrees = asDegrees || false;
var dx = target.x - a.center.x;
var dy = target.y - a.center.y;
this._dx = source.x - target.x;
this._dy = source.y - target.y;
if (asDegrees)
{
return this.game.math.radToDeg(Math.atan2(dy, dx));
}
else
{
return Math.atan2(dy, dx);
}
return Math.sqrt(this._dx * this._dx + this._dy * this._dy);
},
/**
* Find the angle (in radians) between the two Sprite, taking their x/y and origin into account.
* The angle is calculated in clockwise positive direction (down = 90 degrees positive, right = 0 degrees positive, up = 90 degrees negative)
*
* @param a The Sprite to test from
* @param b The Sprite to test to
* @param asDegrees If you need the value in degrees instead of radians, set to true
*
* @return Number The angle (in radians unless asDegrees is true)
*/
angleBetween: function (a, b, asDegrees) {
* Find the distance between a display object (like a Sprite) and the given x/y coordinates.
* The calculation is made from the display objects x/y coordinate. This may be the top-left if its anchor hasn't been changed.
* If you need to calculate from the center of a display object instead use the method distanceBetweenCenters()
*
* @method Phaser.Physics.Arcade#distanceToXY
* @param {any} displayObject - The Display Object to test from.
* @param {number} x - The x coordinate to move towards.
* @param {number} y - The y coordinate to move towards.
* @return {number} The distance between the object and the x/y coordinates.
*/
distanceToXY: function (displayObject, x, y) {
asDegrees = asDegrees || false;
var dx = b.center.x - a.center.x;
var dy = b.center.y - a.center.y;
this._dx = displayObject.x - x;
this._dy = displayObject.y - y;
if (asDegrees)
{
return this.game.math.radToDeg(Math.atan2(dy, dx));
}
else
{
return Math.atan2(dy, dx);
}
return Math.sqrt(this._dx * this._dx + this._dy * this._dy);
},
/**
* Given the GameObject and speed calculate the velocity and return it as an Point based on the direction the sprite is facing
*
* @param parent The Sprite to get the facing value from
* @param speed The speed it will move, in pixels per second sq
*
* @return An Point where Point.x contains the velocity x value and Point.y contains the velocity y value
*/
velocityFromFacing: function (parent, speed) {
* Find the distance between a display object (like a Sprite) and a Pointer. If no Pointer is given the Input.activePointer is used.
* The calculation is made from the display objects x/y coordinate. This may be the top-left if its anchor hasn't been changed.
* If you need to calculate from the center of a display object instead use the method distanceBetweenCenters()
*
* @method Phaser.Physics.Arcade#distanceToPointer
* @param {any} displayObject - The Display Object to test from.
* @param {Phaser.Pointer} [pointer] - The Phaser.Pointer to test to. If none is given then Input.activePointer is used.
* @return {number} The distance between the object and the Pointer.
*/
distanceToPointer: function (displayObject, pointer) {
/*
var a;
pointer = pointer || this.game.input.activePointer;
this._dx = displayObject.x - pointer.x;
this._dy = displayObject.y - pointer.y;
if (parent.facing == Collision.LEFT)
{
a = this._game.math.degreesToRadians(180);
}
else if (parent.facing == Collision.RIGHT)
{
a = this._game.math.degreesToRadians(0);
}
else if (parent.facing == Collision.UP)
{
a = this._game.math.degreesToRadians(-90);
}
else if (parent.facing == Collision.DOWN)
{
a = this._game.math.degreesToRadians(90);
}
return Math.sqrt(this._dx * this._dx + this._dy * this._dy);
},
/**
* Find the angle in radians between two display objects (like Sprites).
*
* @method Phaser.Physics.Arcade#angleBetween
* @param {any} source - The Display Object to test from.
* @param {any} target - The Display Object to test to.
* @return {number} The angle in radians between the source and target display objects.
*/
angleBetween: function (source, target) {
this._dx = target.x - source.x;
this._dy = target.y - source.y;
return Math.atan2(this._dy, this._dx);
},
/**
* Find the angle in radians between a display object (like a Sprite) and the given x/y coordinate.
*
* @method Phaser.Physics.Arcade#angleToXY
* @param {any} displayObject - The Display Object to test from.
* @param {number} x - The x coordinate to get the angle to.
* @param {number} y - The y coordinate to get the angle to.
* @return {number} The angle in radians between displayObject.x/y to Pointer.x/y
*/
angleToXY: function (displayObject, x, y) {
this._dx = x - displayObject.x;
this._dy = y - displayObject.y;
return new Point(Math.cos(a) * speed, Math.sin(a) * speed);
*/
return Math.atan2(this._dy, this._dx);
},
/**
* Find the angle in radians between a display object (like a Sprite) and a Pointer, taking their x/y and center into account.
*
* @method Phaser.Physics.Arcade#angleToPointer
* @param {any} displayObject - The Display Object to test from.
* @param {Phaser.Pointer} [pointer] - The Phaser.Pointer to test to. If none is given then Input.activePointer is used.
* @return {number} The angle in radians between displayObject.center.x/y to Pointer.x/y
* @return {number} The angle in radians between displayObject.x/y to Pointer.x/y
*/
angleBetweenPointer: function (displayObject, pointer) {
angleToPointer: function (displayObject, pointer) {
pointer = pointer || this.game.input.activePointer;
var dx = pointer.x - displayObject.x;
var dy = pointer.y - displayObject.y;
this._dx = pointer.x - displayObject.x;
this._dy = pointer.y - displayObject.y;
return Math.atan2(dy, dx);
return Math.atan2(this._dy, this._dx);
}

View file

@ -298,6 +298,8 @@ Phaser.Utils.Debug.prototype = {
this.start(x, y, color);
this.line('Camera (' + camera.width + ' x ' + camera.height + ')');
this.line('X: ' + camera.x + ' Y: ' + camera.y);
this.line('Bounds x: ' + camera.bounds.x + ' Y: ' + camera.bounds.y + ' w: ' + camera.bounds.width + ' h: ' + camera.bounds.height);
this.line('View x: ' + camera.view.x + ' Y: ' + camera.view.y + ' w: ' + camera.view.width + ' h: ' + camera.view.height);
this.stop();
},