0
votes

I was hoping that I could get some help with the problem that I'm having right now.

Basically I have some bullets, and then I have a group of enemies that move with velocity. The enemies have different sprites and basically I want to assign a value per sprite type. It works, for example, if I say if a diamond is generated and collides with a bullet you get 10 points, but the strange behavior is when it collides, since all enemies on screen give the 10 points and they all get destroyed, not just the one.

Also if the 10 point sprite is not on screen, no points are given which is normal.

Please find my code below and I would appreciate any help given. Thanks.

//here is my bullets group
createBullets: function(){
    //Bullets
    this.bullets = this.add.group();
    this.bullets.enableBody = true;
    this.bullets.physicsBodyType = Phaser.Physics.P2JS;
    this.bullets.createMultiple(500, 'bullet', 0, false);
    this.bullets.setAll('anchor.x', 0.5);
    this.bullets.setAll('anchor.y', 0.5);
    this.bullets.setAll('outOfBoundsKill', true);
    this.bullets.setAll('checkWorldBounds', true);    
},

///here are my enemies
addOneFigura: function(x, y) {
    this.figuras = this.add.group();
    //these are sprites
    this.figuritas = ["figura1","figura2","figura3","figura4","figura5","figura6"];
    this.figurita = this.figuritas[Math.floor(Math.random()*6)];
    this.figura = this.add.sprite(x,y,this.figurita);
    this.figuras.add(this.figura);
    this.physics.p2.enable(this.figura, false);
    this.figura.body.velocity.y = 75;
    this.figura.checkWorldBounds = true;
    this.figura.outOfBoundsKill = true;

    this.figura.body.setCollisionGroup(this.figurasCG);
    this.figura.body.collides(this.bulletsCG, this.collisionBulletMatch, this);
},

//and lastly this is my collision function
collisionBulletMatch: function(figurita, figuritapega) {

   if (this.figurita != this.figuritapega){
       this.score += 10;
       this.scoreText.text = this.score;
       this.resetFigura();
   }
}

So basically when they collide the whole figuras group disappears instead of just the one colliding.

1
What's your resetFigura function look like?James Skemp
looks like that: resetFigura: function(figura) { this.figura.kill(this.figura); }, thanks for your responseRafahc

1 Answers

1
votes

I'll readily admit I haven't done much with P2 physics in Phaser, but the immediate thing that comes to mind is that while you're calling resetFigura you're not passing in a figure (figura). Without seeing the initialization of the variable I can't be sure, but I think you want to change your function to something like the following:

resetFigura: function(figura) {
    figura.sprite.kill();
},

This should be called via this.resetFigura(figuritapega); in collisionBulletMatch.

If it's not in the resetFigura call, I would also recommend taking a look at the official P2 Physics collision group example; I was able to get that up and running fairly quickly. It does have a single player character, but if you simplify your bullets to one you might be able to figure out your issue.