Estoy tratando de crear un juego de plataformas en fase 3 usando el motor de física de la materia, actualmente estoy tratando de crear niveles usando mosaicos, he llegado a un punto en el que el jugador no puede saltar infinitamente, sin embargo, aún puede saltar paredes.
Actualmente estoy tratando de hacerlo para que solo puedan saltar si chocan con una capa específica llamada piso.
Cuando trato de usar el siguiente código, aparece un error que dice:
TypeError no capturado: no se pueden establecer propiedades de nulo (estableciendo 'etiqueta')
game.js (crear función):
create() { //External function to create player player(this,'test',300,200,'player') pawn.setScale(1.5) pawn.setDepth(2) pawn.setBounce(0.1) pawn.setFriction(0,0,1) pawn.setCircle(20) pawn.setMass(1) this.touchingGround = false; this.jumpForce = 0.05 this.map = this.make.tilemap({key: 'lv3'}) this.tileset = this.map.addTilesetImage('tiles','tile', 32, 32) this.walls = this.map.createLayer('walls', this.tileset) this.walls.setCollisionByExclusion(-1, true); this.floor = this.map.createLayer('floor', this.tileset) this.floor.setCollisionByExclusion(-1, true); this.matter.world.convertTilemapLayer(this.walls); this.matter.world.convertTilemapLayer(this.floor); pawn.body.label = 'player' this.floor.body.label = 'floor' this.matter.world.on("collisionactive", (e,o1, o2) => { if(o1.label == 'player' && o2.label == 'floor') { this.touchingGround = true; } }); };Lo siento si esto es una solución realmente fácil o simplemente no funciona y estoy siendo estúpido.
No soy un experto en motores de matter.js , pero supongo que el error ocurre en la línea this.floor.body.label = 'floor' . Una solución fácil sería utilizar el parámetro options , del método convertTilemapLayer ( Documentación ).
Aquí una demostración de trabajo:
(código basado en este ejemplo de phaser.io )
var config = { type: Phaser.AUTO, width: 11 * 16, height: 6 * 16, zoom: 2, pixelArt: true, scene: { preload: preload, create: create }, physics: { default: 'matter', matter: { gravity: { y:.3 }, } }, banner: false }; function preload (){ this.load.image('mario-tiles', 'https://labs.phaser.io/assets/tilemaps/tiles/super-mario.png'); } function create (){ var level = [ [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 14, 14, 14, 14, 14, 0, 0, 0, 0 ], ] var map = this.make.tilemap({ data: level, tileWidth: 16, tileHeight: 16 }); var tiles = map.addTilesetImage('mario-tiles'); var layer = map.createLayer(0, tiles, 0, 0); let player = this.add.rectangle(40, 10, 8, 8, 0xffffff) layer.setCollision([14]); this.matter.add.gameObject(player); this.matter.world.convertTilemapLayer(layer, {label:'floor'}); let info = this.add.text(4, 4, 'Waiting for collision', {color:'#ffffff', fontSize:10}) .setOrigin(0); player.body.label = 'player'; this.matter.world.on("collisionactive", (e, o1, o2) => { if(o1.label == 'player' && o2.label == 'floor') { info.setText('touching'); } }); } new Phaser.Game(config); <script src="//cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.js"></script>