Skip to content

Commit 3b2573d

Browse files
committed
Objects that are 'fixedToCamera' are now still correctly placed even if the camera is scaled (phaserjs#512)
1 parent a6b05f4 commit 3b2573d

9 files changed

Lines changed: 110 additions & 10 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ Bug Fixes:
217217
* Tilemap.putTile now correctly re-calculates the collision data based on the new collideIndexes array (fixes #371)
218218
* Circle.circumferencePoint using the asDegrees parameter would apply degToRad instead of radToDeg (thanks Ziriax, fixes #509)
219219
* InputHandler.enableSnap now correctly assigns the snap offset parameters (fixes #515)
220+
* Objects that are 'fixedToCamera' are now still correctly placed even if the camera is scaled (#512)
220221

221222

222223
TO DO:

examples/wip/fixed to cam scale.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
3+
4+
function preload() {
5+
6+
game.load.image('backdrop', 'assets/pics/remember-me.jpg');
7+
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
8+
game.load.image('coke', 'assets/sprites/cokecan.png');
9+
game.load.bitmapFont('desyrel', 'assets/fonts/bitmapFonts/desyrel.png', 'assets/fonts/bitmapFonts/desyrel.xml', null, 0, -32);
10+
11+
}
12+
13+
var cursors;
14+
var mushroom;
15+
16+
function create() {
17+
18+
game.world.setBounds(0, 0, 1920, 1200);
19+
game.add.image(0, 0, 'backdrop');
20+
21+
mushroom = game.add.sprite(400, 400, 'mushroom');
22+
23+
// Test Fixing an Image to the Camera
24+
var fixie = game.add.image(100, 100, 'coke');
25+
fixie.fixedToCamera = true;
26+
27+
// And tween it
28+
game.add.tween(fixie.cameraOffset).to({ y: 500 }, 2000, Phaser.Easing.Bounce.Out, true, 0, 1000, true);
29+
30+
// Test Fixing a Sprite to the Camera
31+
var fixie2 = game.add.sprite(600, 100, 'coke');
32+
fixie2.fixedToCamera = true;
33+
fixie2.inputEnabled = true;
34+
fixie2.events.onInputDown.add(clicked, this);
35+
36+
// Test Fixing a Text to the Camera
37+
var text = game.add.text(300, 32, '-phaser-');
38+
text.fixedToCamera = true;
39+
40+
// Test fixing a BitmapText to the Camera
41+
var text2 = game.add.bitmapText(200, 500, 'desyrel', 'camera fixies', 32);
42+
text2.fixedToCamera = true;
43+
44+
// Test fixing a Graphics object to the Camera
45+
var graphics = game.add.graphics(0, 0);
46+
graphics.fixedToCamera = true;
47+
graphics.beginFill(0xFF3300);
48+
graphics.lineStyle(2, 0x0000FF, 1);
49+
graphics.drawRect(50, 250, 100, 100);
50+
51+
// Button! do mouse events still work then?
52+
53+
game.camera.scale.set(2);
54+
55+
game.camera.follow(mushroom);
56+
57+
cursors = game.input.keyboard.createCursorKeys();
58+
59+
}
60+
61+
function clicked() {
62+
63+
console.log('boom');
64+
65+
}
66+
67+
function update() {
68+
69+
if (cursors.left.isDown)
70+
{
71+
mushroom.x -= 8;
72+
}
73+
else if (cursors.right.isDown)
74+
{
75+
mushroom.x += 8;
76+
}
77+
78+
if (cursors.up.isDown)
79+
{
80+
mushroom.y -= 8;
81+
}
82+
else if (cursors.down.isDown)
83+
{
84+
mushroom.y += 8;
85+
}
86+
87+
}
88+
89+
function render() {
90+
91+
92+
}

src/core/Camera.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ Phaser.Camera = function (game, id, x, y, width, height) {
9090
* @property {PIXI.DisplayObject} displayObject - The display object to which all game objects are added. Set by World.boot
9191
*/
9292
this.displayObject = null;
93+
94+
/**
95+
* @property {Phaser.Point} scale - The scale of the display object to which all game objects are added. Set by World.boot
96+
*/
97+
this.scale = null;
9398

9499
};
95100

src/core/World.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ Phaser.World.prototype.boot = function () {
5151

5252
this.camera.displayObject = this;
5353

54+
this.camera.scale = this.scale;
55+
5456
this.game.camera = this.camera;
5557

5658
this.game.stage.addChild(this);

src/gameobjects/BitmapText.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ Phaser.BitmapText.prototype.postUpdate = function () {
192192
// Fixed to Camera?
193193
if (this._cache[7] === 1)
194194
{
195-
this.position.x = this.game.camera.view.x + this.cameraOffset.x;
196-
this.position.y = this.game.camera.view.y + this.cameraOffset.y;
195+
this.position.x = (this.game.camera.view.x + this.cameraOffset.x) / this.game.camera.scale.x;
196+
this.position.y = (this.game.camera.view.y + this.cameraOffset.y) / this.game.camera.scale.y;
197197
}
198198

199199
}

src/gameobjects/Graphics.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ Phaser.Graphics.prototype.postUpdate = function () {
128128
// Fixed to Camera?
129129
if (this._cache[7] === 1)
130130
{
131-
this.position.x = this.game.camera.view.x + this.cameraOffset.x;
132-
this.position.y = this.game.camera.view.y + this.cameraOffset.y;
131+
this.position.x = (this.game.camera.view.x + this.cameraOffset.x) / this.game.camera.scale.x;
132+
this.position.y = (this.game.camera.view.y + this.cameraOffset.y) / this.game.camera.scale.y;
133133
}
134134

135135
}

src/gameobjects/Image.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ Phaser.Image.prototype.postUpdate = function() {
187187
// Fixed to Camera?
188188
if (this._cache[7] === 1)
189189
{
190-
this.position.x = this.game.camera.view.x + this.cameraOffset.x;
191-
this.position.y = this.game.camera.view.y + this.cameraOffset.y;
190+
this.position.x = (this.game.camera.view.x + this.cameraOffset.x) / this.game.camera.scale.x;
191+
this.position.y = (this.game.camera.view.y + this.cameraOffset.y) / this.game.camera.scale.y;
192192
}
193193

194194
// Update any Children

src/gameobjects/Sprite.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,8 @@ Phaser.Sprite.prototype.postUpdate = function() {
308308
// Fixed to Camera?
309309
if (this._cache[7] === 1)
310310
{
311-
this.position.x = this.game.camera.view.x + this.cameraOffset.x;
312-
this.position.y = this.game.camera.view.y + this.cameraOffset.y;
311+
this.position.x = (this.game.camera.view.x + this.cameraOffset.x) / this.game.camera.scale.x;
312+
this.position.y = (this.game.camera.view.y + this.cameraOffset.y) / this.game.camera.scale.y;
313313
}
314314

315315
// Update any Children

src/gameobjects/Text.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ Phaser.Text.prototype.postUpdate = function () {
179179

180180
if (this._cache[7] === 1)
181181
{
182-
this.position.x = this.game.camera.view.x + this.cameraOffset.x;
183-
this.position.y = this.game.camera.view.y + this.cameraOffset.y;
182+
this.position.x = (this.game.camera.view.x + this.cameraOffset.x) / this.game.camera.scale.x;
183+
this.position.y = (this.game.camera.view.y + this.cameraOffset.y) / this.game.camera.scale.y;
184184
}
185185

186186
// Update any Children

0 commit comments

Comments
 (0)