class Example extends Phaser.Scene {
preload() {
this.load.image(
"bullet",
"https://assets.codepen.io/9367036/1bit-projectile.png"
);
this.load.image(
"ship",
"https://assets.codepen.io/9367036/1bit-player.png"
);
this.load.image(
"starfield",
"https://assets.codepen.io/9367036/background_1.png"
);
}
create() {
this.stars = this.add
.tileSprite(0, 0, config.width, config.height, "starfield")
.setOrigin(0, 0);
this.player = this.physics.add.sprite(256, config.height - 50, "ship");
this.player.setScale(2);
this.bullets = this.physics.add.group({
defaultKey: "bullet",
maxSize: 10
});
this.input.on("pointermove", (pointer) => {
this.player.x = pointer.worldX;
});
this.input.on("pointerdown", () => {
const bullet = this.bullets.get(this.player.x, this.player.y);
if (bullet) {
bullet.setActive(true);
bullet.setVisible(true);
bullet.setVelocity(0, -200);
}
});
}
update() {
this.bullets.children.each((bullet) => {
let cachee = !this.cameras.main.worldView.contains(bullet.x, bullet.y);
if (bullet.active && cachee) {
bullet.setActive(false);
bullet.setVisible(false);
}
});
}
}
const config = {
type: Phaser.AUTO,
width: 800,
height: 400,
pixelArt: true,
parent: "phaser-example",
physics: {
default: "arcade",
arcade: { debug: true }
},
scene: Example
};
const game = new Phaser.Game(config);
activateControls(["LMB"]);