Jump to content

Newbie question: Collide sprite (eg player) with tile (eg collectible treasure)


owen
 Share

Recommended Posts

Hi, simple newbie question for you.

 

This is regarding a game created with Tiled and Phaser.

 

I have a tilemap with tiles and in it I have a player sprite which is a spritesheet and I want to fire a given function whenever the player sprite collides with certain tiles.  

 

Specifically I am trying to have the player collect treasure.  (The player is a spritesheet, the treasure is a variety of tiles).  All of the treasure tiles are in a specific tileset.  It should be really simple but I cannot find a clear working example of detecting collision between a spritesheet and tile within a Phaser game.

 

How can I initialise the detection, presumably in the create() function?

 

I'm trying to do something like this but its not having any effect:

 

   map.forEach(function (t) {        if (t) {                        var tsi = t.index;                               if (tsi >=  0 && tsi < 100) {                 // platforms - impassable from above but passable from below, left, right                t.collideDown = false;                t.collideLeft = false;                t.collideRight = false;            } else if (tsi >= 200 && tsi < 300) {                // blocks - impassable from any direction                t.collideDown = false;                t.collideLeft = false;                t.collideUp = false;                t.collideRight = false;            } else if (tsi >= 100 && tsi < 200) {                // scenery (eg. trees, flowers) - passable from all directions                //            } else if (tsi >= 400 && tsi < 500) {                // treasure - impassable from any direction                t.collideDown = false;                t.collideLeft = false;                t.collideUp = false;                t.collideRight = false;                // my attempt at collecting treasure - does not work!                map.setTileIndexCallback(t.index, hitTreasure, this);            } else {                // default - passable from below only                t.collideDown = false;            }        }    }, game, 0, 0, map.width, map.height, layer);
And the function to be called is below, and does not fire.
function hitTreasure(player,tile) {    tile.alpha = 0.2;    layer.dirty = true;    console.log("hitTreasure!");    return false;}

 

 

 

The attempt so far can be fond at: http://www.owensouthwood.com/mrgoggles

 

 

EDIT: I solved it, I was in fact doing it right but the problem was this line:

} else if (tsi >= 400 && tsi < 500) { // should have been 300..400... sorry

Thanks,

Owen

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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