Hi All!
Im currently building a dungeon crawler, and my character is not colliding wit the walls.
Everything is made up of tiles, all wals are added to the physics group, and also I’m creating the player as a physics object.
This is how I setup everything:
create () {
this.walls = this.physics.add.staticGroup();
this.map = new MapGenerator(this.width, this.height, 20, 10, 10, 60);
this.asciiMap = this.map.getMap();
this.displayMapOnCanvas(this.asciiMap);
this.player = this.physics.add.sprite(this.playerSpawn.spawnX, this.playerSpawn.spawnY, 'hero');
this.player.setBounce(0.2);
this.physics.add.collider(this.game, false, this.player, this.walls);
this.cameras.main.setBounds(0, 0, this.game.width, this.game.height);
this.cameras.main.startFollow(this.player);
this.cursors = this.input.keyboard.createCursorKeys();
}
And this is how I draw the map with the walls:
displayMapOnCanvas(asciiMap) {
var wallPhysics = null;
var sprite = null;
var image = null;
var playerX = null;
var playerY = null;
for(var y = 0; y < this.height; y++){
for (var x = 0; x < this.width; x++){
switch(asciiMap[y][x].pointFace){
case '#': sprite = 'tile-stone';
break;
case ' ': sprite = 'tile-ground';
break;
case 'D': sprite = 'tile-door';
break;
case 'H': sprite = 'tile-path';
break;
case 'X': sprite = 'tile-marker';
break;
case '+': sprite = 'tile-wall';
break;
case '@': sprite = 'player';
break;
default: sprite = 'tile-ground';
}
if (sprite == 'tile-wall') {
wallPhysics = this.physics.add.sprite(x*32, y*32, sprite);
wallPhysics.setOrigin(0, 0);
this.walls.add(wallPhysics);
} else if (sprite == 'player'){
image = this.add.image(x*32, y*32, 'tile-ground');
image.setOrigin(0, 0);
this.playerSpawn = {spawnX: (x * 32) + 16, spawnY: (y * 32) + 16};
} else {
image = this.add.image(x*32, y*32, sprite);
image.setOrigin(0, 0);
}
}
}
}
The collisions are not being detected at all.
Thank you for all the help!