mirror of
https://github.com/photonstorm/phaser
synced 2024-11-23 13:13:43 +00:00
P2 Body Begin and End Contact events done and working nicely.
This commit is contained in:
parent
d2762719b8
commit
fc788f909c
5 changed files with 165 additions and 21 deletions
|
@ -411,6 +411,7 @@ Beyond version 2.2
|
|||
* Multiple Camera support.
|
||||
* DragonBones support.
|
||||
* Cache to localStorage using If-Modified-Since. [See github request](https://github.com/photonstorm/phaser/issues/495)
|
||||
* Allow for complex assets like Bitmap Fonts to be stored within a texture atlas.
|
||||
|
||||
|
||||
Nadion
|
||||
|
|
105
examples/p2 physics/contact events.js
Normal file
105
examples/p2 physics/contact events.js
Normal file
|
@ -0,0 +1,105 @@
|
|||
|
||||
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
|
||||
|
||||
function preload() {
|
||||
|
||||
game.load.image('contra2', 'assets/pics/contra2.png');
|
||||
game.load.image('block', 'assets/sprites/block.png');
|
||||
game.load.image('wizball', 'assets/sprites/wizball.png');
|
||||
game.load.image('tetrisblock1', 'assets/sprites/tetrisblock1.png');
|
||||
game.load.image('tetrisblock2', 'assets/sprites/tetrisblock2.png');
|
||||
game.load.image('tetrisblock3', 'assets/sprites/tetrisblock3.png');
|
||||
|
||||
game.load.physics('physicsData', 'assets/physics/sprites.json');
|
||||
|
||||
}
|
||||
|
||||
var contra;
|
||||
var block;
|
||||
var wizball;
|
||||
var tetris1;
|
||||
var tetris2;
|
||||
var tetris3;
|
||||
|
||||
var cursors;
|
||||
|
||||
var result = 'Move with the cursors';
|
||||
|
||||
function create() {
|
||||
|
||||
// Enable p2 physics
|
||||
game.physics.startSystem(Phaser.Physics.P2JS);
|
||||
|
||||
contra = game.add.sprite(200, 200, 'contra2');
|
||||
block = game.add.sprite(500, 200, 'block');
|
||||
wizball = game.add.sprite(500, 500, 'wizball');
|
||||
tetris1 = game.add.sprite(100, 450, 'tetrisblock1');
|
||||
tetris2 = game.add.sprite(300, 450, 'tetrisblock2');
|
||||
tetris3 = game.add.sprite(650, 350, 'tetrisblock3');
|
||||
|
||||
// Enable the physics bodies on all the sprites
|
||||
game.physics.p2.enable([ contra, block, wizball, tetris1, tetris2, tetris3 ], false);
|
||||
|
||||
// The following just loads the polygon data into the objects
|
||||
contra.body.clearShapes();
|
||||
contra.body.loadPolygon('physicsData', 'contra2');
|
||||
|
||||
wizball.body.setCircle(45);
|
||||
|
||||
tetris1.body.clearShapes();
|
||||
tetris1.body.loadPolygon('physicsData', 'tetrisblock1');
|
||||
|
||||
tetris2.body.clearShapes();
|
||||
tetris2.body.loadPolygon('physicsData', 'tetrisblock2');
|
||||
|
||||
tetris3.body.clearShapes();
|
||||
tetris3.body.loadPolygon('physicsData', 'tetrisblock3');
|
||||
|
||||
cursors = game.input.keyboard.createCursorKeys();
|
||||
|
||||
// Check for the block hitting another object
|
||||
block.body.onBeginContact.add(blockHit, this);
|
||||
|
||||
}
|
||||
|
||||
function blockHit (body, shapeA, shapeB, equation) {
|
||||
|
||||
// The block hit something
|
||||
// This callback is sent: the Body it collides with
|
||||
// shapeA is the shape in the calling Body involved in the collision
|
||||
// shapeB is the shape in the Body it hit
|
||||
// equation is an array with the contact equation data in it
|
||||
|
||||
result = 'You last hit: ' + body.sprite.key;
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
block.body.setZeroVelocity();
|
||||
|
||||
if (cursors.left.isDown)
|
||||
{
|
||||
block.body.moveLeft(200);
|
||||
}
|
||||
else if (cursors.right.isDown)
|
||||
{
|
||||
block.body.moveRight(200);
|
||||
}
|
||||
|
||||
if (cursors.up.isDown)
|
||||
{
|
||||
block.body.moveUp(200);
|
||||
}
|
||||
else if (cursors.down.isDown)
|
||||
{
|
||||
block.body.moveDown(200);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function render() {
|
||||
|
||||
game.debug.text(result, 32, 32);
|
||||
|
||||
}
|
|
@ -463,10 +463,12 @@ Phaser.Game.prototype = {
|
|||
var v = Phaser.DEV_VERSION;
|
||||
var r = 'Canvas';
|
||||
var a = 'HTML Audio';
|
||||
var c = 1;
|
||||
|
||||
if (this.renderType == Phaser.WEBGL)
|
||||
if (this.renderType === Phaser.WEBGL)
|
||||
{
|
||||
r = 'WebGL';
|
||||
c++;
|
||||
}
|
||||
else if (this.renderType == Phaser.HEADLESS)
|
||||
{
|
||||
|
@ -476,21 +478,33 @@ Phaser.Game.prototype = {
|
|||
if (this.device.webAudio)
|
||||
{
|
||||
a = 'WebAudio';
|
||||
c++;
|
||||
}
|
||||
|
||||
if (this.device.chrome)
|
||||
{
|
||||
var args = [
|
||||
'%c %c %c Phaser v' + v + ' - ' + r + ' - ' + a + ' %c %c ' + ' http://phaser.io %c %c ',
|
||||
'%c %c %c Phaser v' + v + ' - ' + r + ' - ' + a + ' %c %c ' + ' http://phaser.io %c %c ♥%c♥%c♥ ',
|
||||
'background: #0cf300',
|
||||
'background: #00bc17',
|
||||
'color: #ffffff; background: #00711f;',
|
||||
'background: #00bc17',
|
||||
'background: #0cf300',
|
||||
'background: #00bc17',
|
||||
'background: #00711f'
|
||||
'background: #00bc17'
|
||||
];
|
||||
|
||||
for (var i = 0; i < 3; i++)
|
||||
{
|
||||
if (i < c)
|
||||
{
|
||||
args.push('color: #ff2424; background: #fff');
|
||||
}
|
||||
else
|
||||
{
|
||||
args.push('color: #959595; background: #fff');
|
||||
}
|
||||
}
|
||||
|
||||
console.log.apply(console, args);
|
||||
}
|
||||
else
|
||||
|
|
|
@ -86,6 +86,16 @@ Phaser.Physics.P2.Body = function (game, sprite, x, y, mass) {
|
|||
*/
|
||||
this.onImpact = new Phaser.Signal();
|
||||
|
||||
/**
|
||||
* @property {Phaser.Signal} onBeginContact - Dispatched when a first contact is created between two bodies. This event is fired before the step has been done.
|
||||
*/
|
||||
this.onBeginContact = new Phaser.Signal();
|
||||
|
||||
/**
|
||||
* @property {Phaser.Signal} onEndContact - Dispatched when final contact occurs between two bodies. This event is fired before the step has been done.
|
||||
*/
|
||||
this.onEndContact = new Phaser.Signal();
|
||||
|
||||
/**
|
||||
* @property {array} collidesWith - Array of CollisionGroups that this Bodies shapes collide with.
|
||||
* @private
|
||||
|
|
|
@ -324,19 +324,23 @@ Phaser.Physics.P2.prototype = {
|
|||
* Handles a p2 begin contact event.
|
||||
*
|
||||
* @method Phaser.Physics.P2#beginContactHandler
|
||||
* @private
|
||||
* @param {object} event - The event data.
|
||||
*/
|
||||
beginContactHandler: function (event) {
|
||||
|
||||
// console.log('beginContactHandler');
|
||||
// console.log(event);
|
||||
|
||||
if (event.bodyA.id > 1 && event.bodyB.id > 1)
|
||||
{
|
||||
// console.log('beginContactHandler');
|
||||
// console.log(event.bodyA.parent.sprite.key);
|
||||
// console.log(event.bodyB.parent.sprite.key);
|
||||
this.onBeginContact.dispatch(event.bodyA, event.bodyB, event.shapeA, event.shapeB, event.contactEquations);
|
||||
|
||||
if (event.bodyA.parent)
|
||||
{
|
||||
event.bodyA.parent.onBeginContact.dispatch(event.bodyB.parent, event.shapeA, event.shapeB, event.contactEquations);
|
||||
}
|
||||
|
||||
if (event.bodyB.parent)
|
||||
{
|
||||
event.bodyB.parent.onBeginContact.dispatch(event.bodyA.parent, event.shapeB, event.shapeA, event.contactEquations);
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
|
@ -345,19 +349,23 @@ Phaser.Physics.P2.prototype = {
|
|||
* Handles a p2 end contact event.
|
||||
*
|
||||
* @method Phaser.Physics.P2#endContactHandler
|
||||
* @private
|
||||
* @param {object} event - The event data.
|
||||
*/
|
||||
endContactHandler: function (event) {
|
||||
|
||||
// console.log('endContactHandler');
|
||||
// console.log(event);
|
||||
|
||||
|
||||
if (event.bodyA.id > 1 && event.bodyB.id > 1)
|
||||
{
|
||||
// console.log('endContactHandler');
|
||||
// console.log(event);
|
||||
this.onEndContact.dispatch(event.bodyA, event.bodyB, event.shapeA, event.shapeB);
|
||||
|
||||
if (event.bodyA.parent)
|
||||
{
|
||||
event.bodyA.parent.onEndContact.dispatch(event.bodyB.parent, event.shapeA, event.shapeB);
|
||||
}
|
||||
|
||||
if (event.bodyB.parent)
|
||||
{
|
||||
event.bodyB.parent.onEndContact.dispatch(event.bodyA.parent, event.shapeB, event.shapeA);
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
|
@ -817,7 +825,7 @@ Phaser.Physics.P2.prototype = {
|
|||
|
||||
while (i--)
|
||||
{
|
||||
output.push(this.world.springs[i]);
|
||||
output.push(this.world.springs[i].parent);
|
||||
}
|
||||
|
||||
return output;
|
||||
|
@ -828,7 +836,7 @@ Phaser.Physics.P2.prototype = {
|
|||
* Populates and returns an array of all current Constraints in the world.
|
||||
*
|
||||
* @method Phaser.Physics.P2#getConstraints
|
||||
* @return {array<Phaser.Physics.P2.Constraints>} An array containing all current Constraints in the world.
|
||||
* @return {array<Phaser.Physics.P2.Constraint>} An array containing all current Constraints in the world.
|
||||
*/
|
||||
getConstraints: function () {
|
||||
|
||||
|
@ -837,7 +845,7 @@ Phaser.Physics.P2.prototype = {
|
|||
|
||||
while (i--)
|
||||
{
|
||||
output.push(this.world.springs[i]);
|
||||
output.push(this.world.constraints[i].parent);
|
||||
}
|
||||
|
||||
return output;
|
||||
|
@ -898,6 +906,12 @@ Phaser.Physics.P2.prototype = {
|
|||
|
||||
},
|
||||
|
||||
/**
|
||||
* Creates a Collision Group for the wall shapes.
|
||||
*
|
||||
* @method Phaser.Physics.P2#createCollisionGroup
|
||||
* @protected
|
||||
*/
|
||||
createCollisionGroup: function () {
|
||||
|
||||
var bitmask = Math.pow(2, this._collisionGroupID);
|
||||
|
|
Loading…
Reference in a new issue