Phaser 3.17不会设置tilemap图层和播放器精灵之间的碰撞

时间:2019-05-16 13:38:58

标签: collision phaser-framework tile

地图图层和播放器精灵之间没有碰撞。但是世界界限之间的碰撞是可行的。怎么了?

我尝试了可以​​在网上找到的其他变通办法,但都没有奏效。

游戏配置

const config = {
    type: Phaser.AUTO,
    width: 800,
    height: 600,
    backgroundColor: "#f",
    physics: {
        default: 'arcade',
        arcade: {
            gravity: { y: gameState.gravity },
            debug: true
        }
    },
    scene: {
        preload,
        create,
        update
    }
};

create()中的代码,有关图块及其图层和字符

gameState.map = this.add.tilemap('map');
gameState.dungeonTileset = gameState.map.addTilesetImage('dungeon', 'dungeonTiles');

gameState.backgroundLayer = gameState.map.createStaticLayer('Background', gameState.dungeonTileset);
gameState.mapLayer = gameState.map.createStaticLayer('Map', gameState.dungeonTileset);
gameState.miscLayer = gameState.map.createStaticLayer('Misc', gameState.dungeonTileset);

gameState.mapLayer.setCollisionByExclusion([-1]);

this.physics.world.bounds.width = gameState.mapLayer.width;
this.physics.world.bounds.height = gameState.mapLayer.height;

gameState.player = this.physics.add.sprite(73, 398, 'player', 0);
gameState.player.setCollideWorldBounds(true);

this.physics.add.collider(gameState.player, gameState.mapLayer);

控制台中没有警告,也没有错误出现。我不知道该怎么办。

谢谢!

1 个答案:

答案 0 :(得分:0)

我加了一点the original example,所以您可以看到它的外观,我认为您代码中的问题是与gameState.mapLayer.setCollisionByExclusion([-1]);有关的行,但是不确定,因为我看不到tilemap已创建

var config = {
    type: Phaser.WEBGL,
    width: 800,
    height: 576,
    backgroundColor: '#2d2d2d',
    parent: 'phaser-example',
    loader: {
      baseURL: 'https://labs.phaser.io',
      crossOrigin: 'anonymous'
    },
    pixelArt: true,
    physics: {
        default: 'arcade',
        arcade: { gravity: { y: 300 } }
    },
    scene: {
        preload: preload,
        create: create,
        update: update
    }
};

var game = new Phaser.Game(config);
var map;
var cursors;
var player;
var groundLayer;

function preload ()
{
    this.load.image('ground_1x1', 'assets/tilemaps/tiles/ground_1x1.png');
    this.load.tilemapTiledJSON('map', 'assets/tilemaps/maps/tile-collision-test.json');
    this.load.image('player', 'assets/sprites/phaser-dude.png');
}

function create ()
{
  map = this.make.tilemap({ key: 'map' });
  var groundTiles = map.addTilesetImage('ground_1x1');

  map.createDynamicLayer('Background Layer', groundTiles, 0, 0);
  groundLayer = map.createDynamicLayer('Ground Layer', groundTiles, 0, 0);
  
  groundLayer.setCollisionBetween(1, 25);//here

  player = this.physics.add.sprite(80, 70, 'player')
      .setBounce(0.1);

  this.physics.add.collider(player, groundLayer);

  cursors = this.input.keyboard.createCursorKeys();
  this.cameras.main.setBounds(0, 0, map.widthInPixels, map.heightInPixels);
  this.cameras.main.startFollow(player);

}


function update ()
{

  player.body.setVelocityX(0);

  if (cursors.left.isDown)
  {
      player.body.setVelocityX(-200);
  }
  else if (cursors.right.isDown)
  {
      player.body.setVelocityX(200);
  }

  if (cursors.up.isDown && player.body.onFloor())
  {
      player.body.setVelocityY(-300);
  }
}
<script src="//cdn.jsdelivr.net/npm/phaser@3.17.0/dist/phaser.min.js"></script>