Jump to content

Bullet and layer collision issue


mrdotb
 Share

Recommended Posts

Hi,

I have an issue on my fire function. When i keep spacebar pressed collision between the bullet and the layer doesn't working.

You can test it http://s475613576.onlinehome.fr/portfolio/jeu/jeu/warped/index.html

 create() {// bullet group    APP.bullets = game.add.group();    APP.bullets.createMultiple(10, 'bullet');    APP.bullets.setAll('anchor.x', 0.5);    APP.bullets.setAll('anchor.y', 1);}update(){if (APP.fireButton.isDown)        {            fireBullet();        }game.physics.arcade.overlap(APP.bullet, APP.layer, function(bullet, layer) {        bullet.kill();    }, null, this);}}function fireBullet() {    if (game.time.now > APP.bulletTime) {        //game.sound.play('fire');        APP.bulletTime = game.time.now + APP.bulletRate;        //  Grab the first bullet we can from the pool        APP.bullet = APP.bullets.getFirstExists(false);        if (APP.bullet)        {            APP.bullet.lifespan = 2000; //kill after 2000ms            game.physics.enable(APP.bullet);            if (APP.facing === "right") {                //  And fire it                APP.bullet.reset(APP.player.x + 15, APP.player.y + 15);                APP.bullet.body.velocity.x = APP.bulletvelocity;            }            else if (APP.facing === "left") {                APP.bullet.reset(APP.player.x, APP.player.y + 15);                APP.bullet.body.velocity.x = -APP.bulletvelocity;            }        }    }}

Thank you for you're help .

Link to comment
Share on other sites

This is due to you using APP.bullet to create a global bullet reference and then your overlap check is only checking the most recent bullet. Instead of doing this, create a local var called 'currentBullet' or similar. Like so:

 create() {// bullet group    APP.bullets = game.add.group();    APP.bullets.createMultiple(10, 'bullet');    APP.bullets.setAll('anchor.x', 0.5);    APP.bullets.setAll('anchor.y', 1);    // ensure that all bullets have physics, rather than setting individually    APP.bullets.enableBody = true;    APP.bullets.physicsBodyType = Phaser.Physics.ARCADE;}update(){if (APP.fireButton.isDown)        {            fireBullet();        }// Changed the overlap to check the layer against the whole group instead of// an individual global bullet reference which will keep changing.game.physics.arcade.overlap(APP.layer, APP.bullets, function(bullet, layer) {        bullet.kill();    }, null, this);}}function fireBullet() {    if (game.time.now > APP.bulletTime) {        //game.sound.play('fire');        APP.bulletTime = game.time.now + APP.bulletRate;        // Grab the first bullet we can from the pool that's dead (to ensure        // you don't accidentally grab bullets which are mid-flight)        var currentBullet = APP.bullets.getFirstDead();        if (currentBullet)        {            currentBullet.lifespan = 2000; // kill after 2000ms            if (APP.facing === "right") {                //  And fire it                currentBullet.reset(APP.player.x + 15, APP.player.y + 15);                currentBullet.body.velocity.x = APP.bulletvelocity;            }            else if (APP.facing === "left") {                currentBullet.reset(APP.player.x, APP.player.y + 15);                currentBullet.body.velocity.x = -APP.bulletvelocity;            }        }    }}
Link to comment
Share on other sites

I resolve the issue by replacing APP.bullets.physicsBodyType = Phaser.Physics.ARCADE; by game.physics.enable(APP.bullets); and now it fire but when the bullet hit the layer

 game.physics.arcade.overlap(APP.layer, APP.bullets, function(layer, bullet) {        bullet.kill();    }, null, this);

I got .kill is not a function probably because we don't target the bullet who hit the layer but the bulletsgroup. I don't know what to do.

Link to comment
Share on other sites

No this is correct, however the order of the parameters may be wrong - try changing function(layer, bullet) to function(bullet, layer) and see if that works. The order that the colliding objects are returned in is not always intuitive.

 

Also, be aware that using game.physics.enable on the group will only enable physics for objects already in the group. If you later add more bullets, they will not have physics enabled.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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