Skip to content

Commit bdc1c2c

Browse files
committed
Sorted out the bounds for when sprites are in trimmed texture atlases to stop the physics checks going insane. Also bundled in Advanced Physics lib, although not hooked up yet.
1 parent 923c250 commit bdc1c2c

34 files changed

Lines changed: 7598 additions & 126 deletions

Phaser.sublime-workspace

Lines changed: 1301 additions & 53 deletions
Large diffs are not rendered by default.

examples/body1.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<title>phaser.js - a new beginning</title>
5+
<?php
6+
require('js.php');
7+
?>
8+
</head>
9+
<body>
10+
11+
<script type="text/javascript">
12+
13+
(function () {
14+
15+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
16+
17+
function preload() {
18+
game.load.atlasJSONHash('bot', 'assets/sprites/running_bot.png', 'assets/sprites/running_bot.json');
19+
}
20+
21+
var s;
22+
23+
function create() {
24+
25+
game.world._stage.backgroundColorString = '#182d3b';
26+
27+
s = game.add.sprite(game.world.centerX, game.world.centerY, 'bot');
28+
// s.anchor.setTo(0.5, 0.5);
29+
30+
// s.body.offset.setTo(0, 0);
31+
32+
33+
// s.scale.setTo(2, 2);
34+
35+
s.animations.add('run');
36+
s.animations.play('run', 10, true);
37+
38+
}
39+
40+
function update() {
41+
42+
s.rotation += 0.01;
43+
44+
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
45+
{
46+
s.x -= 4;
47+
}
48+
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
49+
{
50+
s.x += 4;
51+
}
52+
53+
if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
54+
{
55+
s.y -= 4;
56+
}
57+
else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN))
58+
{
59+
s.y += 4;
60+
}
61+
62+
}
63+
64+
function render() {
65+
66+
game.debug.renderSpriteCorners(s, true, true);
67+
// game.debug.renderRectangle(s.body.bounds, 'rgba(255,0,0,0.3)');
68+
game.debug.renderSpriteInfo(s, 20, 32);
69+
70+
}
71+
72+
})();
73+
74+
</script>
75+
76+
</body>
77+
</html>

examples/js.php

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
<script src="../src/pixi/textures/BaseTexture.js"></script>
3939
<script src="../src/pixi/textures/Texture.js"></script>
4040
<script src="../src/pixi/textures/RenderTexture.js"></script>
41-
<!--<script src="../src/pixi/utils/Detector.js"></script>-->
4241
<script src="../src/pixi/utils/EventTarget.js"></script>
4342
<script src="../src/pixi/utils/Polyk.js"></script>
4443
<script src="../src/pixi/utils/Utils.js"></script>
@@ -53,51 +52,64 @@
5352
<script src="../src/core/Stage.js"></script>
5453
<script src="../src/core/World.js"></script>
5554
<script src="../src/core/Game.js"></script>
56-
5755
<script src="../src/input/Input.js"></script>
5856
<script src="../src/input/Keyboard.js"></script>
5957
<script src="../src/input/Mouse.js"></script>
6058
<script src="../src/input/MSPointer.js"></script>
6159
<script src="../src/input/Pointer.js"></script>
6260
<script src="../src/input/Touch.js"></script>
63-
6461
<script src="../src/system/Canvas.js"></script>
6562
<script src="../src/gameobjects/GameObjectFactory.js"></script>
6663
<script src="../src/gameobjects/Sprite.js"></script>
6764
<script src="../src/gameobjects/TileSprite.js"></script>
6865
<script src="../src/gameobjects/Text.js"></script>
69-
7066
<script src="../src/system/Canvas.js"></script>
7167
<script src="../src/system/Device.js"></script>
7268
<script src="../src/system/RequestAnimationFrame.js"></script>
73-
7469
<script src="../src/math/RandomDataGenerator.js"></script>
7570
<script src="../src/math/Math.js"></script>
76-
7771
<script src="../src/geom/Circle.js"></script>
7872
<script src="../src/geom/Point.js"></script>
7973
<script src="../src/geom/Rectangle.js"></script>
80-
8174
<script src="../src/net/Net.js"></script>
82-
8375
<script src="../src/tween/TweenManager.js"></script>
8476
<script src="../src/tween/Tween.js"></script>
8577
<script src="../src/tween/Easing.js"></script>
86-
8778
<script src="../src/time/Time.js"></script>
88-
8979
<script src="../src/animation/AnimationManager.js"></script>
9080
<script src="../src/animation/Animation.js"></script>
9181
<script src="../src/animation/Frame.js"></script>
9282
<script src="../src/animation/FrameData.js"></script>
9383
<script src="../src/animation/Parser.js"></script>
94-
9584
<script src="../src/loader/Cache.js"></script>
9685
<script src="../src/loader/Loader.js"></script>
97-
9886
<script src="../src/sound/Sound.js"></script>
9987
<script src="../src/sound/SoundManager.js"></script>
100-
10188
<script src="../src/utils/Debug.js"></script>
10289

90+
<script src="../src/physics/arcade/ArcadePhysics.js"></script>
91+
<script src="../src/physics/arcade/Body.js"></script>
92+
93+
<script src="../src/physics/advanced/Math.js"></script>
94+
<script src="../src/physics/advanced/Util.js"></script>
95+
<script src="../src/physics/advanced/Collision.js"></script>
96+
<script src="../src/physics/advanced/Body.js"></script>
97+
<script src="../src/physics/advanced/Joint.js"></script>
98+
<script src="../src/physics/advanced/Shape.js"></script>
99+
<script src="../src/physics/advanced/Contact.js"></script>
100+
<script src="../src/physics/advanced/ContactSolver.js"></script>
101+
<script src="../src/physics/advanced/Space.js"></script>
102+
<script src="../src/physics/advanced/joints/Angle.js"></script>
103+
<script src="../src/physics/advanced/joints/Revolute.js"></script>
104+
<script src="../src/physics/advanced/joints/Weld.js"></script>
105+
<script src="../src/physics/advanced/joints/Wheel.js"></script>
106+
<script src="../src/physics/advanced/joints/Prismatic.js"></script>
107+
<script src="../src/physics/advanced/joints/Distance.js"></script>
108+
<script src="../src/physics/advanced/joints/Rope.js"></script>
109+
<script src="../src/physics/advanced/joints/Mouse.js"></script>
110+
<script src="../src/physics/advanced/shapes/Circle.js"></script>
111+
<script src="../src/physics/advanced/shapes/Segment.js"></script>
112+
<script src="../src/physics/advanced/shapes/Poly.js"></script>
113+
<script src="../src/physics/advanced/shapes/Triangle.js"></script>
114+
<script src="../src/physics/advanced/shapes/Box.js"></script>
103115

examples/sprite4.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<title>phaser.js - a new beginning</title>
5+
<?php
6+
require('js.php');
7+
?>
8+
</head>
9+
<body>
10+
11+
<script type="text/javascript">
12+
13+
(function () {
14+
15+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
16+
17+
function preload() {
18+
game.load.atlasJSONHash('bot', 'assets/sprites/running_bot.png', 'assets/sprites/running_bot.json');
19+
}
20+
21+
var s;
22+
23+
function create() {
24+
25+
// game.world._stage.backgroundColorString = '#182d3b';
26+
27+
s = game.add.sprite(game.world.centerX, game.world.centerY, 'bot');
28+
// s.anchor.setTo(0.5, 0.5);
29+
s.scale.setTo(2, 2);
30+
31+
s.animations.add('run');
32+
s.animations.play('run', 10, true);
33+
34+
}
35+
36+
function update() {
37+
38+
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
39+
{
40+
s.x -= 4;
41+
}
42+
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
43+
{
44+
s.x += 4;
45+
}
46+
47+
if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
48+
{
49+
s.y -= 4;
50+
}
51+
else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN))
52+
{
53+
s.y += 4;
54+
}
55+
56+
}
57+
58+
function render() {
59+
60+
game.debug.renderSpriteCorners(s, false, false);
61+
game.debug.renderSpriteInfo(s, 20, 32);
62+
63+
}
64+
65+
})();
66+
67+
</script>
68+
69+
</body>
70+
</html>

src/animation/Animation.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ Phaser.Animation.prototype = {
4141
*/
4242
play: function (frameRate, loop) {
4343

44-
if (typeof frameRate === "undefined") { frameRate = null; }
45-
if (typeof loop === "undefined") { loop = null; }
44+
frameRate = frameRate || null;
45+
loop = loop || null;
4646

4747
if (frameRate !== null)
4848
{
@@ -113,6 +113,7 @@ Phaser.Animation.prototype = {
113113
{
114114
this._frameIndex = 0;
115115
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
116+
this._parent.setTexture(PIXI.TextureCache[this.currentFrame.uuid]);
116117
// this._parent.events.onAnimationLoop.dispatch(this._parent, this);
117118
}
118119
else

src/animation/AnimationManager.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,19 +177,24 @@ Phaser.AnimationManager.prototype = {
177177

178178
/**
179179
* Update animation and parent sprite's bounds.
180+
* Returns true if a new frame has been set, otherwise false.
180181
*/
181182
update: function () {
182183

183184
if (this.updateIfVisible && this._parent.visible == false)
184185
{
185-
return;
186+
return false;
186187
}
187188

188189
if (this.currentAnim && this.currentAnim.update() == true)
189190
{
190191
this.currentFrame = this.currentAnim.currentFrame;
192+
this._parent.currentFrame = this.currentFrame;
193+
return true;
191194
}
192195

196+
return false;
197+
193198
},
194199

195200
/**
@@ -256,6 +261,7 @@ Object.defineProperty(Phaser.AnimationManager.prototype, "frame", {
256261
{
257262
this.currentFrame = this._frameData.getFrame(value);
258263
this._frameIndex = value;
264+
this._parent.currentFrame = this.currentFrame;
259265
this._parent.setTexture(PIXI.TextureCache[this.currentFrame.uuid]);
260266
}
261267

@@ -282,6 +288,7 @@ Object.defineProperty(Phaser.AnimationManager.prototype, "frameName", {
282288
{
283289
this.currentFrame = this._frameData.getFrameByName(value);
284290
this._frameIndex = this.currentFrame.index;
291+
this._parent.currentFrame = this.currentFrame;
285292
this._parent.setTexture(PIXI.TextureCache[this.currentFrame.uuid]);
286293
}
287294
else

src/animation/Frame.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ Phaser.Animation.Frame = function (x, y, width, height, name, uuid) {
1414
this.y = y;
1515
this.width = width;
1616
this.height = height;
17+
this.sourceSizeW = width;
18+
this.sourceSizeH = height;
19+
this.centerX = Math.floor(width / 2);
20+
this.centerY = Math.floor(height / 2);
1721
this.name = name;
1822
this.uuid = uuid;
1923
this.distance = Phaser.Math.distance(0, 0, width, height);
@@ -51,6 +55,18 @@ Phaser.Animation.Frame.prototype = {
5155
*/
5256
height: 0,
5357

58+
/**
59+
* center X position within the image to cut from.
60+
* @type {number}
61+
*/
62+
centerX: 0,
63+
64+
/**
65+
* center Y position within the image to cut from.
66+
* @type {number}
67+
*/
68+
centerY: 0,
69+
5470
/**
5571
* The distance from the top left to the bottom-right of this Frame.
5672
* @type {number}
@@ -136,11 +152,14 @@ Phaser.Animation.Frame.prototype = {
136152

137153
this.trimmed = trimmed;
138154

139-
if (trimmed) {
155+
if (trimmed)
156+
{
140157
this.width = actualWidth;
141158
this.height = actualHeight;
142159
this.sourceSizeW = actualWidth;
143160
this.sourceSizeH = actualHeight;
161+
this.centerX = Math.floor(actualWidth / 2);
162+
this.centerY = Math.floor(actualHeight / 2);
144163
this.spriteSourceSizeX = destX;
145164
this.spriteSourceSizeY = destY;
146165
this.spriteSourceSizeW = destWidth;

0 commit comments

Comments
 (0)