Jump to content

collisions not working with multiple layers


Recommended Posts

I am attempting to make a player interact with one tile within a tilemap. I have created a whole world in Tiled, it has 4 layers, a BackgroundLayer, GroundLayer, ExitDoor, and EnemyTurn. Now, everything works, but the final piece of this. I have my code below...

this.maps = this.game.add.tilemap('tilemap');
this.maps.addTilesetImage('game_map', 'tiles');
this.backgroundLayer = this.maps.createLayer('BackgroundLayer');
this.groundLayer = this.maps.createLayer('GroundLayer');
this.enemyTurnLayer = this.maps.createLayer('EnemyTurns');
this.door = this.maps.createLayer('ExitDoor'); 

so the "BackgroundLayer" is nothing but decoration, so there is no collisions on that layer. However, the ground layer is my main world layer so I setup collisions for it by using the setCollision command. I did the same on EnemyTurn, which is to tell the enemies when to turn around because they are at the ends of thier platforms, and the "ExitDoor" which is a single tile at the very end of the level, a door the player will go through. Here is my code for the collisions...

this.maps.setCollision([133, 134, 135, 136, 137, 138, 139, 140, 141, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186], true, 'GroundLayer');
this.maps.setCollision(133, true, 'EnemyTurns');
this.maps.setCollision(154, true, 'ExitDoor');

Now, all works perfectly well, the player interacts with the platforms, the enemies turn when they are supposed to, the only problem is that last one, the "ExitDoor" I setup a special handler for that specific layer, so I can handle my own level end and new level start events....

this.game.physics.arcade.overlap(this.player, this.redKey, this.collectKey, null, this);
this.game.physics.arcade.overlap(this.player, this.door, this.canPlayerEnter, null, this);

that is in my update method. Here is the "canPlayerEnter" method...

canPlayerEnter(player, door) {
  console.log('player overlapped the door');
  if(player.hasKey) {
    this.game.physics.arcade.isPaused = true;
    this.stateText.text = "Congratulations! you beat it!!!!!\nClick the screen to start again.";
    this.stateText.visible = true;
    this.game.input.onDown.addOnce(this.restart, this);

the problem is, the very instant the game is started, I am getting logs in my console. Within the first 1 second of the game being played I got 157 console logs of "player overlapped the door". I don't know why this is happening, I have set the collision on that layer to only be with tile id 154, I have even user this.maps.putTileWorldXY to make sure that 154 is the correct tile, and it is. So why on earth is the "canPlayerEnter" method getting called constantly? It makes no sense, the player is not overlapping that tile. Please help, please, please, help, this is driving me nuts.

Thank you for any help you can give. 

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...