Jump to content

How to only trigger tileCollisions once?


jakedotdk
 Share

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    },

/Jake

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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