Skip to content

Commit 3de6290

Browse files
committed
Nearly fixed the tilemap / body issue. More tests needed but then can push to master.
1 parent 7ceb11a commit 3de6290

8 files changed

Lines changed: 92 additions & 74 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Version 1.1.2
4444
* Fixed issue 136 - distanceTo using worldX/Y instead of x/y.
4545
* Added init method to plugins, to be called as they are added to the PluginManager (thanks beeglebug)
4646
* If you pause an Animation, when you next play it it'll resume (un-pause itself).
47+
* Started work on fixing the body / camera / tilemap issue - pretty much sorted now I think, more tests needed.
4748

4849

4950

examples/camera/basic follow.js

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,72 @@
11

22
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
33

4-
var baddie;
5-
var keys = Phaser.Keyboard;
6-
74
function preload() {
85

96
game.load.image('background','assets/misc/starfield.jpg');
10-
game.load.image('ufo','assets/sprites/ufo.png');
11-
game.load.image('baddie','assets/sprites/space-baddie.png');
7+
game.load.image('ufo','assets/sprites/space-baddie.png');
8+
game.load.image('player','assets/sprites/phaser-dude.png');
129

1310
}
1411

12+
var player;
13+
var fixed;
14+
var cursors;
15+
1516
function create() {
1617

1718
game.add.tileSprite(0, 0, 2000, 2000, 'background');
1819

19-
game.world.setBounds(0, 0, 1400,1400);
20+
game.world.setBounds(0, 0, 1400, 1400);
2021

21-
for (var i = 0; i < 10; i++)
22+
for (var i = 0; i < 100; i++)
2223
{
2324
game.add.sprite(game.world.randomX, game.world.randomY, 'ufo');
2425
}
2526

26-
baddie = game.add.sprite(150, 320, 'baddie');
27+
fixed = game.add.sprite(300, 320, 'player');
28+
fixed.fixedToCamera = true;
29+
fixed.cameraOffset.x = 300;
30+
fixed.cameraOffset.y = 300;
31+
32+
player = game.add.sprite(150, 320, 'player');
33+
34+
cursors = game.input.keyboard.createCursorKeys();
2735

28-
game.camera.follow(baddie);
36+
game.camera.follow(player);
2937

3038
}
3139

3240
function update() {
3341

34-
baddie.body.velocity.setTo(0, 0);
42+
player.body.velocity.setTo(0, 0);
3543

36-
if (game.input.keyboard.isDown(keys.LEFT) && !game.input.keyboard.isDown(keys.RIGHT))
44+
if (cursors.up.isDown)
3745
{
38-
baddie.body.velocity.x = -155;
46+
player.body.velocity.y = -200;
3947
}
40-
else if (game.input.keyboard.isDown(keys.RIGHT) && !game.input.keyboard.isDown(keys.LEFT))
48+
else if (cursors.down.isDown)
4149
{
42-
baddie.body.velocity.x = 155;
50+
player.body.velocity.y = 200;
4351
}
44-
else if (game.input.keyboard.isDown(keys.UP) && !game.input.keyboard.isDown(keys.DOWN))
52+
53+
if (cursors.left.isDown)
4554
{
46-
baddie.angle = 90;
47-
baddie.body.velocity.y = -155;
55+
player.body.velocity.x = -200;
4856
}
49-
else if (game.input.keyboard.isDown(keys.DOWN) && !game.input.keyboard.isDown(keys.UP))
57+
else if (cursors.right.isDown)
5058
{
51-
baddie.angle = 90;
52-
baddie.body.velocity.y = 155;
59+
player.body.velocity.x = 200;
5360
}
61+
5462
}
5563

5664
function render() {
5765

5866
game.debug.renderCameraInfo(game.camera, 32, 32);
67+
game.debug.renderSpriteCoords(player, 32, 200);
68+
game.debug.renderSpriteCoords(fixed, 600, 200);
69+
70+
game.debug.renderSpriteCoords(game.world._container, 32, 400);
5971

6072
}
Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,52 @@
11

2-
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update });
2+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
33

44
function preload() {
55
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
66
}
77

8+
var cursors;
9+
810
function create() {
911

1012
game.stage.backgroundColor = '#2d2d2d';
1113

1214
// Make our game world 2000x2000 pixels in size (the default is to match the game size)
13-
game.world.setBounds(0,0,2000, 2000);
15+
game.world.setBounds(0, 0, 2000, 2000);
1416

15-
for (var i = 0; i < 50; i++)
16-
{
17-
var s = game.add.sprite(game.world.randomX, game.world.randomY, 'mushroom');
18-
}
19-
20-
for (var i = 0; i < 50; i++)
17+
for (var i = 0; i < 150; i++)
2118
{
2219
game.add.sprite(game.world.randomX, game.world.randomY, 'mushroom');
2320
}
2421

25-
for (var i = 0; i < 50; i++)
26-
{
27-
var s = game.add.sprite(game.world.randomX, game.world.randomY, 'mushroom');
28-
}
22+
cursors = game.input.keyboard.createCursorKeys();
2923

3024
}
3125

3226
function update() {
3327

34-
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
28+
if (cursors.up.isDown)
3529
{
36-
game.camera.x -= 4;
30+
game.camera.y -= 4;
3731
}
38-
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
32+
else if (cursors.down.isDown)
3933
{
40-
game.camera.x += 4;
34+
game.camera.y += 4;
4135
}
4236

43-
if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
37+
if (cursors.left.isDown)
4438
{
45-
game.camera.y -= 4;
39+
game.camera.x -= 4;
4640
}
47-
else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN))
41+
else if (cursors.right.isDown)
4842
{
49-
game.camera.y += 4;
43+
game.camera.x += 4;
5044
}
5145

5246
}
47+
48+
function render() {
49+
50+
game.debug.renderCameraInfo(game.camera, 32, 32);
51+
52+
}

examples/tilemaps/sci fly.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function particleBurst() {
8080

8181
function update() {
8282

83-
// game.physics.collide(sprite, layer);
83+
game.physics.collide(sprite, layer);
8484
game.physics.collide(emitter, layer);
8585

8686
sprite.body.velocity.x = 0;
@@ -111,7 +111,7 @@ function update() {
111111

112112
function render() {
113113

114-
// game.debug.renderSpriteCorners(sprite);
114+
game.debug.renderSpriteCorners(sprite);
115115
// game.debug.renderSpriteInfo(sprite, 32, 32);
116116
game.debug.renderSpriteCoords(sprite, 32, 32);
117117

src/core/Camera.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,8 @@ Phaser.Camera.prototype = {
195195
this.checkBounds();
196196
}
197197

198-
if (this.view.x !== -this.displayObject.position.x)
199-
{
200-
this.displayObject.position.x = -this.view.x;
201-
}
202-
203-
if (this.view.y !== -this.displayObject.position.y)
204-
{
205-
this.displayObject.position.y = -this.view.y;
206-
}
198+
this.displayObject.position.x = -this.view.x;
199+
this.displayObject.position.y = -this.view.y;
207200

208201
},
209202

src/gameobjects/Sprite.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ Phaser.Sprite = function (game, x, y, key, frame) {
306306
* @default
307307
*/
308308
this.fixedToCamera = false;
309+
this.cameraOffset = new Phaser.Point;
310+
this.world = new Phaser.Point;
309311

310312
/**
311313
* You can crop the Sprites texture by modifying the crop properties. For example crop.width = 50 would set the Sprite to only render 50px wide.
@@ -364,17 +366,19 @@ Phaser.Sprite.prototype.preUpdate = function() {
364366
this.renderOrderID = this.game.world.currentRenderOrderID++;
365367
}
366368

369+
this.prevX = this.x;
370+
this.prevY = this.y;
371+
367372
if (this.fixedToCamera)
368373
{
369-
this.prevX = this.game.camera.view.x + this.x;
370-
this.prevY = this.game.camera.view.y + this.y;
371-
}
372-
else
373-
{
374-
this.prevX = this.x;
375-
this.prevY = this.y;
374+
this.x = this.game.camera.view.x + this.cameraOffset.x;
375+
this.y = this.game.camera.view.y + this.cameraOffset.y;
376+
// this.prevX = this.game.camera.view.x + this.x;
377+
// this.prevY = this.game.camera.view.y + this.y;
376378
}
377379

380+
this.world.setTo(this.game.camera.x + this.worldTransform[2], this.game.camera.y + this.worldTransform[5]);
381+
378382
this.updateCache();
379383
this.updateAnimation();
380384

@@ -656,20 +660,19 @@ Phaser.Sprite.prototype.postUpdate = function() {
656660

657661
if (this.fixedToCamera)
658662
{
659-
this._cache.x = this.game.camera.view.x + this.x;
660-
this._cache.y = this.game.camera.view.y + this.y;
663+
this._cache.x = this.game.camera.view.x + this.cameraOffset.x;
664+
this._cache.y = this.game.camera.view.y + this.cameraOffset.y;
661665
}
662666
else
663667
{
664668
this._cache.x = this.x;
665669
this._cache.y = this.y;
666670
}
667671

668-
if (this.position.x != this._cache.x || this.position.y != this._cache.y)
669-
{
670-
this.position.x = this._cache.x;
671-
this.position.y = this._cache.y;
672-
}
672+
this.world.setTo(this.game.camera.x + this.worldTransform[2], this.game.camera.y + this.worldTransform[5]);
673+
674+
this.position.x = this._cache.x;
675+
this.position.y = this._cache.y;
673676
}
674677

675678
}

src/physics/arcade/Body.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,13 +350,14 @@ Phaser.Physics.Arcade.Body.prototype = {
350350

351351
this.embedded = false;
352352

353-
354-
355353
this.screenX = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
356354
this.screenY = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
357355

358-
this.preX = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
359-
this.preY = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
356+
// this.preX = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
357+
// this.preY = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
358+
359+
this.preX = (this.sprite.world.x - (this.sprite.anchor.x * this.width)) + this.offset.x;
360+
this.preY = (this.sprite.world.y - (this.sprite.anchor.y * this.height)) + this.offset.y;
360361

361362
this.preRotation = this.sprite.angle;
362363

@@ -513,8 +514,10 @@ Phaser.Physics.Arcade.Body.prototype = {
513514

514515
this.angularVelocity = 0;
515516
this.angularAcceleration = 0;
516-
this.preX = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
517-
this.preY = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
517+
// this.preX = (this.sprite.worldTransform[2] - (this.sprite.anchor.x * this.width)) + this.offset.x;
518+
// this.preY = (this.sprite.worldTransform[5] - (this.sprite.anchor.y * this.height)) + this.offset.y;
519+
this.preX = (this.sprite.world.x - (this.sprite.anchor.x * this.width)) + this.offset.x;
520+
this.preY = (this.sprite.world.y - (this.sprite.anchor.y * this.height)) + this.offset.y;
518521
this.preRotation = this.sprite.angle;
519522

520523
this.x = this.preX;

src/utils/Debug.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -556,15 +556,21 @@ Phaser.Utils.Debug.prototype = {
556556

557557
this.start(x, y, color);
558558

559-
this.line(sprite.name);
559+
if (sprite.name)
560+
{
561+
this.line(sprite.name);
562+
}
563+
560564
this.line('x: ' + sprite.x);
561565
this.line('y: ' + sprite.y);
562566
this.line('pos x: ' + sprite.position.x);
563567
this.line('pos y: ' + sprite.position.y);
564568
this.line('local x: ' + sprite.localTransform[2]);
565569
this.line('local y: ' + sprite.localTransform[5]);
566-
this.line('world x: ' + sprite.worldTransform[2]);
567-
this.line('world y: ' + sprite.worldTransform[5]);
570+
this.line('t x: ' + sprite.worldTransform[2]);
571+
this.line('t y: ' + sprite.worldTransform[5]);
572+
this.line('world x: ' + sprite.world.x);
573+
this.line('world y: ' + sprite.world.y);
568574

569575
this.stop();
570576

0 commit comments

Comments
 (0)