Reputation: 281
I have a few sprites, which are arranged horizontally. Used physics engine is P2. In update loop I setup velocity for each sprite:
sprite.body.velocity.x = 150;
Then I clear shapes for each sprite and load my custom polygon:
sprite.body.clearShapes();
sprite.body.loadPolygon('physicsData', 'sprite1');
After I load polygons, sprites start to move with different speed (different images). Why this happened? When I don't load polygons - everything works fine, sprites move with the same speed.
Upvotes: 0
Views: 1811
Reputation: 1396
I suppose it has to do with polygons, that is, when creating them. But I do not understand something, you say that they both move at different speeds? Are they in contact or not? Because I have implemented a simple code from the Phaser examples and both polygons move at the same speed without being in contact logically.
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);
}
}
Upvotes: 0