Fixed the Sprite / World / Group body issue. So invaders now works again, as do the new examples.

This commit is contained in:
photonstorm
2014-02-03 03:02:05 +00:00
parent 3488880956
commit 28f71ed86d
4 changed files with 61 additions and 39 deletions
+2
View File
@@ -300,5 +300,7 @@ function render() {
{
game.debug.renderPhysicsBody(aliens._container.children[i].body);
}
game.debug.renderPhysicsBody(player.body);
}
+10 -5
View File
@@ -114,7 +114,9 @@ function createAliens () {
}
*/
bob = aliens.create(48, 50, 'invader');
bob = game.add.sprite(32, 32, 'invader');
// bob = aliens.create(48, 50, 'invader');
bob.debug = true;
@@ -145,8 +147,9 @@ console.log('descend');
function update() {
aliens.x += 1;
// bob.body.velocity.x = 100;
// aliens.x += 1;
bob.body.velocity.x = 100;
// bob.body.velocity.y = 100;
// Scroll the background
// starfield.tilePosition.y += 2;
@@ -325,7 +328,9 @@ function render() {
for (var i = 0; i < aliens._container.children.length; i++)
{
game.debug.renderPhysicsBody(aliens._container.children[i].body);
// game.debug.renderPhysicsBody(aliens._container.children[i].body);
}
game.debug.renderPhysicsBody(bob.body);
}
+2 -2
View File
@@ -729,7 +729,7 @@ Phaser.Sprite.prototype.postUpdate = function() {
{
this.body.postUpdate();
console.log('Sprite postUpdate wt', this.worldTransform[2], this.worldTransform[5], 'xy', this.x, this.y);
// 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;
@@ -751,7 +751,7 @@ Phaser.Sprite.prototype.postUpdate = function() {
this._cache.y = this.y;
}
this.world.setTo(this.game.camera.x + this.worldTransform[2], this.game.camera.y + this.worldTransform[5]);
// this.world.setTo(this.game.camera.x + this.worldTransform[2], this.game.camera.y + this.worldTransform[5]);
this.position.x = this._cache.x;
this.position.y = this._cache.y;
+47 -32
View File
@@ -477,27 +477,34 @@ 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;
// 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.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 covers any motion that happens during this frame, no since the last frame
this.preX = this.x;
this.preY = this.y;
this.preRotation = this.sprite.angle;
// I think I need to record both the world AND local X/Y values and decide if they should update?
// Velocity will adjust a sprites local x/y, but it may still be being updated by a group x/y too, so don't over-write both!
// Equally don't use world x/y changes as delta, but DO use them for precise positioning of the Body.
this.rotation = this.preRotation;
// this.preX = this.x;
// this.preY = this.y;
// this.preRotation = this.sprite.angle;
if (this.sprite.scale.x !== this._sx || this.sprite.scale.y !== this._sy)
{
this.updateScale();
@@ -1334,26 +1341,32 @@ if (this.sprite.debug)
// 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 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)
if (this.deltaX() !== 0 || this.deltaY() !== 0)
{
console.log('BoDY APPLIED', this.x, this.y, 'pre', this.preX, this.preY);
// console.log('BoDY APPLIED', this.x, this.y, 'pre', this.preX, this.preY, 'delta', this.deltaX(), this.deltaY());
this.sprite.x += this.deltaX();
this.sprite.y += this.deltaY();
}
// 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.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)
{
@@ -1400,6 +1413,8 @@ if (this.sprite.debug)
this.blocked = { x: 0, y: 0, up: false, down: false, left: false, right: false };
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.preX = this.x;
this.preY = this.y;
this.updateBounds();
},
@@ -1412,14 +1427,14 @@ if (this.sprite.debug)
*/
deltaX: function () {
if (this.moves)
{
// if (this.moves)
// {
return this.x - this.preX;
}
else
{
return this.sprite.deltaX;
}
// }
// else
// {
// return this.sprite.deltaX;
// }
},
@@ -1431,14 +1446,14 @@ if (this.sprite.debug)
*/
deltaY: function () {
if (this.moves)
{
// if (this.moves)
// {
return this.y - this.preY;
}
else
{
return this.sprite.deltaY;
}
// }
// else
// {
// return this.sprite.deltaY;
// }
},