Phaser - 街机碰撞物理

Phaser - Arcade collision physics

我正在使用 Phaser 框架开发一个简单的方块游戏,但不幸的是,我在使用 Arcade 碰撞方法时偶然发现了 "bug"。我希望所有的瓷砖都能完美地堆叠在一起,但上面的瓷砖总是穿过它们下面的瓷砖。

这是代码:

var game = new Phaser.Game(700, 700, Phaser.AUTO, 'phaser-demo', {
  create: create,
  update: update
});

var tiles, textureRegistry = {};

function create() {
  game.physics.startSystem(Phaser.Physics.ARCADE);
  game.physics.arcade.gravity.y = 500;

  tiles = game.add.group();
  tiles.physicsBodyType = Phaser.Physics.ARCADE;
  tiles.enableBody = true;

  for (var i = 0; i < 10; i++) {
    for (var j = 0; j < 10; j++) {
      tiles.add(game.add.sprite(i * 70, j * 70, createBlock(64, 'red')));
    }
  }
  tiles.setAll('body.collideWorldBounds', true);
  tiles.setAll('body.bounce', new Phaser.Point(0.5, 0.5));

}

function update() {
  game.physics.arcade.collide(tiles);
}

function createBlock(size, color) {
  var name = size + '_' + color;
  if (textureRegistry[name]) {
    return textureRegistry[name];
  }

  var bmd = game.add.bitmapData(size, size);
  bmd.ctx.fillStyle = color;
  bmd.ctx.fillRect(0, 0, size, size);
  textureRegistry[name] = bmd;
  return bmd;
}
<script src="https://github.com/photonstorm/phaser/releases/download/v2.6.2/phaser.min.js"></script>

Chrome 看起来更糟。重要的是要注意,只有在使用 4x4 或更多图块时才会出现此问题。

Phaser Arcade Physics doesn't handle multi-body contact very well, due to limitations on current version

Alternatively consider using P2 physics instead and/or see discussions below.