phaser/examples/collision/polygon body.js

50 lines
1.4 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('parsec', 'assets/sprites/parsec.png');
game.load.image('spaceman', 'assets/sprites/exocet_spaceman.png');
}
var sprite1;
var sprite2;
function create() {
game.stage.backgroundColor = '#2d2d2d';
// Here we've replaced the sprites body with a polygon.
// The point data was generated by tracing the PNGs in Physics Editor: http://www.codeandweb.com/physicseditor
sprite1 = game.add.sprite(550, 250, 'parsec');
sprite1.body.setPolygon(56, -1 , 10, -5 , 1, -13 , 0, -34 , 55, -60 , 122, -78 , 165, -80 , 214, -74 , 285, -71 , 296, -44 , 298, -12 , 292, -5 , 168, -3);
sprite1.body.translate(0, 80);
sprite1.body.velocity.x = -100;
sprite2 = game.add.sprite(0, 200, 'spaceman');
sprite2.body.setPolygon(34, -172 , 75, -172 , 87, -145 , 121, -52 , 105, -16 , 55, -3 , 9, -19 , 1, -57 , 24, -145);
sprite2.body.translate(0, 175);
sprite2.body.velocity.x = 100;
}
function update() {
game.physics.collide(sprite1, sprite2, collisionHandler, null, this);
}
function collisionHandler (obj1, obj2) {
game.stage.backgroundColor = '#992d2d';
}
function render() {
game.debug.physicsBody(sprite1.body);
game.debug.physicsBody(sprite2.body);
}