Akamatsu Posted March 18, 2015 Share Posted March 18, 2015 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 More sharing options...
Akamatsu Posted March 19, 2015 Author Share Posted March 19, 2015 bump Link to comment Share on other sites More sharing options...
corpsefilth Posted March 20, 2015 Share Posted March 20, 2015 Hi, 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) {bullet.kill();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 More sharing options...
Nikow Posted March 20, 2015 Share Posted March 20, 2015 Do u try to test it with overlap function ? Link to comment Share on other sites More sharing options...
Akamatsu Posted March 22, 2015 Author Share Posted March 22, 2015 Thank you Corpsefilth i used : game.physics.arcade.overlap(mustang, bullet, yourFunction, null, this); but i changed "bullet" with "bullets" and now it's work! Link to comment Share on other sites More sharing options...
corpsefilth Posted March 22, 2015 Share Posted March 22, 2015 Great, I'm glad that helped. -- How do we set this topic as "answered". Link to comment Share on other sites More sharing options...
Recommended Posts