mirror of
https://github.com/wassname/phaser.git
synced 2026-08-13 12:30:11 +08:00
Sprite vs. Sprite Group Body Tests.
This commit is contained in:
+21
-15
@@ -96,6 +96,11 @@ Phaser.Group = function (game, parent, name, useStage) {
|
||||
*/
|
||||
this.scale = this._container.scale;
|
||||
|
||||
/**
|
||||
* @property {Phaser.Point} pivot - The pivot point of the Group container.
|
||||
*/
|
||||
this.pivot = this._container.pivot;
|
||||
|
||||
/**
|
||||
* The cursor is a simple way to iterate through the objects in a Group using the Group.next and Group.previous functions.
|
||||
* The cursor is set to the first child added to the Group and doesn't change unless you call next, previous or set it directly with Group.cursor.
|
||||
@@ -164,14 +169,14 @@ Phaser.Group.prototype = {
|
||||
{
|
||||
child.group = this;
|
||||
|
||||
this._container.addChild(child);
|
||||
|
||||
child.updateTransform();
|
||||
|
||||
if (child.events)
|
||||
{
|
||||
child.events.onAddedToGroup.dispatch(child, this);
|
||||
}
|
||||
|
||||
this._container.addChild(child);
|
||||
|
||||
child.updateTransform();
|
||||
}
|
||||
|
||||
if (this.cursor === null)
|
||||
@@ -209,14 +214,14 @@ Phaser.Group.prototype = {
|
||||
{
|
||||
child.group = this;
|
||||
|
||||
this._container.addChildAt(child, index);
|
||||
|
||||
child.updateTransform();
|
||||
|
||||
if (child.events)
|
||||
{
|
||||
child.events.onAddedToGroup.dispatch(child, this);
|
||||
}
|
||||
|
||||
this._container.addChildAt(child, index);
|
||||
|
||||
child.updateTransform();
|
||||
}
|
||||
|
||||
if (this.cursor === null)
|
||||
@@ -265,15 +270,15 @@ Phaser.Group.prototype = {
|
||||
child.visible = exists;
|
||||
child.alive = exists;
|
||||
|
||||
this._container.addChild(child);
|
||||
|
||||
child.updateTransform();
|
||||
|
||||
if (child.events)
|
||||
{
|
||||
child.events.onAddedToGroup.dispatch(child, this);
|
||||
}
|
||||
|
||||
this._container.addChild(child);
|
||||
|
||||
child.updateTransform();
|
||||
|
||||
if (this.cursor === null)
|
||||
{
|
||||
this.cursor = child;
|
||||
@@ -307,14 +312,15 @@ Phaser.Group.prototype = {
|
||||
child.visible = exists;
|
||||
child.alive = exists;
|
||||
|
||||
this._container.addChild(child);
|
||||
|
||||
child.updateTransform();
|
||||
|
||||
if (child.events)
|
||||
{
|
||||
child.events.onAddedToGroup.dispatch(child, this);
|
||||
}
|
||||
|
||||
this._container.addChild(child);
|
||||
child.updateTransform();
|
||||
|
||||
if (this.cursor === null)
|
||||
{
|
||||
this.cursor = child;
|
||||
|
||||
@@ -209,6 +209,7 @@ Phaser.Sprite = function (game, x, y, key, frame) {
|
||||
*/
|
||||
this._cache = {
|
||||
|
||||
fresh: true,
|
||||
dirty: false,
|
||||
|
||||
// Transform cache
|
||||
@@ -376,6 +377,8 @@ Phaser.Sprite = function (game, x, y, key, frame) {
|
||||
*/
|
||||
this.debug = false;
|
||||
|
||||
// this.events.onAddedToGroup.add(this.initGroup, this);
|
||||
|
||||
this.updateCache();
|
||||
this.updateBounds();
|
||||
|
||||
@@ -389,6 +392,18 @@ Phaser.Sprite = function (game, x, y, key, frame) {
|
||||
Phaser.Sprite.prototype = Object.create(PIXI.Sprite.prototype);
|
||||
Phaser.Sprite.prototype.constructor = Phaser.Sprite;
|
||||
|
||||
|
||||
/*
|
||||
Phaser.Sprite.prototype.initGroup = function() {
|
||||
|
||||
// this.world.setTo(this.parent.position.x + this.x, this.parent.position.y + this.y);
|
||||
|
||||
// console.log('Sprite initGroup', this.world);
|
||||
console.log('Sprite initGroup', this.group.x, this.group.y);
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Automatically called by World.preUpdate. Handles cache updates, lifespan checks, animation updates and physics updates.
|
||||
*
|
||||
@@ -397,6 +412,34 @@ Phaser.Sprite.prototype.constructor = Phaser.Sprite;
|
||||
*/
|
||||
Phaser.Sprite.prototype.preUpdate = function() {
|
||||
|
||||
if (this._cache.fresh)
|
||||
{
|
||||
this.world.setTo(this.parent.position.x + this.x, this.parent.position.y + this.y);
|
||||
this.worldTransform[2] = this.world.x;
|
||||
this.worldTransform[5] = this.world.y;
|
||||
console.log('Sprite initGroup', this.world);
|
||||
// console.log('Sprite initGroup', this.group.x, this.group.y);
|
||||
// console.log('Sprite initGroup', this.parent.position);
|
||||
this._cache.fresh = false;
|
||||
|
||||
if (this.body)
|
||||
{
|
||||
this.body.x = (this.world.x - (this.anchor.x * this.width)) + this.body.offset.x;
|
||||
this.body.y = (this.world.y - (this.anchor.y * this.height)) + this.body.offset.y;
|
||||
// this.body.preUpdate();
|
||||
this.body.preX = this.body.x;
|
||||
this.body.preY = this.body.y;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.debug)
|
||||
{
|
||||
console.log('Sprite preUpdate', this.parent.worldTransform[2], this.parent.worldTransform[5], 'LT', this.parent.localTransform[2], this.parent.localTransform[5], 'xy', this.parent.position.x, this.parent.position.y);
|
||||
console.log('Sprite preUpdate', this.x, this.y, 'world', this.world.x, this.world.y);
|
||||
}
|
||||
|
||||
if (!this.exists || (this.group && !this.group.exists))
|
||||
{
|
||||
this.renderOrderID = -1;
|
||||
@@ -455,6 +498,11 @@ Phaser.Sprite.prototype.updateCache = function() {
|
||||
this._cache.prevX = this.world.x;
|
||||
this._cache.prevY = this.world.y;
|
||||
|
||||
if (this.debug)
|
||||
{
|
||||
console.log('Sprite updateCache', this._cache.prevX, this._cache.prevY);
|
||||
}
|
||||
|
||||
if (this.fixedToCamera)
|
||||
{
|
||||
this.x = this.game.camera.view.x + this.cameraOffset.x;
|
||||
@@ -677,10 +725,19 @@ Phaser.Sprite.prototype.postUpdate = function() {
|
||||
|
||||
if (this.exists)
|
||||
{
|
||||
// The sprite is positioned in this call, after taking into consideration motion updates and collision
|
||||
if (this.body)
|
||||
{
|
||||
this.body.postUpdate();
|
||||
|
||||
console.log('Sprite postUpdate wt', this.worldTransform[2], this.worldTransform[5], 'xy', this.x, this.y);
|
||||
|
||||
// this._cache.x = this.x;
|
||||
// this._cache.y = this.y;
|
||||
|
||||
// this.position.x = this._cache.x;
|
||||
// this.position.y = this._cache.y;
|
||||
|
||||
// this.world.setTo(this.game.camera.x + this.worldTransform[2], this.game.camera.y + this.worldTransform[5]);
|
||||
}
|
||||
|
||||
if (this.fixedToCamera)
|
||||
@@ -698,6 +755,12 @@ Phaser.Sprite.prototype.postUpdate = function() {
|
||||
|
||||
this.position.x = this._cache.x;
|
||||
this.position.y = this._cache.y;
|
||||
|
||||
if (this.debug)
|
||||
{
|
||||
console.log('Sprite postUpdate delta', this.deltaX, this.deltaY, 'prev', this._cache.prevX, this._cache.prevY);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
+54
-19
@@ -477,13 +477,26 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
*/
|
||||
preUpdate: function () {
|
||||
|
||||
// this.preX = this.x;
|
||||
// this.preY = this.y;
|
||||
// this.preRotation = this.sprite.angle;
|
||||
|
||||
// If the GROUP is moving, then this doesn't work!!!
|
||||
this.x = (this.sprite.x - (this.sprite.anchor.x * this.sprite.width)) + this.offset.x;
|
||||
this.y = (this.sprite.y - (this.sprite.anchor.y * this.sprite.height)) + this.offset.y;
|
||||
|
||||
// If the SPRITE is moving, but in a Group, this doesn't work!!!
|
||||
// this.x = (this.sprite.world.x - (this.sprite.anchor.x * this.sprite.width)) + this.offset.x;
|
||||
// this.y = (this.sprite.world.y - (this.sprite.anchor.y * this.sprite.height)) + this.offset.y;
|
||||
|
||||
// I think I need to record both the world AND local X/Y values and decide if they should update?
|
||||
|
||||
this.rotation = this.preRotation;
|
||||
|
||||
this.preX = this.x;
|
||||
this.preY = this.y;
|
||||
this.preRotation = this.sprite.angle;
|
||||
|
||||
this.x = (this.sprite.world.x - (this.sprite.anchor.x * this.sprite.width)) + this.offset.x;
|
||||
this.y = (this.sprite.world.y - (this.sprite.anchor.y * this.sprite.height)) + this.offset.y;
|
||||
this.rotation = this.preRotation;
|
||||
|
||||
if (this.sprite.scale.x !== this._sx || this.sprite.scale.y !== this._sy)
|
||||
{
|
||||
@@ -492,9 +505,15 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
|
||||
if (this.sprite.debug)
|
||||
{
|
||||
console.log('Body preUpdate x:', this.x, 'y:', this.y, 'left:', this.left, 'right:', this.right, 'WAS', this.preX, this.preY);
|
||||
console.log('Body preUpdate blocked:', this.blocked, this.blockFlags);
|
||||
console.log('Body preUpdate velocity:', this.velocity.x, this.velocity.y);
|
||||
console.log('Body preUpdate x:', this.x, 'y:', this.y);
|
||||
console.log('Body preUpdate Sprite x:', this.sprite.x, 'y:', this.sprite.y);
|
||||
console.log('Body preUpdate Sprite world:', this.sprite.world.x, 'y:', this.sprite.world.y);
|
||||
// console.log('Body preUpdate Sprite position:', this.sprite.position.x, 'y:', this.sprite.position.y);
|
||||
// console.log('Body preUpdate Sprite localTransform:', this.sprite.localTransform[2], 'y:', this.sprite.localTransform[5]);
|
||||
// console.log('Body preUpdate Sprite worldTransform:', this.sprite.worldTransform[2], 'y:', this.sprite.worldTransform[5]);
|
||||
// console.log('Body preUpdate x:', this.x, 'y:', this.y, 'left:', this.left, 'right:', this.right, 'WAS', this.preX, this.preY);
|
||||
// console.log('Body preUpdate blocked:', this.blocked, this.blockFlags);
|
||||
// console.log('Body preUpdate velocity:', this.velocity.x, this.velocity.y);
|
||||
// console.log('Body preUpdate rotation:', this.rotation, this.preRotation);
|
||||
}
|
||||
|
||||
@@ -1305,24 +1324,40 @@ if (this.sprite.debug)
|
||||
this.facing = Phaser.DOWN;
|
||||
}
|
||||
|
||||
if (this.sprite.debug)
|
||||
{
|
||||
// console.log('Body postUpdate x:', this.x, 'y:', this.y, 'left:', this.left, 'right:', this.right, 'WAS', this.preX, this.preY);
|
||||
// console.log('Body postUpdate blocked:', this.blocked, this.blockFlags);
|
||||
// console.log('Body postUpdate velocity:', this.velocity.x, this.velocity.y);
|
||||
// console.log('Body postUpdate Sprite:', this.sprite.x, this.sprite.y, 'cached', this.sprite._cache.x, this.sprite._cache.y);
|
||||
console.log('Body postUpdate Rotation:', this.rotation);
|
||||
}
|
||||
|
||||
if (this.deltaX() !== 0 || this.deltaY() !== 0)
|
||||
if (this.sprite.debug)
|
||||
{
|
||||
this.sprite.worldTransform[2] = this.sprite.x = (this.x + (this.sprite.anchor.x * this.sprite.width) - this.offset.x);
|
||||
this.sprite.worldTransform[5] = this.sprite.y = (this.y + (this.sprite.anchor.y * this.sprite.height) - this.offset.y);
|
||||
// Temp. Debugging Stuff
|
||||
// console.log('Body postUpdate x:', this.x, 'y:', this.y, 'left:', this.left, 'right:', this.right, 'WAS', this.preX, this.preY);
|
||||
// console.log('Body postUpdate blocked:', this.blocked, this.blockFlags);
|
||||
// console.log('Body postUpdate velocity:', this.velocity.x, this.velocity.y);
|
||||
// console.log('Body postUpdate delta:', this.deltaX(), this.deltaY());
|
||||
// console.log('Body postUpdate Sprite:', this.sprite.x, this.sprite.y, 'cached', this.sprite._cache.x, this.sprite._cache.y);
|
||||
// console.log('Body postUpdate Rotation:', this.rotation);
|
||||
|
||||
// console.log('Body postUpdate Sprite x:', this.sprite.x, 'y:', this.sprite.y);
|
||||
// console.log('Body postUpdate Sprite world:', this.sprite.world.x, 'y:', this.sprite.world.y);
|
||||
// console.log('Body postUpdate Sprite position:', this.sprite.position.x, 'y:', this.sprite.position.y);
|
||||
// console.log('Body postUpdate Sprite localTransform:', this.sprite.localTransform[2], 'y:', this.sprite.localTransform[5]);
|
||||
// console.log('Body postUpdate Sprite worldTransform:', this.sprite.worldTransform[2], 'y:', this.sprite.worldTransform[5]);
|
||||
|
||||
}
|
||||
|
||||
if (this.preX !== this.x || this.preY !== this.y)
|
||||
{
|
||||
console.log('BoDY APPLIED', this.x, this.y, 'pre', this.preX, this.preY);
|
||||
// this.sprite.x = (this.x + (this.sprite.anchor.x * this.sprite.width) - this.offset.x);
|
||||
// this.sprite.y = (this.y + (this.sprite.anchor.y * this.sprite.height) - this.offset.y);
|
||||
|
||||
this.sprite.x = (this.x + (this.sprite.anchor.x * this.sprite.width) - this.offset.x);
|
||||
this.sprite.y = (this.y + (this.sprite.anchor.y * this.sprite.height) - this.offset.y);
|
||||
|
||||
// this.sprite.position.x = this.sprite.x = this.x + (this.sprite.anchor.x * this.sprite.width) - this.offset.x;
|
||||
// this.sprite.position.y = this.sprite.y = this.y + (this.sprite.anchor.y * this.sprite.height) - this.offset.y;
|
||||
}
|
||||
|
||||
if (this.allowRotation && this.deltaZ() !== 0)
|
||||
{
|
||||
// this.sprite.angle += this.deltaZ();
|
||||
this.sprite.angle += this.deltaZ();
|
||||
}
|
||||
|
||||
if (this.sprite.scale.x !== this._sx || this.sprite.scale.y !== this._sy)
|
||||
|
||||
Reference in New Issue
Block a user