Jump to content

Killing a bullet when it hits a wall


Recommended Posts

Hey all, just started working with Phaser today and it's great fun. 


I've become stuck trying to destroy a sprite when it hits a wall. 


Something like this works quite well for colliding and stopping against a wall:


    game.physics.collide(mobs, walls);
And something like this is great for killing mobs when they hit a bullet:
    game.physics.overlap(mobs, bulletGroup, killMob, null, this);
But the same overlap callbacks don't seem to work when I'm comparing a wall to a group. 
For instance, this doesn't work:     game.physics.overlap(wall, bulletGroup, killBullet, null, this);

In this case, the walls are:

  walls = game.add.tilemapLayer(0, 0, 800, 600, tileset, map, 1);
Looking forward to carrying on with the project!



Link to comment
Share on other sites

I solved this in a really hacky way: 


    if (bulletGroup.length >= 1) {
      game.physics.collide(bulletGroup, walls);
    bulletGroup.forEach(function (bullet) {
      if (bullet.body.velocity.x == 0 && bullet.body.velocity.y == 0) {
This way they stop if they hit a wall and they're killed if they stop. 
There must be a better way to do this?
Link to comment
Share on other sites


  • Recently Browsing   0 members

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