Jump to content

How to make setIndexTileCallback() trigger once per collision


Recommended Posts


I ran across an issue with the setTileIndexCallback method involving it triggerring multiple times.  I have a health potion tile in my game and I would like for it to disappear when my player collides with it as well has add 1 to a variable storing the amount of health potions the player has collected.  The problem is that the health potion doesn't disappear until my player's sprite actually collides with the health potion tile but the callback function triggers as long as part of my player is touching or inside the 32x32 box around the health potion, which is smaller than 32x32.  Therefore, my variable the stores the amount of health potions collected increases infinitely as long as my player is in the 32x32 box but hasn't touched the health potion image.  

Here's my code relating to this issue:

Inside the create function:

var healthPotionCounter = 0;

map.setTileIndexCallback(9, function(){this.collectHealthPotion(game)}, this);

Inside this.collectHealthPotion(game) function:

map.putTile(-1, layer1.getTileX(player.x), layer1.getTileY(player.y));

healthPotionCounter += 1;

Help is much appreciated! Thank you!


Link to comment
Share on other sites

2 hours ago, drhayes said:

Is your health potion a tile? This might work better if it were a sprite. Then you could rely on a physics overlap call instead of a tile callback since, as you pointed out, the tile callback is called if even a tiny piece of a sprite overlaps the tile.

Thank you so much for the reply! It works great now!

Link to comment
Share on other sites


  • Recently Browsing   0 members

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