[SOLVED] Cant get my weapon to collide with my enemy

hey i’m trying to make a small tank game but i tried making my bullets as a weapon and now it wont collide with my enemies please help. This is the code for my tank game so far. Some part of my code is french tho like jeu is game but most is english.

var jeu = new Phaser.Game(1200, 600, Phaser.CANVAS, ‘’, { preload: chargement, create: creation, update: repetition });
var tank;
var enemieTank;
var jouer = true;
var fleches;
var backdrop;
var barrel;
var weapon;

function chargement () {
jeu.load.image(‘tank’, ‘ressources/images/tank160X120.png’);
jeu.load.image(‘enemie’, ‘ressources/images/enemie160X120.png’);
jeu.load.image(‘backdrop’, ‘ressources/images/backdrop.png’);
jeu.load.image(‘barrel’, ‘ressources/images/barrel.png’);
jeu.load.image(‘bullet’, ‘ressources/images/bullet.png’);
}

function creation () {
jeu.physics.startSystem(Phaser.Physics.ARCADE);

jeu.world.setBounds(0, 0, 4000, 600);

backdrop = jeu.add.sprite(2000, 300, 'backdrop');
barrel = jeu.add.sprite(231, 486, 'barrel');
enemieTank = jeu.add.sprite(1280, 475, 'enemie');


weapon = jeu.add.weapon(99, 'bullet');
weapon.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;
jeu.physics.enable(weapon, Phaser.Physics.ARCADE);
weapon.bulletSpeed = 500;
weapon.fireRate = 400;
weapon.rotation = jeu.physics.arcade.angleToPointer(weapon);
tank = this.add.sprite(220, 475, 'tank');
weapon.trackSprite(barrel, 0, 0, true);
jeu.input.activePointer.isDown = false;


backdrop.anchor.setTo(0.5,0.5);
tank.anchor.setTo(0.5,0.5);
barrel.anchor.setTo(0.5,0.5);
enemieTank.anchor.setTo(0.5,0.5);

jeu.physics.enable(tank, Phaser.Physics.ARCADE);
jeu.physics.enable(barrel, Phaser.Physics.ARCADE);
jeu.physics.enable(enemieTank, Phaser.Physics.ARCADE);

jeu.camera.follow(tank);

enemieTank.body.immovable = true; 

enemieTank.body.velocity.setTo(0, 0);

enemieTank.body.bounce.setTo(1, 1);

tank.body.collideWorldBounds = true;
enemieTank.body.collideWorldBounds = true; 

cleD = jeu.input.keyboard.addKey(Phaser.Keyboard.D);
cleA = jeu.input.keyboard.addKey(Phaser.Keyboard.A);   

}

function platformerFollow() {
jeu.camera.follow(tank, Phaser.Camera.FOLLOW_PLATFORMER);
style = ‘STYLE_PLATFORMER’;
}

function repetition () {
if (jouer){
//jeu.physics.arcade.collide(bullet, enemieTank);
//jeu.physics.arcade.overlap(bullet, enemieTank, bulletHitEnemy, null, this);
jeu.physics.arcade.collide(tank, enemieTank);
tank.body.velocity.setTo(0, 0);
barrel.rotation = jeu.physics.arcade.angleToPointer(barrel);

    barrel.body.x = tank.body.x + 8

    if (cleD.isDown){
        tank.body.velocity.x = 200;
    }
    else if (cleA.isDown){
        tank.body.velocity.x = -200;
    }
    if (jeu.input.activePointer.isDown){
        weapon.fire();
    }
    jeu.physics.arcade.overlap(weapon, enemieTank, collisionHandler, null, this);
}

}
function bulletHitEnemy (tank, bullet) {

bullet.kill();

}
function collisionHandler (weapon, enemieTank) {
weapon.kill();
enemieTank.kill();
}

There are a couple of problems here:

  1. You’re posting in a phaser3 forum but your code is using phaser2. You’re likely to get much better help if you post the question in the correct section of the discussion board.

  2. And, in your update function:

function repetition () {
  if (jouer){
    //jeu.physics.arcade.collide(bullet, enemieTank);
    //jeu.physics.arcade.overlap(bullet, enemieTank, bulletHitEnemy, null, this);
    jeu.physics.arcade.collide(tank, enemieTank);
    tank.body.velocity.setTo(0, 0);

You’re trying test for overlap between the enemy tank and bullet but bullet doesn’t exist – it hasn’t been defined as a variable anywhere so you’re actually saying “has this enemy tank collided with nothing.”

You’re using the Weapon plugin to handle bullet creation so you need to get the collection of bullets it is managing out of it in order to test if they are overlapping with an enemy tank. Based on the Weapon docs that should look something like: weaponBullets = weapon.bullets;. So now that you have the actual bullets you can do the check to see if they are colliding with a tank:

function repetition () {
  if (jouer){
    var weaponBullets = weapon.bullets;
    jeu.physics.arcade.overlap(weaponBullets, enemieTank, bulletHitEnemy, null, this);
    jeu.physics.arcade.collide(tank, enemieTank);
    tank.body.velocity.setTo(0, 0);
    ...

Hope that helps, it’s been a while since I worked on phaser v2 but this should get you started.

1 Like

OMG thank you so much i tried it out and it works i would have never thought of that and i didn’t notice that i was on the phaser 3 forum ill post my thing on phaser 2 forum.