Jump to content

How to only trigger tileCollisions once?


Recommended Posts

Hi :)

I am coding a small tilebased-top-down adventuregame.

I'm learning Phaser as I go along, and I've discovered the setTileIndexCallback method.

map.setTileIndexCallback(tileIndex, callBackFunction, this); 

It's working like a charm ... except for the fact that I only want it to trigger the callback function when my character enters the tile.

It seems as though every Update() triggers the callback function as long as the player and the tile overlap.

Is there any way to alter this behavior so it only triggers when the character enters the tile?


Kind regards - Jakob

Link to comment
Share on other sites

Fixed it myself... :)

a ) add a global variable

var currentTile;

b ) set up the tileindexcallback as usual (TileType.chest is the index of the tile type)

 game.map.setTileIndexCallback(TileType.chest, this.hitChest, this);

c ) in the callback function check whether this tile is the last one hit, and if it is - return immediately, otherwise do stuff, and remember this tile in the global variable:

    hitChest: function (sprite, tile) {        if (tile === currentTile) { return }        else { currentTile = tile; };       //do stuff    },


Link to comment
Share on other sites


  • Recently Browsing   0 members

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