Hello, I am having trouble creating a collision. When the bullets meet the enemy object, it is supposed to be destroyed, but it isn’t working. Can you help me?
Main
import * as Phaser from 'phaserjs';
import {Player} from './../script/player.js';
import {Bullet} from './../script/bullet.js';
import {Enemy} from './../script/Enemy.js';
let player;
let bullets;
let enemy;
export class chapter0 extends Phaser.Scene {
constructor() {
super({
key: 'chapter0'
});
}
init() {
this.screenWidth = this.game.config.width;
this.screenHeight = this.game.config.height;
}
preload() {
this.load.image("dragon", "/assets/image/dragon.png");
this.load.image("bullet", "/assets/image/laser.png");
}
create() {
player = new Player(this, 800, 300, 'dragon'); // Adjust x, y, and texture as needed
bullets = new Bullet(this, 'bullet', player); // Adjust x, y, and texture as needed
enemy = new Enemy(this, 'dragon'); // Adjust x, y, and texture as needed
// this.physics.world.enable([player, enemy]);
this.physics.add.overlap(bullets, enemy,this.destroyEnemy, null, this);
}
destroyEnemy(bullets, circle) {
// Destroy the enemy when player overlaps with it
console.log("Destroy")
circle.destroy();
this.physics.world.disable(circle); // Disable physics for the graphics object
circle.body.destroy();
}
update() {
player.update();
this.physics.world.overlap(bullets, enemy, (dragoon, note) => {
// this.dragoonTween[0].stop()
// this.queenTween[0].stop()
console.log(1)
// score = score + 10
});
}
}
Bullet
import * as Phaser from 'phaserjs';
let bullets;
let scenes;
let lastPressedDirection = true;
var shootingDelay = 500; // 1000 milliseconds (1 second)
let player;
var lastShotTime = 0;
let destroyTimer;
export class Bullet extends Phaser.GameObjects.Sprite {
constructor(scene, texture, pl) {
// Call the super constructor before accessing 'this' or returning
player = pl
super(scene, 0, 0, texture);
scenes = scene;
bullets = scenes.physics.add.group({
defaultKey: texture,
});
scenes.input.keyboard.on('keydown', function (event) {
if (
(event.code === 'Enter' || event.key === 'Enter' || event.keyCode === 13) &&
scenes.time.now - lastShotTime > shootingDelay
) {
this.shootBullet(Phaser.Math.DegToRad(player.posBullet));
lastShotTime = scenes.time.now;
}
}, this);
destroyTimer = scene.time.addEvent({
delay: 10000, // 10 seconds in milliseconds
callback: this.destroyBullets,
callbackScope: this,
loop: true // Set to true to make the timer repeat
});
}
destroyBullets() {
bullets.getChildren().forEach(bullet => {
if (bullet.active) {
bullet.setActive(false);
bullet.setVisible(false);
bullet.destroy();
}
});
}
shootBullet(angle) {
var bullet = bullets.get(player.x, player.y);
if (bullet) {
bullet.setActive(true);
bullet.setVisible(true);
bullet.setScale(0.06);
bullet.rotation = angle;
scenes.physics.velocityFromAngle(angle * Phaser.Math.RAD_TO_DEG, 500, bullet.body.velocity);
}
}
}
Enemy
import * as Phaser from 'phaserjs';
let scenes;
export class Enemy extends Phaser.GameObjects.Sprite {
constructor(scene, texture) {
// Call the super constructor before accessing 'this' or returning
// player = pl
super(scene, 100, 100, texture);
// scene.physics.add.existing(texture);WW
// Add the sprite to the scene
scenes = scene;
this.enemies = scene.physics.add.staticGroup({
key: texture,
maxSize: 2, // Adjust as needed
setXY: { x: 300, y: 200, stepX: 100 } // Adjust the initial positions of enemies
});
// Offset the body to match the drawn circle
}
}