I am trying to have my bullets collide with my tilemap. I am able to have my player collide with the tilemap but I can’t seem to figure out how I can get my bullets to collide with the tilemap. My bullets will collide with other objects as well.
Bullet Class:
var Bullet = new Phaser.Class({
Extends: Phaser.GameObjects.Image,
initialize:
function Bullet (scene){
Phaser.GameObjects.Image.call(this, scene, 0, 0, 'bullet');
this.speed = 0.15;
this.born = 0;
this.direction = 0;
this.xSpeed = 0;
this.ySpeed = 0;
this.setScale(0.05)
},
fire: function (shooter, target){
this.setPosition(shooter.x, shooter.y);
this.direction = Math.atan( (target.x-this.x) / (target.y-this.y));
// Calculate X and y velocity of bullet to moves it from shooter to target
if (target.y >= this.y)
{
this.xSpeed = this.speed*Math.sin(this.direction);
this.ySpeed = this.speed*Math.cos(this.direction);
}
else
{
this.xSpeed = -this.speed*Math.sin(this.direction);
this.ySpeed = -this.speed*Math.cos(this.direction);
}
this.rotation = shooter.rotation; // angle bullet with shooters rotation
this.born = 0; // Time since new bullet spawned
},
// Updates the position of the bullet each cycle
update: function (time, delta)
{
this.x += this.xSpeed * delta;
this.y += this.ySpeed * delta;
this.born += delta;
if (this.born > 1800)
{
this.setActive(false);
this.setVisible(false);
}
}
});
Creating a class to use the bullets:
export default class PistolBulletClass {
preload(scene){
scene.load.image('bullet', Bullet)
}
create(scene){
this.playerBullets = scene.physics.add.group({classType: Bullets, runChildUpdate: true});
}
this.physics.add.collider(this.Player.player, this.Map.ground)
this.physics.add.collider(this.Player.brick, this.Bullet.playerBullets, hitBrick);
this.physics.add.collider(this.Bullet.playerBullets, this.Map.ground, hitWall)
The first two collision lines work and the last one doesn’t. Any help is appreciated! (Player.player is a physics image and Player.brick is a staticGroup)