Get First Dead example.

This commit is contained in:
photonstorm 2014-03-13 10:14:06 +00:00
parent 8318a58f69
commit aa6b1821bd
5 changed files with 123 additions and 40 deletions

View file

@ -0,0 +1,49 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.spritesheet('veg', 'assets/sprites/fruitnveg32wh37.png', 32, 32);
}
var veg;
function create() {
// Create a group
veg = game.add.group();
// Add 20 sprites to it - the 'false' parameter sets them all to dead
veg.createMultiple(20, 'veg', 0, false);
// Set-up a simple repeating timer
game.time.events.repeat(Phaser.Timer.SECOND, 20, resurrect, this);
}
function resurrect() {
// Get a dead item
var item = veg.getFirstDead();
if (item)
{
// And bring it back to life
item.reset(game.world.randomX, game.world.randomY);
// This just changes its frame
item.frame = game.rnd.integerInRange(0, 36);
}
}
function update() {
}
function render() {
game.debug.text('One item will be resurrected every second', 32, 32);
game.debug.text('Living: ' + veg.countLiving() + ' Dead: ' + veg.countDead(), 32, 64);
}

View file

@ -4,11 +4,13 @@ var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload:
function preload() {
game.load.image('atari', 'assets/sprites/atari130xe.png');
game.load.image('sky', 'assets/skies/sunset.png');
game.load.image('ball', 'assets/sprites/red_ball.png');
game.load.image('sky', 'assets/skies/cavern2.png');
}
var sprite;
var sprite1;
var sprite2;
var cursors;
function create() {
@ -18,22 +20,15 @@ function create() {
// Enable p2 physics
game.physics.startSystem(Phaser.Physics.P2JS);
// Add a sprite
sprite = game.add.sprite(200, 200, 'atari');
// Add 2 sprites which we'll join with a spring
sprite1 = game.add.sprite(200, 100, 'ball');
sprite2 = game.add.sprite(200, 300, 'atari');
// Enable if for physics. This creates a default rectangular body.
game.physics.p2.enable(sprite);
game.physics.p2.enable([sprite1, sprite2]);
// Create our spring
var spring = game.physics.p2.createSpring(bodyA, bodyB, restLength, stiffness, damping, worldA, worldB, localA, localB);
// var spring = game.physics.p2.
// var spring = new p2.Spring(bodyA,bodyB, {
// stiffness: k,
// restLength: l,
// damping : d
// });
// var spring = game.physics.p2.createSpring(sprite1, sprite2, restLength, stiffness, damping, worldA, worldB, localA, localB);
// var spring = game.physics.p2.createSpring(sprite1, sprite2);

View file

@ -274,6 +274,22 @@ Phaser.Group.prototype.create = function (x, y, key, frame, exists) {
var child = new Phaser.Sprite(this.game, x, y, key, frame);
if (this.enableBody)
{
if (this.physicsBodyType === Phaser.Physics.ARCADE)
{
this.game.physics.arcade.enable(child);
}
else if (this.physicsBodyType === Phaser.Physics.NINJA && this.game.physics.ninja)
{
this.game.physics.ninja.enable(child);
}
else if (this.physicsBodyType === Phaser.Physics.P2JS && this.game.physics.p2)
{
this.game.physics.p2.enable(child, this.enableBodyDebug);
}
}
child.exists = exists;
child.visible = exists;
child.alive = exists;
@ -292,22 +308,6 @@ Phaser.Group.prototype.create = function (x, y, key, frame, exists) {
this.cursor = child;
}
if (this.enableBody)
{
if (this.physicsBodyType === Phaser.Physics.ARCADE)
{
this.game.physics.arcade.enable(child);
}
else if (this.physicsBodyType === Phaser.Physics.NINJA && this.game.physics.ninja)
{
this.game.physics.ninja.enable(child);
}
else if (this.physicsBodyType === Phaser.Physics.P2JS && this.game.physics.p2)
{
this.game.physics.p2.enable(child, this.enableBodyDebug);
}
}
return child;
}

View file

@ -5,14 +5,14 @@
*/
/**
* Creates a spring, connecting two bodies.
* Creates a spring, connecting two bodies. A spring can have a resting length, a stiffness and damping.
*
* @class Phaser.Physics.P2.Spring
* @classdesc Physics Spring Constructor
* @constructor
* @param {Phaser.Physics.P2} world - A reference to the P2 World.
* @param {Phaser.Physics.P2.Body} bodyA - First connected body.
* @param {Phaser.Physics.P2.Body} bodyB - Second connected body.
* @param {p2.Body} bodyA - First connected body.
* @param {p2.Body} bodyB - Second connected body.
* @param {number} [restLength=1] - Rest length of the spring. A number > 0.
* @param {number} [stiffness=100] - Stiffness of the spring. A number >= 0.
* @param {number} [damping=1] - Damping of the spring. A number >= 0.
@ -63,7 +63,7 @@ Phaser.Physics.P2.Spring = function (world, bodyA, bodyB, restLength, stiffness,
options.localAnchorB = [ world.pxm(localB[0]), world.pxm(localB[1]) ];
}
p2.Spring.call(this, bodyA.data, bodyB.data, options);
p2.Spring.call(this, bodyA, bodyB, options);
}

View file

@ -843,7 +843,7 @@ Phaser.Physics.P2.prototype = {
},
/**
* Populates and returns an array of all current Bodies in the world.
* Populates and returns an array with references to of all current Bodies in the world.
*
* @method Phaser.Physics.P2#getBodies
* @return {array<Phaser.Physics.P2.Body>} An array containing all current Bodies in the world.
@ -862,6 +862,35 @@ Phaser.Physics.P2.prototype = {
},
/**
* Checks the given object to see if it has a p2.Body and if so returns it.
*
* @method Phaser.Physics.P2#getBody
* @param {object} object - The object to check for a p2.Body on.
* @return {p2.Body} The p2.Body, or null if not found.
*/
getBody: function (object) {
if (object['world'])
{
// Native p2 body
return object;
}
else if (object instanceof Phaser.Physics.P2.Body)
{
// Phaser P2 Body
return object.data;
}
else if (object['body'] && object['body'].type === Phaser.Physics.P2JS)
{
// Sprite, TileSprite, etc
return object.body.data;
}
return null;
},
/**
* Populates and returns an array of all current Springs in the world.
*
@ -1031,11 +1060,11 @@ Phaser.Physics.P2.prototype = {
},
/**
* Creates a spring, connecting two bodies.
* Creates a spring, connecting two bodies. A spring can have a resting length, a stiffness and damping.
*
* @method Phaser.Physics.P2#createSpring
* @param {Phaser.Physics.P2.Body} bodyA - First connected body.
* @param {Phaser.Physics.P2.Body} bodyB - Second connected body.
* @param {Phaser.Sprite|Phaser.Physics.P2.Body|p2.Body} bodyA - First connected body.
* @param {Phaser.Sprite|Phaser.Physics.P2.Body|p2.Body} bodyB - Second connected body.
* @param {number} [restLength=1] - Rest length of the spring. A number > 0.
* @param {number} [stiffness=100] - Stiffness of the spring. A number >= 0.
* @param {number} [damping=1] - Damping of the spring. A number >= 0.
@ -1050,7 +1079,17 @@ Phaser.Physics.P2.prototype = {
*/
createSpring: function (bodyA, bodyB, restLength, stiffness, damping, worldA, worldB, localA, localB) {
return new Phaser.Physics.P2.Spring(this, bodyA, bodyB, restLength, stiffness, damping, worldA, worldB, localA, localB);
bodyA = this.getBody(bodyA);
bodyB = this.getBody(bodyB);
if (!bodyA || !bodyB)
{
console.warn('Cannot create Spring, invalid body objects given');
}
else
{
return new Phaser.Physics.P2.Spring(this, bodyA, bodyB, restLength, stiffness, damping, worldA, worldB, localA, localB);
}
},