Skip to content

Commit e5e643b

Browse files
committed
fixedToCamera now works across all display objects. When enabled it will fix at its current x/y coordinate, but can be changed via cameraOffset.
fixedToCamrea now works for Groups as well :) You can fix a Group to the camera and it will influence its children. Also fixed the issue with World.preUpdate/postUpdate not being called and various small documentation issues.
1 parent e5a4620 commit e5e643b

13 files changed

Lines changed: 716 additions & 96 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ New features:
115115
* Added Cache.updateFrameData which is really useful for swapping FrameData blocks in the cache.
116116
* Loader.physics now lets you load Lime + Corona JSON Physics data, which can be used with Body.loadPolygon and Body.loadData.
117117
* Cache.addPhysicsData and Cache.getPhysicsData allow you to store parsed JSON physics data in the cache, for sharing between Bodies.
118+
* fixedToCamera now works across all display objects. When enabled it will fix at its current x/y coordinate, but can be changed via cameraOffset.
119+
* fixedToCamrea now works for Groups as well :) You can fix a Group to the camera and it will influence its children.
118120

119121

120122
Updates:

examples/wip/fixed to cam.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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/desyrel.png', 'assets/fonts/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+
// Button! do mouse events still work then?
45+
46+
game.camera.follow(mushroom);
47+
48+
cursors = game.input.keyboard.createCursorKeys();
49+
50+
}
51+
52+
function clicked() {
53+
54+
console.log('boom');
55+
56+
}
57+
58+
function update() {
59+
60+
if (cursors.left.isDown)
61+
{
62+
mushroom.x -= 8;
63+
}
64+
else if (cursors.right.isDown)
65+
{
66+
mushroom.x += 8;
67+
}
68+
69+
if (cursors.up.isDown)
70+
{
71+
mushroom.y -= 8;
72+
}
73+
else if (cursors.down.isDown)
74+
{
75+
mushroom.y += 8;
76+
}
77+
78+
}
79+
80+
function render() {
81+
82+
83+
}

examples/wip/group fixed to cam.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
10+
}
11+
12+
var cursors;
13+
var mushroom;
14+
15+
function create() {
16+
17+
game.world.setBounds(0, 0, 1920, 1200);
18+
game.add.image(0, 0, 'backdrop');
19+
20+
mushroom = game.add.sprite(400, 400, 'mushroom');
21+
22+
var group = game.add.group();
23+
24+
for (var i = 0; i < 20; i++)
25+
{
26+
group.create(Math.random() * 800, Math.random() * 600, 'coke');
27+
}
28+
29+
group.fixedToCamera = true;
30+
31+
game.camera.follow(mushroom);
32+
33+
cursors = game.input.keyboard.createCursorKeys();
34+
35+
}
36+
37+
function clicked() {
38+
39+
console.log('boom');
40+
41+
}
42+
43+
function update() {
44+
45+
if (cursors.left.isDown)
46+
{
47+
mushroom.x -= 8;
48+
}
49+
else if (cursors.right.isDown)
50+
{
51+
mushroom.x += 8;
52+
}
53+
54+
if (cursors.up.isDown)
55+
{
56+
mushroom.y -= 8;
57+
}
58+
else if (cursors.down.isDown)
59+
{
60+
mushroom.y += 8;
61+
}
62+
63+
}
64+
65+
function render() {
66+
67+
68+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.AUTO, '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('starfield', 'assets/misc/starfield.jpg');
9+
10+
}
11+
12+
var sprite;
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+
sprite = game.add.tileSprite(100, 100, 200, 200, 'starfield');
22+
sprite.autoScroll(0, 100);
23+
sprite.fixedToCamera = true;
24+
25+
mushroom = game.add.sprite(400, 400, 'mushroom');
26+
27+
game.camera.follow(mushroom);
28+
29+
cursors = game.input.keyboard.createCursorKeys();
30+
31+
}
32+
33+
function update() {
34+
35+
if (cursors.left.isDown)
36+
{
37+
mushroom.x -= 8;
38+
}
39+
else if (cursors.right.isDown)
40+
{
41+
mushroom.x += 8;
42+
}
43+
44+
if (cursors.up.isDown)
45+
{
46+
mushroom.y -= 8;
47+
}
48+
else if (cursors.down.isDown)
49+
{
50+
mushroom.y += 8;
51+
}
52+
53+
}
54+
55+
function render() {
56+
57+
// game.debug.renderText(sprite.frame, 32, 32);
58+
59+
}

src/core/Group.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,26 @@ Phaser.Group = function (game, parent, name, addToStage) {
8989

9090
this._cursorIndex = 0;
9191

92+
/**
93+
* @property {Phaser.Point} cameraOffset - If this object is fixedToCamera then this stores the x/y offset that its drawn at, from the top-left of the camera view.
94+
*/
95+
this.cameraOffset = new Phaser.Point();
96+
97+
/**
98+
* A small internal cache:
99+
* 0 = previous position.x
100+
* 1 = previous position.y
101+
* 2 = previous rotation
102+
* 3 = renderID
103+
* 4 = fresh? (0 = no, 1 = yes)
104+
* 5 = outOfBoundsFired (0 = no, 1 = yes)
105+
* 6 = exists (0 = no, 1 = yes)
106+
* 7 = fixed to camera (0 = no, 1 = yes)
107+
* @property {Int16Array} _cache
108+
* @private
109+
*/
110+
this._cache = new Int16Array([0, 0, 0, 0, 1, 0, 1, 0]);
111+
92112
};
93113

94114
Phaser.Group.prototype = Object.create(PIXI.DisplayObjectContainer.prototype);
@@ -708,11 +728,19 @@ Phaser.Group.prototype.callAll = function (method, context) {
708728
*/
709729
Phaser.Group.prototype.preUpdate = function () {
710730

731+
if (!this.exists || !this.parent.exists)
732+
{
733+
this.renderOrderID = -1;
734+
return false;
735+
}
736+
711737
for (var i = this.children.length - 1; i >= 0; i--)
712738
{
713739
this.children[i].preUpdate();
714740
}
715741

742+
return true;
743+
716744
}
717745

718746
/**
@@ -736,6 +764,13 @@ Phaser.Group.prototype.update = function () {
736764
*/
737765
Phaser.Group.prototype.postUpdate = function () {
738766

767+
// Fixed to Camera?
768+
if (this._cache[7] === 1)
769+
{
770+
this.x = this.game.camera.view.x + this.cameraOffset.x;
771+
this.y = this.game.camera.view.y + this.cameraOffset.y;
772+
}
773+
739774
for (var i = this.children.length - 1; i >= 0; i--)
740775
{
741776
this.children[i].postUpdate();
@@ -1177,6 +1212,37 @@ Object.defineProperty(Phaser.Group.prototype, "angle", {
11771212

11781213
});
11791214

1215+
/**
1216+
* A Group that is fixed to the camera uses its x/y coordinates as offsets from the top left of the camera. These are stored in Group.cameraOffset.
1217+
* Note that the cameraOffset values are in addition to any parent in the display list.
1218+
* So if this Group was in a Group that has x: 200, then this will be added to the cameraOffset.x
1219+
*
1220+
* @name Phaser.Group#fixedToCamera
1221+
* @property {boolean} fixedToCamera - Set to true to fix this Group to the Camera at its current world coordinates.
1222+
*/
1223+
Object.defineProperty(Phaser.Group.prototype, "fixedToCamera", {
1224+
1225+
get: function () {
1226+
1227+
return !!this._cache[7];
1228+
1229+
},
1230+
1231+
set: function (value) {
1232+
1233+
if (value)
1234+
{
1235+
this._cache[7] = 1;
1236+
this.cameraOffset.set(this.x, this.y);
1237+
}
1238+
else
1239+
{
1240+
this._cache[7] = 0;
1241+
}
1242+
}
1243+
1244+
});
1245+
11801246
// Documentation stubs
11811247

11821248
/**

src/core/Stage.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Phaser.Stage.prototype.preUpdate = function () {
8282

8383
var i = this.children.length;
8484

85-
while(i--)
85+
while (i--)
8686
{
8787
this.children[i].preUpdate();
8888
}
@@ -98,7 +98,7 @@ Phaser.Stage.prototype.update = function () {
9898

9999
var i = this.children.length;
100100

101-
while(i--)
101+
while (i--)
102102
{
103103
this.children[i].update();
104104
}
@@ -123,7 +123,7 @@ Phaser.Stage.prototype.postUpdate = function () {
123123

124124
var i = this.children.length;
125125

126-
while(i--)
126+
while (i--)
127127
{
128128
if (this.children[i] !== this.game.world.camera.target)
129129
{
@@ -137,7 +137,7 @@ Phaser.Stage.prototype.postUpdate = function () {
137137

138138
var i = this.children.length;
139139

140-
while(i--)
140+
while (i--)
141141
{
142142
this.children[i].postUpdate();
143143
}

0 commit comments

Comments
 (0)