Lots of examples fixes and put Group.sort back in.

This commit is contained in:
photonstorm 2014-03-10 14:33:18 +00:00
parent c694ec9c7b
commit 9f997daa46
10 changed files with 75 additions and 174 deletions

View file

@ -14,13 +14,17 @@ function create() {
sprite = game.add.sprite(400, 300, 'arrow');
sprite.anchor.setTo(0.5, 0.5);
// Enable Arcade Physics for the sprite
game.physics.enable(sprite, Phaser.Physics.ARCADE);
// Tell it we don't want physics to manage the rotation
sprite.body.allowRotation = false;
}
function update() {
game.physics.arcade.moveToPointer(sprite, game.input.activePointer, 60, 500, 500);
sprite.rotation = game.physics.arcade.moveToPointer(sprite, 60, game.input.activePointer, 500);
}

View file

@ -24,24 +24,27 @@ function create() {
// This will check Group vs. Group collision (bullets vs. veggies!)
veggies = game.add.group();
veggies.enableBody = true;
veggies.physicsBodyType = Phaser.Physics.ARCADE;
for (var i = 0; i < 50; i++)
{
var c = veggies.create(game.world.randomX, Math.random() * 500, 'veggies', game.rnd.integerInRange(0, 36));
c.name = 'veg' + i;
game.physics.enable(c, Phaser.Physics.ARCADE);
c.body.immovable = true;
}
bullets = game.add.group();
bullets.enableBody = true;
bullets.physicsBodyType = Phaser.Physics.ARCADE;
for (var i = 0; i < 10; i++)
for (var i = 0; i < 20; i++)
{
var b = bullets.create(0, 0, 'bullet');
b.name = 'bullet' + i;
game.physics.enable(b, Phaser.Physics.ARCADE);
b.exists = false;
b.visible = false;
b.checkWorldBounds = true;
b.events.onOutOfBounds.add(resetBullet, this);
}

View file

@ -1,52 +0,0 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('atari', 'assets/sprites/atari130xe.png');
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
}
var sprite1;
var sprite2;
function create() {
game.stage.backgroundColor = '#2d2d2d';
sprite1 = game.add.sprite(130, 200, 'atari');
sprite1.name = 'atari';
// Here we're rotated both the sprite and the physics body
sprite1.rotation = 0.6;
sprite1.body.polygon.rotate(0.6);
sprite1.body.immovable = true;
sprite2 = game.add.sprite(700, 210, 'mushroom');
sprite2.name = 'mushroom';
sprite2.body.velocity.x = -100;
}
function update() {
game.physics.collide(sprite1, sprite2, collisionHandler, null, this);
}
function collisionHandler (obj1, obj2) {
game.stage.backgroundColor = '#992d2d';
}
function render() {
game.debug.bodyInfo(sprite1, 32, 32);
game.debug.physicsBody(sprite1.body);
game.debug.physicsBody(sprite2.body);
}

View file

@ -18,23 +18,19 @@ function create() {
game.stage.backgroundColor = '#124184';
bmd = game.add.bitmapData(800, 600);
bmd.fillStyle('#ffffff');
var bg = game.add.sprite(0, 0, bmd);
bg.body.moves = false;
bmd.context.fillStyle = '#ffffff';
// game.enableStep();
var bg = game.add.sprite(0, 0, bmd);
game.physics.arcade.gravity.y = 100;
sprite = game.add.sprite(32, 450, 'arrow');
sprite.anchor.setTo(0.5, 0.5);
sprite.anchor.set(0.5);
game.physics.enable(sprite, Phaser.Physics.ARCADE);
sprite.body.collideWorldBounds = true;
sprite.body.bounce.setTo(0.8, 0.8);
sprite.body.linearDamping = 0.1;
sprite.body.bounce.set(0.8);
game.input.onDown.add(launch, this);
@ -55,9 +51,9 @@ function launch() {
function update() {
sprite.rotation = sprite.body.angle;
// sprite.rotation = sprite.body.angle;
bmd.context.fillStyle('#ffff00');
bmd.context.fillStyle = '#ffff00';
bmd.context.fillRect(sprite.x, sprite.y, 2, 2);
}

View file

@ -19,21 +19,17 @@ function create() {
game.stage.backgroundColor = '#313131';
bullets = game.add.group();
bullets.enableBody = true;
bullets.physicsBodyType = Phaser.Physics.ARCADE;
bullets.createMultiple(50, 'bullet');
bullets.setAll('anchor.x', 0.5);
bullets.setAll('anchor.y', 0.5);
bullets.setAll('checkWorldBounds', true);
bullets.setAll('outOfBoundsKill', true);
sprite = game.add.sprite(400, 300, 'arrow');
sprite.anchor.setTo(0.5, 0.5);
sprite.anchor.set(0.5);
game.physics.enable(sprite, Phaser.Physics.ARCADE);
bullets.forEach(function (sprite) {
game.physics.enable(sprite, Phaser.Physics.ARCADE);
sprite.body.outOfBoundsKill = true;
});
sprite.body.allowRotation = false;
}
@ -56,9 +52,9 @@ function fire() {
var bullet = bullets.getFirstDead();
bullet.reset(sprite.x, sprite.y);
bullet.reset(sprite.x - 8, sprite.y - 8);
bullet.rotation = game.physics.arcade.moveToPointer(bullet, 300);
game.physics.arcade.moveToPointer(bullet, 300);
}
}

View file

@ -1,42 +0,0 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('fuji', 'assets/pics/atari_fujilogo.png');
}
var fuji;
var b;
function create() {
game.stage.backgroundColor = 'rgb(0,0,100)';
fuji = game.add.sprite(game.world.centerX, game.world.centerY, 'fuji');
fuji.anchor.setTo(0, 0.5);
// fuji.angle = 34;
b = new Phaser.Rectangle(fuji.width/2, fuji.height/2, fuji.width, fuji.height);
//Remember that the sprite is rotating around its anchor
game.add.tween(fuji).to({ angle: 360 }, 20000, Phaser.Easing.Linear.None, true, 0, true);
}
function update() {
if (game.input.activePointer.justPressed())
{
fuji.position = game.input;
}
b.x = fuji.width/2 - fuji.width/2;
b.y = fuji.height/2 - fuji.height/2;
}
function render() {
game.debug.geom(b, 'rgba(0,20,91,1)');
}

View file

@ -16,21 +16,21 @@ function create() {
game.stage.backgroundColor = '#2d2d2d';
game.physics.enable(game.world, Phaser.Physics.ARCADE);
// This example will check Sprite vs. Group collision
sprite = game.add.sprite(32, 200, 'phaser');
sprite.name = 'phaser-dude';
group = game.add.group();
game.physics.enable(game.world, Phaser.Physics.ARCADE);
group.enableBody = true;
group.physicsBodyType = Phaser.Physics.ARCADE;
for (var i = 0; i < 50; i++)
{
var c = group.create(game.rnd.integerInRange(100, 770), game.rnd.integerInRange(0, 570), 'veggies', game.rnd.integerInRange(0, 36));
c.name = 'veg' + i;
game.physics.enable(c, Phaser.Physics.ARCADE);
c.body.immovable = true;
}
@ -39,7 +39,6 @@ function create() {
// Here we'll create some chillis which the player can pick-up. They are still part of the same Group.
var c = group.create(game.rnd.integerInRange(100, 770), game.rnd.integerInRange(0, 570), 'veggies', 17);
c.name = 'chilli' + i;
game.physics.enable(c, Phaser.Physics.ARCADE);
c.body.immovable = true;
}

View file

@ -100,6 +100,12 @@ Phaser.Group = function (game, parent, name, addToStage) {
*/
this.physicsBodyType = Phaser.Physics.ARCADE;
/**
* @property {string} _sortProperty - The property on which children are sorted.
* @private
*/
this._sortProperty = 'y';
/**
* A small internal cache:
* 0 = previous position.x
@ -111,10 +117,11 @@ Phaser.Group = function (game, parent, name, addToStage) {
* 6 = exists (0 = no, 1 = yes)
* 7 = fixed to camera (0 = no, 1 = yes)
* 8 = cursor index
* 9 = sort order
* @property {Int16Array} _cache
* @private
*/
this._cache = new Int16Array([0, 0, 0, 0, 1, 0, 1, 0, 0]);
this._cache = new Int16Array([0, 0, 0, 0, 1, 0, 1, 0, 0, 0]);
};
@ -1013,11 +1020,36 @@ Phaser.Group.prototype.sort = function (index, order) {
if (typeof index === 'undefined') { index = 'y'; }
if (typeof order === 'undefined') { order = Phaser.Group.SORT_ASCENDING; }
this._sortProperty = index;
this._cache[9] = order;
this.children.sort(this.sortHandler);
}
/**
* An internal helper function for the sort process.
*
* @method Phaser.Group#sortHandler
* @param {object} a - The first object being sorted.
* @param {object} b - The second object being sorted.
*/
Phaser.Group.prototype.sortHandler = function (a, b) {
if (a[this._sortProperty] && b[this._sortProperty])
{
if (a[this._sortProperty] < b[this._sortProperty])
{
return this._cache[9];
}
else if (a[this._sortProperty] > b[this._sortProperty])
{
return -this._cache[9];
}
}
return 0;
}
/**

View file

@ -347,7 +347,7 @@ Phaser.Physics.Arcade.Body.prototype = {
// this is where the Sprite currently is, in world coordinates
// this.preX = (this.sprite.world.x - (this.sprite.anchor.x * this.width)) + this.offset.x;
// this.preY = (this.sprite.world.y - (this.sprite.anchor.y * this.height)) + this.offset.y;
// this.preRotation = this.sprite.angle;
this.preRotation = this.sprite.angle;
// this.x = this.preX;
// this.y = this.preY;
@ -409,66 +409,33 @@ Phaser.Physics.Arcade.Body.prototype = {
// this.position.add(this.newVelocity.x, this.newVelocity.y);
this.sprite.x = (this.position.x - this.offset.x);
this.sprite.y = (this.position.y - this.offset.y);
/* if (this.deltaX() < 0)
if (this.deltaX() < 0)
{
this.facing = Phaser.LEFT;
this.sprite.x += this.deltaX();
}
else if (this.deltaX() > 0)
{
this.facing = Phaser.RIGHT;
this.sprite.x += this.deltaX();
}
if (this.deltaY() < 0)
{
this.facing = Phaser.UP;
this.sprite.y += this.deltaY();
}
else if (this.deltaY() > 0)
{
this.facing = Phaser.DOWN;
this.sprite.y += this.deltaY();
}
*/
// this.sprite.x += this.overlapX;
// this.sprite.y += this.overlapY;
// this is where the Sprite currently is, in world coordinates
// this.sprite.x = (this.sprite.world.x - (this.sprite.anchor.x * this.width)) + this.offset.x;
// this.sprite.y = (this.sprite.world.y - (this.sprite.anchor.y * this.height)) + this.offset.y;
/*
if (this.deltaX() < 0 && this.blocked.left === false)
if (this.deltaX() !== 0 || this.deltaY() !== 0)
{
this.facing = Phaser.LEFT;
this.sprite.x += this.deltaX();
// this is where the Sprite currently is, in world coordinates
// this.sprite.x = (this.sprite.world.x - (this.sprite.anchor.x * this.width)) + this.offset.x;
// this.sprite.y = (this.sprite.world.y - (this.sprite.anchor.y * this.height)) + this.offset.y;
this.sprite.x = (this.position.x - this.offset.x);
this.sprite.y = (this.position.y - this.offset.y);
this.center.setTo(this.x + this.halfWidth, this.y + this.halfHeight);
}
else if (this.deltaX() > 0 && this.blocked.right === false)
{
this.facing = Phaser.RIGHT;
this.sprite.x += this.deltaX();
}
if (this.deltaY() < 0 && this.blocked.up === false)
{
this.facing = Phaser.UP;
this.sprite.y += this.deltaY();
}
else if (this.deltaY() > 0 && this.blocked.down === false)
{
this.facing = Phaser.DOWN;
this.sprite.y += this.deltaY();
}
*/
this.center.setTo(this.x + this.halfWidth, this.y + this.halfHeight);
if (this.allowRotation)
{

View file

@ -226,11 +226,9 @@ Phaser.Physics.Arcade.prototype = {
*/
updateMotion: function (body) {
// Rotation
this._velocityDelta = (this.computeVelocity(body.angularVelocity, body.angularAcceleration, body.angularDrag, body.maxAngular, 0) - body.angularVelocity) * 0.5;
this._velocityDelta = this.computeVelocity(body.angularVelocity, body.angularAcceleration, body.angularDrag, body.maxAngular) - body.angularVelocity;
body.angularVelocity += this._velocityDelta;
body.rotation += (body.angularVelocity * this.game.time.physicsElapsed);
body.angularVelocity += this._velocityDelta;
// Apply gravity using the p2 style gravityScale
body.velocity.x += this.gravity.x * this.game.time.physicsElapsed * body.gravityScale.x;
@ -259,7 +257,7 @@ Phaser.Physics.Arcade.prototype = {
if (acceleration)
{
velocity + acceleration * this.game.time.physicsElapsed;
velocity += acceleration * this.game.time.physicsElapsed;
}
else if (drag)
{
@ -1433,7 +1431,7 @@ Phaser.Physics.Arcade.prototype = {
if (typeof maxTime === 'undefined') { maxTime = 0; }
this._angle = this.angleToPointer(displayObject, pointer);
if (maxTime > 0)
{
// We know how many pixels we need to move, but how fast?