0
votes

Hi I'm trying to make my bullets collide and then die on collision with my blocks but as soon as I try to setCollisionGroup I get the following error:

Uncaught TypeError: Cannot read property 'mask' of undefined

and it points me to this code:

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);    
    this.bullets.forEach(function(bullet){
      bullet.body.setCollisionGroup(this.bulletsCG);  <-------This line
      bullet.body.collides(this.bloquesCG);
    });
},

////...............this below is my other colliding group:

addOneBloque: function(x, y) {
    this.bloque = this.add.sprite(x,y,'bloque');
    this.physics.p2.enable(this.bloque, false);
    this.bloque.body.velocity.x = -200;
    this.bloque.checkWorldBounds = true;
    this.bloque.outOfBoundsKill = true;
    this.bloque.body.setCollisionGroup(this.bloquesCG);
    this.bloque.body.collides(this.bulletsCG, this.resetBullet, this);
},

Everything works until I try to setCollisionGroups then everything breaks. Any help would be greatly appreciated.

1
What's your code that defines bulletsCG?James Skemp
this.bulletsCG = this.game.physics.p2.createCollisionGroup(); this.bloquesCG = this.game.physics.p2.createCollisionGroup();Rafahc
Full error here: Uncaught TypeError: Cannot read property 'mask' of undefined phaser.js:87525c.Physics.P2.Body.setCollisionGroup phaser.js:87525(anonymous function) level1.js:120c.Group.forEach phaser.js:32664BasicGame.Game.createBullets level1.js:119BasicGame.Game.create level1.js:57c.StateManager.loadComplete phaser.js:29240c.Loader.finishedLoading phaser.js:71446c.Loader.processLoadQueue phaser.js:71403c.Loader.asyncComplete phaser.js:71476c.Loader.fileComplete phaser.js:72323a.data.onloadRafahc

1 Answers

0
votes

I was declaring in the wrong place thanks man.