Jump to content

Check for collision on tile layer then restart game.


D3thy
 Share

Recommended Posts

Dear Phaser Community,

 

Thanks for taking the time help out. I'm currently trying to kill my player when he touches some fire. The player is just an individual sprite and the fire is my second layer from a tilemap. The idea was to check for collision and then restart game if true. What is the best method to go about doing this, i seem to be unable to figure it out from the collision examples provided by Phaser.

 

preload:

 

//the player

game.load.spritesheet('mainCharacter', 'assets/Game-Graphics/player2.png', 25, 25);

//the tilemap

game.load.tilemap('map', 'assets/map.json', null, Phaser.Tilemap.TILED_JSON);

//the fire

game.load.image('doom-fire-tile', 'assets/Game-Graphics/doom-fire.png');

 

create:

 

//the map

map = game.add.tilemap('map');

//the tileset

map.addTilesetImage('doom-fire', 'doom-fire-tile');

//the layer

layer2 = map.createLayer('killfire')

 

What would be the best method to check for collision between player and the fire or layer 2, and then have the game restart. I'm only using the generic game states so i thought i could just have the restart function reload the "preload state" like this, but it didn't seem to work.

 

function restart(){
            game.state.start('preload')
        }

 

 

 

Link to comment
Share on other sites

 

1. Check overlapping in update function, like in example Overlap Without Physics

function checkOverlap(spriteA, spriteB) {    var boundsA = spriteA.getBounds();    var boundsB = spriteB.getBounds();    return Phaser.Rectangle.intersects(boundsA, boundsB);}

 

Like i already said in my first post "i seem to be unable to figure it out from the collision examples provided by Phaser." I have already attempted to check for collision and then run my restart function, was unable to make it work. Hence why i am providing the code used to create my objects i want to check collision on, and my restart function to make sure that isn't the problem.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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