Skip to content

Commit 68b7d22

Browse files
committed
Fixed issue with the camera being slightly out of sync with 'fixedToCamera' sprites. Also fixed 'jitter' issue with camera targets.
1 parent 6e4e99f commit 68b7d22

7 files changed

Lines changed: 119 additions & 78 deletions

File tree

examples/wip/rabbit map.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ console.log(' --- state create start ---');
3434
game.step();
3535
});
3636

37+
// game.stage.backgroundColor = '#124184';
38+
39+
3740
marker = new Phaser.Line(256, 0, 256, 600);
3841

3942
map = game.add.tilemap('map');
@@ -48,22 +51,21 @@ console.log(' --- state create start ---');
4851

4952
// layer.debug = true;
5053

51-
// layer.resizeWorld();
54+
layer.resizeWorld();
5255

53-
game.physics.gravity.y = 200;
56+
// game.physics.gravity.y = 200;
5457

5558

5659
sprite = game.add.sprite(100, 300, 'phaser'); // up test
5760

5861
// sprite = game.add.sprite(200, 240, 'phaser'); // 3-block corner test
5962

60-
sprite.debug = true;
61-
62-
game.stepping = true;
63+
// sprite.debug = true;
64+
// game.stepping = true;
6365

6466
// sprite.body.velocity.y = -300;
6567

66-
sprite.body.velocity.y = 200;
68+
// sprite.body.velocity.y = 200;
6769

6870

6971
// sprite.anchor.setTo(0.5, 0.5);
@@ -79,7 +81,7 @@ console.log(' --- state create start ---');
7981

8082
// sprite.angle = 35;
8183

82-
// game.camera.follow(sprite);
84+
game.camera.follow(sprite);
8385

8486
// game.input.onDown.add(getIt, this);
8587

@@ -89,7 +91,7 @@ console.log(' --- state create start ---');
8991

9092
function update() {
9193

92-
console.log(' --- state update start ---');
94+
// console.log(' --- state update start ---');
9395

9496
/*
9597
if (cursors.left.isDown)
@@ -114,8 +116,8 @@ console.log(' --- state update start ---');
114116

115117
// sprite.body.velocity.y = -300;
116118

117-
// sprite.body.velocity.x = 0;
118-
// sprite.body.velocity.y = 0;
119+
sprite.body.velocity.x = 0;
120+
sprite.body.velocity.y = 0;
119121
// sprite.body.angularVelocity = 0;
120122

121123
// sprite.body.acceleration.x = 0;
@@ -157,8 +159,8 @@ console.log(' --- state update start ---');
157159

158160
if (cursors.up.isDown)
159161
{
160-
console.log('cursor up');
161-
sprite.body.velocity.y = -300;
162+
// console.log('cursor up');
163+
sprite.body.velocity.y = -200;
162164
}
163165
else if (cursors.down.isDown)
164166
{
@@ -167,14 +169,13 @@ console.log(' --- state update start ---');
167169

168170
if (cursors.left.isDown)
169171
{
170-
sprite.body.velocity.x = -100;
172+
sprite.body.velocity.x = -200;
171173
}
172174
else if (cursors.right.isDown)
173175
{
174-
sprite.body.velocity.x = 100;
176+
sprite.body.velocity.x = 200;
175177
}
176178

177-
178179
}
179180

180181
function render() {

src/core/Camera.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Phaser.Camera = function (game, id, x, y, width, height) {
3939
* Camera view.
4040
* The view into the world we wish to render (by default the game dimensions).
4141
* The x/y values are in world coordinates, not screen coordinates, the width/height is how many pixels to render.
42-
* Objects outside of this view are not rendered (unless set to ignore the Camera, i.e. UI?).
42+
* Objects outside of this view are not rendered if set to camera cull.
4343
* @property {Phaser.Rectangle} view
4444
*/
4545
this.view = new Phaser.Rectangle(x, y, width, height);
@@ -86,6 +86,9 @@ Phaser.Camera = function (game, id, x, y, width, height) {
8686
*/
8787
this._edge = 0;
8888

89+
/**
90+
* @property {PIXI.DisplayObject} displayObject - The display object to which all game objects are added. Set by World.boot
91+
*/
8992
this.displayObject = null;
9093

9194
};
@@ -203,35 +206,36 @@ Phaser.Camera.prototype = {
203206

204207
},
205208

209+
/**
210+
* Internal method
211+
* @method Phaser.Camera#updateTarget
212+
* @private
213+
*/
206214
updateTarget: function () {
207215

208216
if (this.deadzone)
209217
{
210-
// this._edge = this.target.bounds.x - this.deadzone.x;
211218
this._edge = this.target.x - this.deadzone.x;
212219

213220
if (this.view.x > this._edge)
214221
{
215222
this.view.x = this._edge;
216223
}
217224

218-
// this._edge = this.target.bounds.right - this.deadzone.x - this.deadzone.width;
219225
this._edge = this.target.x + this.target.width - this.deadzone.x - this.deadzone.width;
220226

221227
if (this.view.x < this._edge)
222228
{
223229
this.view.x = this._edge;
224230
}
225231

226-
// this._edge = this.target.bounds.y - this.deadzone.y;
227232
this._edge = this.target.y - this.deadzone.y;
228233

229234
if (this.view.y > this._edge)
230235
{
231236
this.view.y = this._edge;
232237
}
233238

234-
// this._edge = this.target.bounds.bottom - this.deadzone.y - this.deadzone.height;
235239
this._edge = this.target.y + this.target.height - this.deadzone.y - this.deadzone.height;
236240

237241
if (this.view.y < this._edge)
@@ -246,6 +250,10 @@ Phaser.Camera.prototype = {
246250

247251
},
248252

253+
/**
254+
* Update the Camera bounds to match the game world.
255+
* @method Phaser.Camera#setBoundsToWorld
256+
*/
249257
setBoundsToWorld: function () {
250258

251259
this.bounds.setTo(this.game.world.bounds.x, this.game.world.bounds.y, this.game.world.bounds.width, this.game.world.bounds.height);
@@ -268,10 +276,10 @@ Phaser.Camera.prototype = {
268276
this.view.x = this.bounds.x;
269277
}
270278

271-
if (this.view.x > this.bounds.right - this.width)
279+
if (this.view.right > this.bounds.right)
272280
{
273281
this.atLimit.x = true;
274-
this.view.x = (this.bounds.right - this.width) + 1;
282+
this.view.x = this.bounds.right - this.width;
275283
}
276284

277285
if (this.view.y < this.bounds.top)
@@ -280,10 +288,10 @@ Phaser.Camera.prototype = {
280288
this.view.y = this.bounds.top;
281289
}
282290

283-
if (this.view.y > this.bounds.bottom - this.height)
291+
if (this.view.bottom > this.bounds.bottom)
284292
{
285293
this.atLimit.y = true;
286-
this.view.y = (this.bounds.bottom - this.height) + 1;
294+
this.view.y = this.bounds.bottom - this.height;
287295
}
288296

289297
this.view.floor();

src/core/Game.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ Phaser.Game.prototype = {
592592
}
593593

594594
this.plugins.preUpdate();
595-
console.log('world preUpdate');
595+
// console.log('world preUpdate');
596596
this.world.preUpdate();
597597

598598
this.stage.update();
@@ -606,7 +606,7 @@ Phaser.Game.prototype = {
606606
this.particles.update();
607607
this.plugins.update();
608608

609-
console.log('world postUpdate');
609+
// console.log('world postUpdate');
610610
this.world.postUpdate();
611611
this.plugins.postUpdate();
612612
}

src/core/World.js

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ Phaser.World.prototype.boot = function () {
6161
}
6262

6363
/**
64-
* This is called automatically every frame, and is where main logic happens.
64+
* This is called automatically after the plugins preUpdate and before the State.update.
65+
* Most objects have preUpdate methods and it's where initial movement, drawing and calculations are done.
6566
*
6667
* @method Phaser.World#update
6768
*/
@@ -90,7 +91,8 @@ Phaser.World.prototype.preUpdate = function () {
9091
}
9192

9293
/**
93-
* This is called automatically every frame, and is where main logic happens.
94+
* This is called automatically after the State.update, but before particles or plugins update.
95+
* Most objects won't have an update method set unless explicitly given one.
9496
*
9597
* @method Phaser.World#update
9698
*/
@@ -121,31 +123,58 @@ Phaser.World.prototype.update = function () {
121123
}
122124

123125
/**
124-
* This is called automatically every frame, and is where main logic happens.
126+
* This is called automatically before the renderer runs and after the plugins have updated.
127+
* In postUpdate this is where all the final physics calculatations and object positioning happens.
128+
* The objects are processed in the order of the display list.
129+
* The only exception to this is if the camera is following an object, in which case that is updated first.
130+
*
125131
* @method Phaser.World#postUpdate
126132
*/
127133
Phaser.World.prototype.postUpdate = function () {
128134

129-
this.camera.update();
130-
131-
if (this.game.stage._stage.first._iNext)
135+
if (this.camera.target && this.camera.target['postUpdate'])
132136
{
133-
var currentNode = this.game.stage._stage.first._iNext;
134-
135-
do
137+
this.camera.target.postUpdate();
138+
139+
this.camera.update();
140+
141+
if (this.game.stage._stage.first._iNext)
136142
{
137-
if (currentNode['postUpdate'])
143+
var currentNode = this.game.stage._stage.first._iNext;
144+
145+
do
138146
{
139-
currentNode.postUpdate();
147+
if (currentNode['postUpdate'] && currentNode !== this.camera.target)
148+
{
149+
currentNode.postUpdate();
150+
}
151+
152+
currentNode = currentNode._iNext;
140153
}
154+
while (currentNode != this.game.stage._stage.last._iNext)
155+
}
156+
}
157+
else
158+
{
159+
this.camera.update();
160+
161+
if (this.game.stage._stage.first._iNext)
162+
{
163+
var currentNode = this.game.stage._stage.first._iNext;
141164

142-
currentNode = currentNode._iNext;
165+
do
166+
{
167+
if (currentNode['postUpdate'])
168+
{
169+
currentNode.postUpdate();
170+
}
171+
172+
currentNode = currentNode._iNext;
173+
}
174+
while (currentNode != this.game.stage._stage.last._iNext)
143175
}
144-
while (currentNode != this.game.stage._stage.last._iNext)
145176
}
146177

147-
this.camera.update();
148-
149178
}
150179

151180
/**
@@ -154,8 +183,8 @@ Phaser.World.prototype.postUpdate = function () {
154183
* @method Phaser.World#setBounds
155184
* @param {number} x - Top left most corner of the world.
156185
* @param {number} y - Top left most corner of the world.
157-
* @param {number} width - New width of the world.
158-
* @param {number} height - New height of the world.
186+
* @param {number} width - New width of the world. Can never be smaller than the Game.width.
187+
* @param {number} height - New height of the world. Can never be smaller than the Game.height.
159188
*/
160189
Phaser.World.prototype.setBounds = function (x, y, width, height) {
161190

0 commit comments

Comments
 (0)