Skip to content

Commit 13a8676

Browse files
committed
Phaser.CANVAS_PX_ROUND is a boolean. If 'true' the Canvas renderer will Math.floor() all coordinates before drawImage, stopping pixel interpolation. Defaults to false.
Phaser.CANVAS_CLEAR_RECT is a boolean. If 'true' (the default) it will context.clearRect() every frame. If false this is skipped (useful if you know you don't need it) Collision now works between Sprites positioned via sprite.x/y, sprite.body.x/y or sprite.body.velocity. If you are tweening a sprite and still want physics collision, set `sprite.body.moves = false` otherwise it will fight against the tween motion.
1 parent 68b7d22 commit 13a8676

6 files changed

Lines changed: 101 additions & 41 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ New features:
112112
* Math.normalizeLatitude - Normalizes a latitude to the [-90,90] range.
113113
* Math.normalizeLongitude - Normalizes a longitude to the [-180,180] range.
114114
* Phaser.Line added to the group of geometry classes, with full point on line/segment and intersection tests (see new examples)
115+
* Phaser.CANVAS_PX_ROUND is a boolean. If 'true' the Canvas renderer will Math.floor() all coordinates before drawImage, stopping pixel interpolation. Defaults to false.
116+
* Phaser.CANVAS_CLEAR_RECT is a boolean. If 'true' (the default) it will context.clearRect() every frame. If false this is skipped (useful if you know you don't need it)
117+
* Collision now works between Sprites positioned via sprite.x/y, sprite.body.x/y or sprite.body.velocity.
118+
* If you are tweening a sprite and still want physics collision, set `sprite.body.moves = false` otherwise it will fight against the tween motion.
119+
115120

116121

117122
New Examples:
@@ -204,6 +209,8 @@ Bug Fixes:
204209
* Removed the frame property from TileSprites as it cannot use them, it tiles the whole image only, not just a section of it.
205210
* Fixed WebGLRenderer updateGraphics bug (thanks theadam)
206211
* Removed duplicate Timer.create line (thanks hstolte)
212+
* Fixed issue with the camera being slightly out of sync with 'fixedToCamera' sprites.
213+
* 1px camera jitter issue fixed where map is same size, or smaller than the game size.
207214

208215

209216
You can view the Change Log for all previous versions at https://github.com/photonstorm/phaser/changelog.md

examples/wip/rabbit map.js

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function preload() {
88
game.load.image('walls_1x2', 'assets/tilemaps/tiles/walls_1x2.png');
99
game.load.image('tiles2', 'assets/tilemaps/tiles/tiles2.png');
1010

11-
// game.load.image('phaser', 'assets/sprites/shinyball.png');
11+
game.load.image('ball', 'assets/sprites/shinyball.png');
1212
game.load.image('phaser', 'assets/sprites/firstaid.png');
1313
// game.load.image('phaser', 'assets/sprites/atari130xe.png');
1414
// game.load.image('phaser', 'assets/sprites/mushroom2.png');
@@ -24,6 +24,7 @@ var layer2;
2424
var layer3;
2525

2626
var sprite;
27+
var ball;
2728
var marker;
2829

2930
function create() {
@@ -34,7 +35,7 @@ console.log(' --- state create start ---');
3435
game.step();
3536
});
3637

37-
// game.stage.backgroundColor = '#124184';
38+
game.stage.backgroundColor = '#124184';
3839

3940

4041
marker = new Phaser.Line(256, 0, 256, 600);
@@ -56,7 +57,12 @@ console.log(' --- state create start ---');
5657
// game.physics.gravity.y = 200;
5758

5859

59-
sprite = game.add.sprite(100, 300, 'phaser'); // up test
60+
sprite = game.add.sprite(100, 180, 'phaser');
61+
sprite.body.moves = false;
62+
63+
ball = game.add.sprite(200, 180, 'ball');
64+
65+
game.add.tween(sprite).to({x: 500},5000,Phaser.Easing.Linear.None,true);
6066

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

@@ -112,12 +118,14 @@ function update() {
112118
layer.scrollY += 4;
113119
}
114120
*/
121+
115122
game.physics.collide(sprite, layer);
123+
game.physics.collide(sprite, ball);
116124

117125
// sprite.body.velocity.y = -300;
118126

119-
sprite.body.velocity.x = 0;
120-
sprite.body.velocity.y = 0;
127+
// sprite.body.velocity.x = 0;
128+
// sprite.body.velocity.y = 0;
121129
// sprite.body.angularVelocity = 0;
122130

123131
// sprite.body.acceleration.x = 0;
@@ -157,25 +165,62 @@ function update() {
157165
sprite.angle = sprite.angle + 1;
158166
*/
159167

168+
// if (cursors.up.isDown)
169+
// {
170+
// sprite.body.velocity.y = -200;
171+
// }
172+
// else if (cursors.down.isDown)
173+
// {
174+
// sprite.body.velocity.y = 100;
175+
// }
176+
177+
// if (cursors.left.isDown)
178+
// {
179+
// sprite.body.velocity.x = -200;
180+
// }
181+
// else if (cursors.right.isDown)
182+
// {
183+
// sprite.body.velocity.x = 200;
184+
// }
185+
186+
// if (cursors.up.isDown)
187+
// {
188+
// sprite.y -= 2;
189+
// }
190+
// else if (cursors.down.isDown)
191+
// {
192+
// sprite.y += 2;
193+
// }
194+
195+
// if (cursors.left.isDown)
196+
// {
197+
// sprite.x -= 2;
198+
// }
199+
// else if (cursors.right.isDown)
200+
// {
201+
// sprite.x += 2;
202+
// }
203+
160204
if (cursors.up.isDown)
161205
{
162-
// console.log('cursor up');
163-
sprite.body.velocity.y = -200;
206+
sprite.body.y -= 2;
164207
}
165208
else if (cursors.down.isDown)
166209
{
167-
sprite.body.velocity.y = 100;
210+
sprite.body.y += 2;
168211
}
169212

170213
if (cursors.left.isDown)
171214
{
172-
sprite.body.velocity.x = -200;
215+
sprite.body.x -= 2;
173216
}
174217
else if (cursors.right.isDown)
175218
{
176-
sprite.body.velocity.x = 200;
219+
sprite.body.x += 2;
177220
}
178221

222+
223+
179224
}
180225

181226
function render() {

src/Phaser.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ var Phaser = Phaser || {
3939
LEFT: 1,
4040
RIGHT: 2,
4141
UP: 3,
42-
DOWN: 4
42+
DOWN: 4,
43+
44+
CANVAS_PX_ROUND: false,
45+
CANVAS_CLEAR_RECT: true
4346

4447
};
4548

src/PixiPatch.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* 1) Added support for Trimmed sprite sheets
55
* 2) Skip display objects with an alpha of zero
66
* 3) Avoid Style Recalculation from the incorrect bgcolor value
7+
* 4) Added support for Canvas unit rounding via Phaser.CANVAS_PX_ROUND boolean (disabled by default).
78
*
89
* Hopefully we can remove this once Pixi has been updated to support these things.
910
*/
@@ -23,7 +24,12 @@ PIXI.CanvasRenderer.prototype.render = function(stage)
2324
stage.updateTransform();
2425

2526
this.context.setTransform(1, 0, 0, 1, 0, 0);
26-
this.context.clearRect(0, 0, this.width, this.height)
27+
28+
if (Phaser.CANVAS_CLEAR_RECT)
29+
{
30+
this.context.clearRect(0, 0, this.width, this.height)
31+
}
32+
2733
this.renderDisplayObject(stage, false);
2834

2935
// Remove frame updates
@@ -44,8 +50,6 @@ PIXI.CanvasRenderer.prototype.renderDisplayObject = function(displayObject, rend
4450

4551
do
4652
{
47-
//transform = displayObject.worldTransform;
48-
4953
if (!displayObject.visible && !renderHidden)
5054
{
5155
displayObject = displayObject.last._iNext;
@@ -60,27 +64,30 @@ PIXI.CanvasRenderer.prototype.renderDisplayObject = function(displayObject, rend
6064

6165
if (displayObject instanceof PIXI.Sprite)
6266
{
63-
// var frame = displayObject.texture.frame;
64-
6567
if (displayObject.texture.frame)
6668
{
6769
this.context.globalAlpha = displayObject.worldAlpha;
6870

69-
// this.context.setTransform(
70-
// displayObject.worldTransform[0],
71-
// displayObject.worldTransform[3],
72-
// displayObject.worldTransform[1],
73-
// displayObject.worldTransform[4],
74-
// displayObject.worldTransform[2],
75-
// displayObject.worldTransform[5]);
76-
77-
this.context.setTransform(
78-
displayObject.worldTransform[0],
79-
displayObject.worldTransform[3],
80-
displayObject.worldTransform[1],
81-
displayObject.worldTransform[4],
82-
Math.floor(displayObject.worldTransform[2]),
83-
Math.floor(displayObject.worldTransform[5]));
71+
if (Phaser.CANVAS_PX_ROUND)
72+
{
73+
this.context.setTransform(
74+
displayObject.worldTransform[0],
75+
displayObject.worldTransform[3],
76+
displayObject.worldTransform[1],
77+
displayObject.worldTransform[4],
78+
Math.floor(displayObject.worldTransform[2]),
79+
Math.floor(displayObject.worldTransform[5]));
80+
}
81+
else
82+
{
83+
this.context.setTransform(
84+
displayObject.worldTransform[0],
85+
displayObject.worldTransform[3],
86+
displayObject.worldTransform[1],
87+
displayObject.worldTransform[4],
88+
displayObject.worldTransform[2],
89+
displayObject.worldTransform[5]);
90+
}
8491

8592
if (displayObject.texture.trimmed)
8693
{

src/physics/arcade/Body.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,19 +1273,19 @@ if (this.sprite.debug)
12731273
this.facing = Phaser.DOWN;
12741274
}
12751275

1276-
// this.updateBounds();
1277-
12781276
if (this.sprite.debug)
12791277
{
12801278
console.log('Body postUpdate x:', this.x, 'y:', this.y, 'left:', this.left, 'right:', this.right, 'WAS', this.preX, this.preY);
12811279
console.log('Body postUpdate blocked:', this.blocked, this.blockFlags);
12821280
console.log('Body postUpdate velocity:', this.velocity.x, this.velocity.y);
1281+
console.log('Body postUpdate Sprite:', this.sprite.x, this.sprite.y, 'cached', this.sprite._cache.x, this.sprite._cache.y);
12831282
}
12841283

1285-
this.sprite.x = this.x - this.offset.x;
1286-
this.sprite.y = this.y - this.offset.y;
1287-
this.sprite.worldTransform[2] = this.x - this.offset.x;
1288-
this.sprite.worldTransform[5] = this.y - this.offset.y;
1284+
if (this.deltaX() !== 0 || this.deltaY() !== 0)
1285+
{
1286+
this.sprite.worldTransform[2] = this.sprite.x = (this.x - this.offset.x);
1287+
this.sprite.worldTransform[5] = this.sprite.y = (this.y - this.offset.y);
1288+
}
12891289

12901290
if (this.allowRotation)
12911291
{
@@ -1319,7 +1319,7 @@ if (this.sprite.debug)
13191319
this.mass = 1;
13201320
this.friction = 0.1;
13211321
this.checkCollision = { none: false, any: true, up: true, down: true, left: true, right: true };
1322-
this.blocked
1322+
this.blocked = { x: 0, y: 0, up: false, down: false, left: false, right: false };
13231323

13241324
},
13251325

src/tilemap/TilemapLayer.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -734,8 +734,6 @@ Phaser.TilemapLayer.prototype.render = function () {
734734
this._ty = this._dy;
735735

736736
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
737-
// this.context.fillStyle = '#ff00ff';
738-
// this.context.fillRect(0, 0, this.canvas.width, this.canvas.height);
739737

740738
this.context.fillStyle = this.tileColor;
741739

@@ -802,7 +800,7 @@ Phaser.TilemapLayer.prototype.render = function () {
802800

803801
if (tile.debug)
804802
{
805-
this.context.fillStyle = 'rgba(0,255,0,0.5)';
803+
this.context.fillStyle = 'rgba(0, 255, 0, 0.4)';
806804
this.context.fillRect(Math.floor(this._tx), Math.floor(this._ty), this.map.tileWidth, this.map.tileHeight);
807805
}
808806
}

0 commit comments

Comments
 (0)