Hi there this is my first post(I made this account purely to get help with this, and probelbly more in the comeing weeks). I have the code set up and running to allow my bullets to fire however when I do I get a bullet shooting along the path it is meant to and a bullet that stays in place for until the bullet instance gets destoryed. I can’t figure out why. Any level of input would be very much apperciated.
Sorry if I have posted this incorrectly it my first time posting.
Within the Scene:
create() {
//-unrelated code-
this.projectiles = this.physics.add.group({
classType: Bullet,
maxSize: 1,
runChildUpdate: true,
});
}
update(time, delta) {
if (this.mouse.isDown && time > gameSetting.lastFired) {
if (gameSetting.playerIsDashing == true) {
return;
} else {
this.shoot(time);
}
}
}
shoot() {
let projectile = this.projectiles.get();
if (projectile) {
var bullet = new Bullet(this);
}
}
Now the bullet class:
class Bullet extends Phaser.Physics.Arcade.Sprite {
constructor(scene, x, y) {
super(scene, x, y, "bullet");
this.spawn = 0;
this.speed = 500;
this.setActive(true);
this.setVisible(true);
scene.add.existing(this);
this.play("bullet_anim");
scene.physics.world.enableBody(this);
this.setRotation(scene.player.rotation);
this.x = scene.player.x + 20 * Math.cos(this.rotation);
this.y = scene.player.y + 20 * Math.sin(this.rotation);
this.body.setVelocityX(this.speed * Math.cos((Math.PI * this.angle) / 180));
this.body.setVelocityY(this.speed * Math.sin((Math.PI * this.angle) / 180));
//scene.projectiles.add(this);
}
update(time, delta) {
console.log("this is working");
this.spawn += delta;
if (this.spawn > 1000) {
this.destroy(true);
}
}
}