Tilemap图层未与播放器碰撞

时间:2019-05-31 10:24:42

标签: javascript phaser-framework

我在图块地图上遇到问题,我使用的是单层,并且我将图块编号设置为5,因此它的功能就像是玩家无法通过的障碍物,所以我希望如此。但这是行不通的,正如您从图片中注意到的那样,我使用图形方法来显示障碍是否可碰撞。注意:控制台不会显示任何错误,任何帮助都将非常有用! https://i.stack.imgur.com/UB4S8.png https://i.stack.imgur.com/cLzcu.png

{ "height":6,
 "infinite":false,
 "layers":[
        {
         "data":[1, 1, 1, 1, 1, 1, 1, 5, 5, 1, 1, 1, 5, 1, 1, 1, 1, 5, 1, 1, 1, 5, 5, 1, 1, 1, 1, 1, 1, 1],
         "height":6,
         "id":1,
         "name":"Tile Layer 1",
         "opacity":1,
         "type":"tilelayer",
         "visible":true,
         "width":5,
         "x":0,
         "y":0
        }],
 "nextlayerid":2,
 "nextobjectid":1,
 "orientation":"orthogonal",
 "renderorder":"right-down",
 "tiledversion":"2019.05.08",
 "tileheight":32,
 "tilesets":[
        {
         "columns":25,
         "firstgid":1,
         "image":"ground_1x1.png",
         "imageheight":32,
         "imagewidth":800,
         "margin":0,
         "name":"ground_1x1",
         "spacing":0,
         "tilecount":25,
         "tileheight":32,
         "tilewidth":32,
         "transparentcolor":"#ff0000"
        }],
 "tilewidth":32,
 "type":"map",
 "version":1.2,
 "width":5
}

function create ()
{

    map = this.make.tilemap({key: 'map'});

   groundTiles = map.addTilesetImage('ground_1x1', 'ground');
    map.createStaticLayer('Tile Layer 1', groundTiles,0,0);
    map.setCollision(5);

    var debugGraphics = this.add.graphics();
    map.renderDebug(debugGraphics);
    debugGraphics.visible = true;




indi= this.physics.add.sprite(200, 60, 'mario');
indi.body.allowGravity = false;
indi.setInteractive();
this.cursors = this.input.keyboard.createCursorKeys();
indi.setCollideWorldBounds(true);
this.physics.add.collider(indi, groundTiles);

}

1 个答案:

答案 0 :(得分:0)

var config = {
    type: Phaser.WEBGL,
    width: 400,
    height: 288,
    parent: 'phaser-example',
    loader: {
      baseURL: 'https://raw.githubusercontent.com/nazimboudeffa/assets/master/',
      crossOrigin: 'anonymous'
    },
    physics: {
      default: 'arcade',
      arcade: {
        gravity: { y: 10 }
      }
    },
    scene: {
        preload: preload,
        create: create
    }
};

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

function preload ()
{
    this.load.image('tileset', 'maps/super-pixel-platformer-set/tileset.png');
    this.load.tilemapTiledJSON('map', 'maps/super-pixel-platformer-set/level0.json');
    this.load.image('alien', 'sprites/phaser-alien.png');
}

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

  groundLayer = map.createStaticLayer('ground', groundTiles, 0, 0);
  map.createStaticLayer('jumpThrough', groundTiles, 0, 0);
  
  //groundLayer.setCollision(7);
  groundLayer.setCollisionBetween(7,9);
  
  player = this.physics.add.sprite(100, 50, 'alien');
  player.setCollideWorldBounds(true);
  
  this.physics.add.collider(player, groundLayer);
}
<script src="//cdn.jsdelivr.net/npm/phaser@3.17.0/dist/phaser.min.js"></script>