mirror of
https://github.com/photonstorm/phaser
synced 2024-11-26 22:52:14 +00:00
The physics configuration object can now be used to start physics systems specifically.
This commit is contained in:
parent
a6467f9b60
commit
f321cab77e
2 changed files with 138 additions and 1 deletions
108
examples/wip/physics config.js
Normal file
108
examples/wip/physics config.js
Normal file
|
@ -0,0 +1,108 @@
|
|||
// Here we'll use a Physics Configuration object to specify which physics systems should be started with the game.
|
||||
|
||||
var physicsConfig = {
|
||||
|
||||
arcade: true,
|
||||
ninja: false,
|
||||
p2: true
|
||||
|
||||
};
|
||||
|
||||
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render }, false, false, physicsConfig);
|
||||
|
||||
function preload() {
|
||||
|
||||
game.load.image('atari', 'assets/sprites/atari130xe.png');
|
||||
game.load.spritesheet('bullets', 'assets/sprites/balls.png', 17, 17);
|
||||
|
||||
}
|
||||
|
||||
var atari;
|
||||
var balls;
|
||||
|
||||
function create() {
|
||||
|
||||
game.stage.backgroundColor = '#2d2d2d';
|
||||
|
||||
balls = game.add.group();
|
||||
|
||||
balls.createMultiple(250, 'bullets', 0, false);
|
||||
|
||||
atari = game.add.sprite(300, 450, 'atari');
|
||||
|
||||
game.physics.arcade.gravity.y = 400;
|
||||
|
||||
// Enable physics on everything added to the world so far (the true parameter makes it recurse down into children)
|
||||
game.physics.arcade.enable(game.world, true);
|
||||
|
||||
atari.body.gravityScale.y = 0;
|
||||
atari.body.immovable = true;
|
||||
|
||||
cursors = game.input.keyboard.createCursorKeys();
|
||||
|
||||
game.time.events.loop(150, fire, this);
|
||||
|
||||
}
|
||||
|
||||
function fire() {
|
||||
|
||||
var ball = balls.getFirstExists(false);
|
||||
|
||||
if (ball)
|
||||
{
|
||||
ball.frame = game.rnd.integerInRange(0,6);
|
||||
ball.exists = true;
|
||||
ball.reset(game.world.randomX, 0);
|
||||
|
||||
ball.body.bounce.y = 0.8;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function reflect(a, ball) {
|
||||
|
||||
if (ball.y > (atari.y + 5))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
ball.body.velocity.x = atari.body.velocity.x;
|
||||
ball.body.velocity.y *= -(ball.body.bounce.y);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
game.physics.arcade.collide(atari, balls, null, reflect, this);
|
||||
|
||||
atari.body.velocity.x = 0;
|
||||
|
||||
if (cursors.left.isDown)
|
||||
{
|
||||
atari.body.velocity.x = -200;
|
||||
}
|
||||
else if (cursors.right.isDown)
|
||||
{
|
||||
atari.body.velocity.x = 200;
|
||||
}
|
||||
|
||||
balls.forEachAlive(checkBounds, this);
|
||||
|
||||
}
|
||||
|
||||
function checkBounds(ball) {
|
||||
|
||||
if (ball.y > 600)
|
||||
{
|
||||
ball.kill();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function render() {
|
||||
|
||||
}
|
|
@ -20,6 +20,8 @@
|
|||
*/
|
||||
Phaser.Physics = function (game, config) {
|
||||
|
||||
config = config || {};
|
||||
|
||||
/**
|
||||
* @property {Phaser.Game} game - Local reference to game.
|
||||
*/
|
||||
|
@ -33,7 +35,7 @@ Phaser.Physics = function (game, config) {
|
|||
/**
|
||||
* @property {Phaser.Physics.Arcade} arcade - The Arcade Physics system.
|
||||
*/
|
||||
this.arcade = new Phaser.Physics.Arcade(game);
|
||||
this.arcade = null;
|
||||
|
||||
/**
|
||||
* @property {Phaser.Physics.P2} p2 - The P2.JS Physics system.
|
||||
|
@ -55,6 +57,8 @@ Phaser.Physics = function (game, config) {
|
|||
*/
|
||||
this.chipmunk = null;
|
||||
|
||||
this.parseConfig();
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -89,6 +93,31 @@ Phaser.Physics.CHIPMUNK = 5;
|
|||
|
||||
Phaser.Physics.prototype = {
|
||||
|
||||
/**
|
||||
* Parses the Physics Configuration object passed to the Game constructor and starts any physics systems specified within.
|
||||
*
|
||||
* @method Phaser.Physics#parseConfig
|
||||
*/
|
||||
parseConfig: function () {
|
||||
|
||||
if ((!this.config.hasOwnProperty('arcade') || this.config['arcade'] === true) && Phaser.Physics.hasOwnProperty('Arcade'))
|
||||
{
|
||||
// If Arcade isn't specified, we create it automatically if we can
|
||||
this.arcade = new Phaser.Physics.Arcade(this.game);
|
||||
}
|
||||
|
||||
if (this.config.hasOwnProperty('ninja') && this.config['ninja'] === true && Phaser.Physics.hasOwnProperty('Ninja'))
|
||||
{
|
||||
this.ninja = new Phaser.Physics.Ninja(this.game);
|
||||
}
|
||||
|
||||
if (this.config.hasOwnProperty('p2') && this.config['p2'] === true && Phaser.Physics.hasOwnProperty('P2'))
|
||||
{
|
||||
this.p2 = new Phaser.Physics.P2(this.game, this.config);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* This will create an instance of the requested physics simulation.
|
||||
* Phaser.Physics.Arcade is running by default, but all others need activating directly.
|
||||
|
|
Loading…
Reference in a new issue