Phaser移动精灵

时间:2017-01-12 14:10:40

标签: phaser-framework

我有一些水平排列的精灵。使用的物理引擎是P2。在更新循环中,我为每个精灵设置速度:

sprite.body.velocity.x = 150;

然后我清除每个精灵的形状并加载我的自定义多边形:

sprite.body.clearShapes();
sprite.body.loadPolygon('physicsData', 'sprite1');

加载多边形后,精灵开始以不同的速度移动(不同的图像)。为什么会这样?当我不加载多边形时 - 一切正常,精灵以相同的速度移动。

1 个答案:

答案 0 :(得分:0)

我认为它与多边形有关,也就是说,在创建多边形时。但我不明白,你说它们都以不同的速度移动?他们是否有联系?因为我已经实现了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