phaser/examples/collision/process callback.js
photonstorm 011d2d8e05 The way the collision process callback works has changed significantly and now works as originally intended.
The World level quadtree is no longer created, they are now built and ripped down each time you collide a Group, this helps collision accuracy.
Bodies are no longer added to a world quadtree, so have had all of their quadtree properties removed such as skipQuadtree, quadTreeIndex, etc.
QuadTree.populate - you can pass it a Group and it'll automatically insert all of the children ready for inspection.
Removed ArcadePhysics binding to the QuadTree, so it can now be used independantly of the physics system.
2014-01-14 02:43:09 +00:00

65 lines
1.8 KiB
JavaScript

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';
// This will check Sprite vs. Sprite collision using a custom process callback
sprite1 = game.add.sprite(0, 200, 'atari');
sprite2 = game.add.sprite(750, 220, 'mushroom');
// We'll use random velocities so we can test it in our processCallback
sprite1.body.velocity.x = 50 + Math.random() * 100;
sprite2.body.velocity.x = -(50 + Math.random() * 100);
}
function update() {
game.physics.collide(sprite1, sprite2, collisionCallback, processCallback, this);
}
function processCallback (obj1, obj2) {
// This function can perform your own additional checks on the 2 objects that collided.
// For example you could test for velocity, health, etc.
// This function needs to return either true or false. If it returns true then collision carries on (separating the two objects).
// If it returns false the collision is assumed to have failed and aborts, no further checks or separation happen.
if (obj1.body.speed > obj2.body.speed)
{
return true;
}
else
{
return false;
}
}
function collisionCallback (obj1, obj2) {
game.stage.backgroundColor = '#992d2d';
}
function render() {
game.debug.renderText('The processCallback will only collide if sprite1 is going fastest.', 32, 32);
game.debug.renderText('Sprite 1 speed: ' + sprite1.body.speed, 32, 64);
game.debug.renderText('Sprite 2 speed: ' + sprite2.body.speed, 32, 96);
}