Skip to content

Commit

Permalink
Particles back in and working again. Also updated most of the example…
Browse files Browse the repository at this point in the history
…s and put Body.gravity back in as well.
  • Loading branch information
photonstorm committed Mar 11, 2014
1 parent f321cab commit 384451b
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 14 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Significant API changes:

New features:

* Phaser.Image is a brand new display object perfect for logos, backgrounds, etc. You can scale, rotate, tint, blend an get input events from an Image, but it has no animation, physics body.
* Phaser.Image is a brand new display object perfect for logos, backgrounds, etc. You can scale, rotate, tint, blend an get input events from an Image, but it has no animation or physics body.
* You can now use the hitArea property on Sprites and Image objects. hitArea can be a geometry object (Rectangle, Circle, Polygon, Ellipse) and is used in pointerOver checks.
* InputManager.getLocalPosition(displayObject, pointer, output) will return the local coordinates of the specified displayObject and pointer.
* InputManager.hitTest will test for pointer hits against a Sprite/Image, its hitArea (if set) or any of its children.
Expand Down Expand Up @@ -178,6 +178,7 @@ Updates:
* Animation.stop has a new parameter: dispatchComplete. If true it'll dispatch an Animation.onComplete event.
* TileSprites now have a physics body property and call it in the pre and post updates. As with all physics bodies it's null by default.
* json is now the default tilemap format when not defined (thanks RyanDansie, #528)
* The Particle Emitter now remembers the frames given to it and resets it when a new particle is emitted.


Bug Fixes:
Expand Down
7 changes: 5 additions & 2 deletions examples/particles/collision.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@

var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update });
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update });

var emitter;

function preload() {

game.load.image('sky', 'assets/skies/sky4.png');
game.load.spritesheet('veggies', 'assets/sprites/fruitnveg32wh37.png', 32, 32);

}

function create() {

game.add.image(0, 0, 'sky');

emitter = game.add.emitter(game.world.centerX, game.world.centerY, 250);

emitter.makeParticles('veggies', [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20], 200, true, true);
Expand All @@ -27,6 +30,6 @@ function create() {

function update() {

game.physics.collide(emitter);
game.physics.arcade.collide(emitter);

}
3 changes: 3 additions & 0 deletions examples/particles/rain.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: p

function preload() {

game.load.image('sky', 'assets/skies/underwater3.png');
game.load.spritesheet('rain', 'assets/sprites/rain.png', 17, 17);

}

function create() {

game.add.image(0, 0, 'sky');

var emitter = game.add.emitter(game.world.centerX, 0, 400);
emitter.width = game.world.width;
// emitter.angle = 30; // uncomment to set an angle for the rain.
Expand Down
5 changes: 3 additions & 2 deletions examples/particles/snow.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: p

function preload() {

game.load.image('sky', 'assets/skies/sky3.png');
game.load.spritesheet('snowflakes', 'assets/sprites/snowflakes.png', 17, 17);
game.load.spritesheet('snowflakes_large', 'assets/sprites/snowflakes_large.png', 64, 64);

Expand All @@ -18,6 +19,8 @@ var i = 0;

function create() {

game.add.image(0, 0, 'sky');

back_emitter = game.add.emitter(game.world.centerX, -32, 600);
back_emitter.makeParticles('snowflakes', [0, 1, 2, 3, 4, 5]);
back_emitter.maxParticleScale = 0.6;
Expand All @@ -28,7 +31,6 @@ function create() {
back_emitter.minRotation = 0;
back_emitter.maxRotation = 40;


mid_emitter = game.add.emitter(game.world.centerX, -32, 250);
mid_emitter.makeParticles('snowflakes', [0, 1, 2, 3, 4, 5]);
mid_emitter.maxParticleScale = 1.2;
Expand All @@ -39,7 +41,6 @@ function create() {
mid_emitter.minRotation = 0;
mid_emitter.maxRotation = 40;


front_emitter = game.add.emitter(game.world.centerX, -32, 50);
front_emitter.makeParticles('snowflakes_large', [0, 1, 2, 3, 4, 5]);
front_emitter.maxParticleScale = 1;
Expand Down
23 changes: 17 additions & 6 deletions examples/particles/when particles collide.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@

var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update });
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update });

var leftEmitter;
var rightEmitter;

function preload() {

game.load.image('ball1', 'assets/sprites/red_ball.png');
game.load.image('ball2', 'assets/sprites/blue_ball.png');
game.load.image('sky', 'assets/skies/cavern2.png');
game.load.spritesheet('balls', 'assets/sprites/balls.png', 17, 17);

}

function create() {

game.add.image(0, 0, 'sky');

leftEmitter = game.add.emitter(50, game.world.centerY - 200);
leftEmitter.bounce.setTo(0.5, 0.5);
leftEmitter.setXSpeed(100, 200);
leftEmitter.setYSpeed(-50, 50);
leftEmitter.makeParticles('ball1', 0, 250, 1, true);
leftEmitter.makeParticles('balls', 0, 250, 1, true);

rightEmitter = game.add.emitter(game.world.width - 50, game.world.centerY - 200);
rightEmitter.bounce.setTo(0.5, 0.5);
rightEmitter.setXSpeed(-100, -200);
rightEmitter.setYSpeed(-50, 50);
rightEmitter.makeParticles('ball2', 0, 250, 1, true);
rightEmitter.makeParticles('balls', 1, 250, 1, true);

// explode, lifespan, frequency, quantity
leftEmitter.start(false, 5000, 20);
Expand All @@ -32,5 +34,14 @@ function create() {
}

function update() {
game.physics.collide(leftEmitter, rightEmitter);

game.physics.arcade.collide(leftEmitter, rightEmitter, change, null, this);

}

function change(a, b) {

a.frame = 3;
b.frame = 3;

}
17 changes: 17 additions & 0 deletions src/particles/arcade/Emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ Phaser.Particles.Arcade.Emitter = function (game, x, y, maxParticles) {
*/
this._explode = true;

/**
* @property {any} _frames - Internal helper for the particle frame.
* @private
*/
this._frames = null;

/**
* @property {boolean} on - Determines whether the emitter is currently emitting particles. It is totally safe to directly toggle this.
* @default
Expand Down Expand Up @@ -264,6 +270,7 @@ Phaser.Particles.Arcade.Emitter.prototype.makeParticles = function (keys, frames
var i = 0;
var rndKey = keys;
var rndFrame = frames;
this._frames = frames;

while (i < quantity)
{
Expand All @@ -280,6 +287,7 @@ Phaser.Particles.Arcade.Emitter.prototype.makeParticles = function (keys, frames
}

particle = new Phaser.Sprite(this.game, 0, 0, rndKey, rndFrame);
this.game.physics.arcade.enable(particle, false);
}
// else
// {
Expand Down Expand Up @@ -434,6 +442,15 @@ Phaser.Particles.Arcade.Emitter.prototype.emitParticle = function () {
particle.scale.setTo(scale, scale);
}

if (typeof this._frames === 'object')
{
particle.frame = this.game.rnd.pick(this._frames);
}
else
{
particle.frame = this._frames;
}

particle.body.friction = this.particleFriction;
particle.body.angularDrag = this.angularDrag;

Expand Down
13 changes: 12 additions & 1 deletion src/physics/arcade/Body.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,18 @@ Phaser.Physics.Arcade.Body = function (sprite) {
this.drag = new Phaser.Point();

/**
* @property {Phaser.Point} gravityScale - Gravity scaling factor. If you want the body to ignore gravity, set this to zero. If you want to reverse gravity, set it to -1.
* @property {boolean} allowGravity - Allow this Body to be influenced by world gravity?
* @default
*/
this.allowGravity = true;

/**
* @property {Phaser.Point} gravity - A local gravity applied to this Body. If set this over-rides any world gravity.
*/
this.gravity = new Phaser.Point(0, 0);

/**
* @property {Phaser.Point} gravityScale - Gravity scaling factor. This is only applied to world gravity, not local gravity. If you want the body to ignore gravity, set this to zero. If you want to reverse gravity, set it to -1.
*/
this.gravityScale = new Phaser.Point(1, 1);

Expand Down
19 changes: 17 additions & 2 deletions src/physics/arcade/World.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,23 @@ Phaser.Physics.Arcade.prototype = {
body.rotation += (body.angularVelocity * this.game.time.physicsElapsed);

// Apply gravity using the p2 style gravityScale
body.velocity.x += this.gravity.x * this.game.time.physicsElapsed * body.gravityScale.x;
body.velocity.y += this.gravity.y * this.game.time.physicsElapsed * body.gravityScale.y;
if (body.gravity.x !== 0)
{
body.velocity.x += body.gravity.x * this.game.time.physicsElapsed;
}
else
{
body.velocity.x += this.gravity.x * this.game.time.physicsElapsed * body.gravityScale.x;
}

if (body.gravity.y !== 0)
{
body.velocity.y += body.gravity.y * this.game.time.physicsElapsed;
}
else
{
body.velocity.y += this.gravity.y * this.game.time.physicsElapsed * body.gravityScale.y;
}

// Apply velocity
body.velocity.x = this.computeVelocity(body.velocity.x, body.acceleration.x, body.drag.x, body.maxVelocity.x);
Expand Down

0 comments on commit 384451b

Please sign in to comment.