Hey,
I am making a top down shooter and I can’t find out why doesn’t the left button on mouse work properly when my character moves (WSAD).
Here https://ogamrima.github.io/index.html you can try holding any of the keys on keyboard and try to shoot with left mouse button, it is weird but the right one works okay for me.
Shooting works when the player is standing still and not holding any keys.
Oh and it works when you first start holding the left button and then use keys.
Here is code related to this problem
class GamePlay extends Phaser.Scene {
constructor() {
super({
key: "GamePlay",
});
this.w = 3200;
this.h = 2400;
this.speed = 400;
this.zombies = null;
this.bullets = null;
this.lastFired = 0;
//...
}
preload() {}
create() {
this.background = this.add.image(this.w / 2, this.h / 2, "background");
this.player = this.physics.add
.sprite(this.w / 2, this.h / 2, "player")
.setScale(0.25);
this.bullets = this.physics.add.group({
classType: Bullet,
runChildUpdate: true,
});
this.zombies = this.physics.add.group({
classType: Zombie,
runChildUpdate: true
})
this.player.setCollideWorldBounds();
this.physics.world.setBounds(0, 0, this.w, this.h);
this.cameras.main.zoom = 0.5;
this.cameras.main.startFollow(this.player, true);
this.cameras.main.setBounds(0, 0, this.w, this.h);
this.mouse = this.input.activePointer;
this.addZombies(1);
this.physics.add.collider(this.zombies);
this.physics.add.collider(this.bullets, this.zombies, this.zombieHit, null, this);
//...
this.moveKeys = this.input.keyboard.addKeys({
up: Phaser.Input.Keyboard.KeyCodes.W,
down: Phaser.Input.Keyboard.KeyCodes.S,
left: Phaser.Input.Keyboard.KeyCodes.A,
right: Phaser.Input.Keyboard.KeyCodes.D,
});
this.input.keyboard.on("keydown_W", (e) => {
this.player.setVelocityY(-this.speed);
});
this.input.keyboard.on("keydown_S", (e) => {
this.player.setVelocityY(this.speed);
});
this.input.keyboard.on("keydown_A", (e) => {
this.player.setVelocityX(-this.speed);
});
this.input.keyboard.on("keydown_D", (e) => {
this.player.setVelocityX(this.speed);
});
this.input.keyboard.on("keyup_W", (e) => {
if (this.moveKeys["down"].isUp) {
this.player.setVelocityY(0);
}
});
this.input.keyboard.on("keyup_S", (e) => {
if (this.moveKeys["up"].isUp) {
this.player.setVelocityY(0);
}
});
this.input.keyboard.on("keyup_A", (e) => {
if (this.moveKeys["right"].isUp) {
this.player.setVelocityX(0);
}
});
this.input.keyboard.on("keyup_D", (e) => {
if (this.moveKeys["left"].isUp) {
this.player.setVelocityX(0);
}
});
}
//...
shoot() {
let bullet = this.bullets.get().setActive(true).setVisible(true);
bullet.fire(this.player);
}
update(time, delta) {
//...
if (this.mouse.isDown && time > this.lastFired) {
if (this.player.active == false) return;
this.shoot();
this.lastFired = time + 100;
}
}
}