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
+66
View File
@@ -89,6 +89,26 @@ Phaser.Group = function (game, parent, name, addToStage) {
this._cursorIndex = 0;
/**
* @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();
/**
* 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.Group.prototype = Object.create(PIXI.DisplayObjectContainer.prototype);
@@ -708,11 +728,19 @@ Phaser.Group.prototype.callAll = function (method, context) {
*/
Phaser.Group.prototype.preUpdate = function () {
if (!this.exists || !this.parent.exists)
{
this.renderOrderID = -1;
return false;
}
for (var i = this.children.length - 1; i >= 0; i--)
{
this.children[i].preUpdate();
}
return true;
}
/**
@@ -736,6 +764,13 @@ Phaser.Group.prototype.update = function () {
*/
Phaser.Group.prototype.postUpdate = function () {
// Fixed to Camera?
if (this._cache[7] === 1)
{
this.x = this.game.camera.view.x + this.cameraOffset.x;
this.y = this.game.camera.view.y + this.cameraOffset.y;
}
for (var i = this.children.length - 1; i >= 0; i--)
{
this.children[i].postUpdate();
@@ -1177,6 +1212,37 @@ Object.defineProperty(Phaser.Group.prototype, "angle", {
});
/**
* 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.
* Note that the cameraOffset values are in addition to any parent in the display list.
* So if this Group was in a Group that has x: 200, then this will be added to the cameraOffset.x
*
* @name Phaser.Group#fixedToCamera
* @property {boolean} fixedToCamera - Set to true to fix this Group to the Camera at its current world coordinates.
*/
Object.defineProperty(Phaser.Group.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;
}
}
});
// Documentation stubs
/**
+4 -4
View File
@@ -82,7 +82,7 @@ Phaser.Stage.prototype.preUpdate = function () {
var i = this.children.length;
while(i--)
while (i--)
{
this.children[i].preUpdate();
}
@@ -98,7 +98,7 @@ Phaser.Stage.prototype.update = function () {
var i = this.children.length;
while(i--)
while (i--)
{
this.children[i].update();
}
@@ -123,7 +123,7 @@ Phaser.Stage.prototype.postUpdate = function () {
var i = this.children.length;
while(i--)
while (i--)
{
if (this.children[i] !== this.game.world.camera.target)
{
@@ -137,7 +137,7 @@ Phaser.Stage.prototype.postUpdate = function () {
var i = this.children.length;
while(i--)
while (i--)
{
this.children[i].postUpdate();
}
+75
View File
@@ -95,6 +95,81 @@ Phaser.World.prototype.setBounds = function (x, y, width, height) {
}
/**
* This is called automatically after the plugins preUpdate and before the State.update.
* Most objects have preUpdate methods and it's where initial movement and positioning is done.
*
* @method Phaser.World#preUpdate
*/
Phaser.World.prototype.preUpdate = function () {
this.currentRenderOrderID = 0;
var i = this.children.length;
while (i--)
{
this.children[i].preUpdate();
}
}
/**
* This is called automatically after the State.update, but before particles or plugins update.
*
* @method Phaser.World#update
*/
Phaser.World.prototype.update = function () {
var i = this.children.length;
while (i--)
{
this.children[i].update();
}
}
/**
* This is called automatically before the renderer runs and after the plugins have updated.
* In postUpdate this is where all the final physics calculatations and object positioning happens.
* The objects are processed in the order of the display list.
* The only exception to this is if the camera is following an object, in which case that is updated first.
*
* @method Phaser.World#postUpdate
*/
Phaser.World.prototype.postUpdate = function () {
if (this.game.world.camera.target)
{
this.game.world.camera.target.postUpdate();
this.game.world.camera.update();
var i = this.children.length;
while (i--)
{
if (this.children[i] !== this.game.world.camera.target)
{
this.children[i].postUpdate();
}
}
}
else
{
this.game.world.camera.update();
var i = this.children.length;
while (i--)
{
this.children[i].postUpdate();
}
}
}
/**
* Destroyer of worlds.
* @method Phaser.World#destroy