我想它与多边形有关,也就是说,在创建它们时。但是我不明白,你说它们都以不同的速度移动?他们有没有联系?因为我已经从 Phaser 示例中实现了一个简单的代码,并且两个多边形以相同的速度移动而没有逻辑上的接触。
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('contra2', 'contra2.png');
game.load.image('bunny', 'bunny.png');
// Load our physics data exported from PhysicsEditor
game.load.physics('physicsData', 'sprites.json');
}
var contra, bunny;
function create() {
// Enable p2 physics
game.physics.startSystem(Phaser.Physics.P2JS);
contra = game.add.sprite(400, 300, 'contra2');
bunny = game.add.sprite(100, 300, 'bunny');
// Enable the physics body on this sprite and turn on the visual debugger
game.physics.p2.enable(contra, true);
game.physics.p2.enable(bunny, true);
// Clear the shapes and load the 'contra2' polygon from the physicsData JSON file in the cache
contra.body.clearShapes();
contra.body.loadPolygon('physicsData', 'contra2');
bunny.body.clearShapes();
bunny.body.loadPolygon('physicsData', 'bunny');
// Just starts it rotating
game.input.onDown.add(function() { start = true; }, this);
cursors = game.input.keyboard.createCursorKeys();
}
function update() {
contra.body.setZeroVelocity();
bunny.body.setZeroVelocity();
if (cursors.left.isDown)
{
contra.body.moveLeft(200);
bunny.body.moveLeft(200);
}
else if (cursors.right.isDown)
{
contra.body.moveRight(200);
bunny.body.moveRight(200);
}
if (cursors.up.isDown)
{
contra.body.moveUp(200);
bunny.body.moveUp(200);
}
else if (cursors.down.isDown)
{
contra.body.moveDown(200);
bunny.body.moveDown(200);
}
}
Examples of P2 Polygons
Examples of P2 Movements