Bouncing bullet

This is my code to move bullets

var Shoot = new Phaser.Class({

Extends: Phaser.GameObjects.Image,

initialize:

// Bullet Constructor
function Shoot (scene)
{
    Phaser.GameObjects.Image.call(this, scene, 0, 0, 'bubble');
    this.speed = 0.7;
    this.born = 0;
    this.direction = 0;
    this.xSpeed = 0;
    this.ySpeed = 0;
    this.setSize(12, 12, true);
},

// Fires a bullet from the player to the reticle
fire: function (shooter, target)
{
    this.setPosition(shooter.x, shooter.y); // Initial position
    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);
    }
}

});

I don’t have any idea how to reflect or set bullet to bounce if at left or right edge of screen/canvas.

Actually it’s a bubble shooter game.

How to do this ?

Hi William,

I would look into using Phaser’s arcade physics plug for this in as it vastly simplifies the physics coding. You don’t have to do all those manual calculations.

Regards,
Marc

Thank you!

I don’t know phaser 3 physics have this feature