Files
phaser/examples/collision/sprite vs group.js
T
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

90 lines
2.1 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('phaser', 'assets/sprites/phaser-dude.png');
game.load.spritesheet('veggies', 'assets/sprites/fruitnveg32wh37.png', 32, 32);
}
var sprite;
var group;
function create() {
game.stage.backgroundColor = '#2d2d2d';
// This will check Sprite vs. Group collision
sprite = game.add.sprite(32, 200, 'phaser');
sprite.name = 'phaser-dude';
group = game.add.group();
for (var i = 0; i < 50; i++)
{
var c = group.create(100 + Math.random() * 700, game.world.randomY, 'veggies', game.rnd.integerInRange(0, 36));
c.name = 'veg' + i;
c.body.immovable = true;
}
for (var i = 0; i < 20; i++)
{
// Here we'll create some chillis which the player can pick-up. They are still part of the same Group.
var c = group.create(100 + Math.random() * 700, game.world.randomY, 'veggies', 17);
c.name = 'chilli' + i;
c.body.immovable = true;
}
game.input.keyboard.addKeyCapture([ Phaser.Keyboard.LEFT, Phaser.Keyboard.RIGHT, Phaser.Keyboard.UP, Phaser.Keyboard.DOWN ]);
}
function update() {
sprite.body.velocity.x = 0;
sprite.body.velocity.y = 0;
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
{
sprite.body.velocity.x = -200;
}
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
{
sprite.body.velocity.x = 200;
}
if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
{
sprite.body.velocity.y = -200;
}
else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN))
{
sprite.body.velocity.y = 200;
}
game.physics.collide(sprite, group, collisionHandler, null, this);
}
function collisionHandler (obj1, obj2) {
// If the player collides with the chillis then they get eaten :)
// The chilli frame ID is 17
console.log('Hit', obj2.name);
if (obj2.frame == 17)
{
obj2.kill();
}
}
function render () {
// game.debug.renderQuadTree(game.physics.quadTree);
}