彼らの例から、基本的なフェイザー/ BOX2Dの設定で(例:800x600 ...)、ここにはcreate
とupdate
のメソッドに入るコードがあります。基本的には静的な地面を作成します.2つの動的ボディ:カートとポールを作成し、次にrevoluteJoint
メソッドを使用して2つの動的ボディを結合します。
function create() {
// Enable Box2D physics
game.physics.startSystem(Phaser.Physics.BOX2D);
game.physics.box2d.debugDraw.joints = true;
game.physics.box2d.setBoundsToWorld();
game.physics.box2d.gravity.y = 500;
ground = new Phaser.Physics.Box2D.Body(this.game, null, game.world.centerX, 575, 0); // game, sprite, cx, cy, density [static/kinematic/dynamic], world
ground.setRectangle(800, 50, 0, 0, 0); // width, height, offsetx, offsety, angle (rads)
cart = new Phaser.Physics.Box2D.Body(this.game, null, game.world.centerX, 543);
cart.setRectangle(60, 10, 0, 0, 0);
cart.mass = 1;
pole = new Phaser.Physics.Box2D.Body(this.game, null, game.world.centerX, 495);
pole.setRectangle(4, 100, 0, 0, 0);
pole.mass = 0.1;
pole.linearDamping = 4; // makes the pole fall slower (heavy air resistance)
//bodyA, bodyB, ax, ay, bx, by, motorSpeed, motorTorque, motorEnabled, lowerLimit, upperLimit, limitEnabled
game.physics.box2d.revoluteJoint(cart, pole, 0, -5, 0, 50);
pole.angle = 5; // so it starts out falling to the right
//Set up arrow keys for input
cursors = game.input.keyboard.createCursorKeys();
// track the angle of the pole
game.add.text(5, 5, 'Pole balancing.', { fill: '#ffffff', font: '14pt Arial' });
caption1 = game.add.text(5, 30, 'Angle: ' + pole.angle, { fill: '#dddddd', font: '10pt Arial' });
}
function update() {
caption1.text = 'Angle: ' + pole.angle;
if (cursors.left.isDown) {
cart.applyForce(-5,0);
}
if (cursors.right.isDown) {
cart.applyForce(5,0);
}
}