概要
Plunkerで、Phaser.Physicsやってみた。
プリミティブ全部使ってみた。
写真
サンプルコード
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', {
create: create,
render: render
});
var bodies = [];
var codes = [];
var codeCaption;
function create() {
game.stage.backgroundColor = '#124184';
game.physics.startSystem(Phaser.Physics.BOX2D);
game.physics.box2d.debugDraw.centerOfMass = true;
for (var i = 0; i < 7; i++)
{
bodies.push(new Phaser.Physics.Box2D.Body(this.game, null, 100 + i * 100, 300, 0));
}
bodies[0].setCircle(40);
bodies[1].setRectangle(50, 100);
bodies[2].setRectangle(50, 100, 0, 0, 10);
bodies[3].setRectangle(50, 100, 0, 25, 10);
bodies[4].setEdge(-10, -20, 20, 60);
bodies[5].setChain([-10, -20, 20, 60, 35, -30, -20, -50, -40, 20, -10, 20]);
bodies[6].setPolygon([-10, -20, 20, 60, 35, -30, -20, -50, -40, 20, -10, 20]);
codes[0] = "setCircle(40)";
codes[1] = "setRectangle(50, 100)";
codes[2] = "setRectangle(50, 100, 0, 0, 10)";
codes[3] = "setRectangle(50, 100, 0, 25, 10)";
codes[4] = "setEdge(-10, -20, 20, 60)";
codes[5] = "setChain([-10, -20, 20, 60, 35, -30, -20, -50, -40, 20, -10, 20])";
codes[6] = "setPolygon([-10, -20, 20, 60, 35, -30, -20, -50, -40, 20, -10, 20])";
game.add.text(5, 5, 'Manual fixture creation. Move the mouse near each type to see the code used to create it.', {
fill: '#ffffff',
font: '14pt Arial'
});
codeCaption = game.add.text(5, 25, '', {
fill: '#ffffff',
font: '14pt Arial'
});
}
function render() {
game.debug.box2dWorld();
codeCaption.text = "";
for (var i = 0; i < bodies.length; i++)
{
if (Math.abs(game.input.mousePointer.x - bodies[i].x ) < 50 && Math.abs(game.input.mousePointer.y - bodies[i].y ) < 50 )
{
codeCaption.text = codes[i];
game.debug.box2dBody(bodies[i], 'rgb(255,255,0)');
}
}
}
成果物
以上。