Hi all,
What I want is to create a pool of 30 projectiles at the cannon’s location, the projectiles should be hidden and their bodies disabled. Then when one bullet is fired, its visibility and its body should be enabled.
I managed to do all of this, the only issue I have is that the player collides with the 30 bullets hidden at the cannon’s location. I assume their bodies aren’t properly disabled (visibility is).
In Play.js create multiple cannons from Tiled objects
createCannons() {
this.bulletsLayer.forEach(object => {
var bulletGroup = new BulletGroup({
'world': this.physics.world,
'scene': this,
'x': object.x,
'y': object.y,
});
this.physics.add.overlap(this.player, bulletGroup, this.onBulletHitPlayer, false, this);
});
}
BulletGroup.js
import Bullet from "../prefabs/Bullet.js"
export default class BulletGroup extends Phaser.Physics.Arcade.Group
{
constructor(settings) {
this.createMultiple({
classType: Bullet,
frameQuantity: 30,
active: false,
visible: false,
...
});
}
fireBullet()
{
const bullet = this.getFirstDead();
if (bullet) {
bullet.fire();
}
}
}
Bullet.js
export default class Bullet extends Phaser.Physics.Arcade.Sprite
{
constructor (scene,x,y,key,frame)
{
super(scene,x,y,key,frame);
scene.add.existing(this);
//the code below doesn't solve the issue
// this.scene.physics.world.enable(this); //allow physics to move the container and its child
// this.body.setSize(5, 5, true);
// this.setActive(false);
// this.setVisible(false);
// this.body.enable = false;
}
fire()
{
this.scene.physics.world.enable(this); //allow physics to move the container and its child
this.setActive(true);
this.setVisible(true);
this.body.enable = true;
this.body.setSize(5, 5, true);
//...
}