Jump to content

Issues with bullets groups


Recommended Posts

Hello guys,


I have a problem with my fire bullets function. If i shot my bullets one by one the function work nice, the bullet disappears when he touch the ennemy target and the ennemy target lose 1 health. But when i shot lots of bullets in the same time, the bullets don't work , only the last bullet seems to work . I think it's a problem with the bullet's group but i didn't find it.

// Bullets group    bullets = game.add.group();    bullets.enableBody = true;    bullets.physicsBodyType = Phaser.Physics.ARCADE;    bullets.createMultiple(30, 'bullet');    bullets.setAll('anchor.x', 0.5);    bullets.setAll('anchor.y', 4);    bullets.setAll('outOfBoundsKill', true);    bullets.setAll('checkWorldBounds', true);	fireButton = game.input.keyboard.addKey(Phaser.Keyboard.M);if ((Phaser.Math.distance(mustang.x, mustang.y, bullet.x, bullet.y)) <= 60){ //mustang is the ennemy target		bullet.kill();		if (game.time.now > killTime)  				mustanghealth -= 1 ;		killTime = game.time.now + 40;		}						}function fireBullet () { // My fire function        if (game.time.now > bulletTime  )    {                bullet = bullets.getFirstExists(false);        if (bullet)        {                       			bullet.reset(zero.x, zero.y );			bullet.angle = zero.angle;			game.physics.arcade.velocityFromAngle(zero.angle-90, 300, bullet.body.velocity); 									             bulletTime = game.time.now + 400;         }    }}
Link to comment
Share on other sites



I'm not sure if you noticed but around line 15 right after the line bullet.kill(); you don't have an opening bracket for your if statement, but you have an extra closing bracket. 


I'm guessing that this is how you are checking for bullet collision with the enemy: if ((Phaser.Math.distance(mustang.x, mustang.y, bullet.x, bullet.y)) <= 60) ?


It would be a lot more efficient and most likely to get rid of your bug if you use this to check if the bullet and the enemy overlap ( touch ):


game.physics.arcade.overlap(mustang, bullet, yourFunction, null, this);


The above line of code should be added to your update function, it's checking if mustang is overlapping with bullet and then call a function of your choice, yourFunction() for example, inside that function you can add the code that you are using in your if statement:


youFunction: function(game) {

if (game.time.now > killTime)  
mustanghealth -= 1 ;
killTime = game.time.now + 40;




let me know if this helps.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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