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.
This commit is contained in:
photonstorm
2014-02-15 01:27:42 +00:00
parent e5a4620b87
commit e5e643b103
13 changed files with 717 additions and 97 deletions
+75 -15
View File
@@ -59,13 +59,6 @@ Phaser.BitmapText = function (game, x, y, font, text, size) {
*/
this.world = new Phaser.Point(x, y);
/**
* An object that is fixed to the camera ignores the position of any ancestors in the display list and uses its x/y coordinates as offsets from the top left of the camera.
* @property {boolean} fixedToCamera - Fixes this object to the Camera.
* @default
*/
this.fixedToCamera = false;
/**
* @property {string} _text - Internal cache var.
* @private
@@ -106,10 +99,30 @@ Phaser.BitmapText = function (game, x, y, font, text, size) {
*/
this.input = null;
/**
* @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.
*/
this.cameraOffset = new Phaser.Point();
PIXI.BitmapText.call(this, text);
this.position.set(x, y);
/**
* A small internal cache:
* 0 = previous position.x
* 1 = previous position.y
* 2 = previous rotation
* 3 = renderID
* 4 = fresh? (0 = no, 1 = yes)
* 5 = outOfBoundsFired (0 = no, 1 = yes)
* 6 = exists (0 = no, 1 = yes)
* 7 = fixed to camera (0 = no, 1 = yes)
* @property {Int16Array} _cache
* @private
*/
this._cache = new Int16Array([0, 0, 0, 0, 1, 0, 1, 0]);
};
Phaser.BitmapText.prototype = Object.create(PIXI.BitmapText.prototype);
@@ -134,13 +147,30 @@ Phaser.BitmapText.prototype.setStyle = function() {
*/
Phaser.BitmapText.prototype.preUpdate = function () {
this._cache[0] = this.world.x;
this._cache[1] = this.world.y;
this._cache[2] = this.rotation;
if (!this.exists || !this.parent.exists)
{
// Reset the renderOrderID
this.renderOrderID = -1;
return false;
}
this.world.setTo(this.game.camera.x + this.worldTransform.tx, this.game.camera.y + this.worldTransform.ty);
if (this.autoCull)
{
// Won't get rendered but will still get its transform updated
this.renderable = this.game.world.camera.screenView.intersects(this.getBounds());
}
this.world.setTo(this.game.camera.x + this.worldTransform[2], this.game.camera.y + this.worldTransform[5]);
if (this.visible)
{
this._cache[3] = this.game.world.currentRenderOrderID++;
}
return true;
}
@@ -160,14 +190,13 @@ Phaser.BitmapText.prototype.update = function() {
*/
Phaser.BitmapText.prototype.postUpdate = function () {
if (this.exists)
// Fixed to Camera?
if (this._cache[7] === 1)
{
if (this.fixedToCamera)
{
// this.position.x = this.game.camera.view.x + this.x;
// this.position.y = this.game.camera.view.y + this.y;
}
this.position.x = this.game.camera.view.x + this.cameraOffset.x;
this.position.y = this.game.camera.view.y + this.cameraOffset.y;
}
}
/**
@@ -370,3 +399,34 @@ Object.defineProperty(Phaser.BitmapText.prototype, "inputEnabled", {
}
});
/**
* An BitmapText that is fixed to the camera uses its x/y coordinates as offsets from the top left of the camera. These are stored in BitmapText.cameraOffset.
* Note that the cameraOffset values are in addition to any parent in the display list.
* So if this BitmapText was in a Group that has x: 200, then this will be added to the cameraOffset.x
*
* @name Phaser.BitmapText#fixedToCamera
* @property {boolean} fixedToCamera - Set to true to fix this BitmapText to the Camera at its current world coordinates.
*/
Object.defineProperty(Phaser.BitmapText.prototype, "fixedToCamera", {
get: function () {
return !!this._cache[7];
},
set: function (value) {
if (value)
{
this._cache[7] = 1;
this.cameraOffset.set(this.x, this.y);
}
else
{
this._cache[7] = 0;
}
}
});