# Any ideas why my code is spawning a 'shadow bullet'

**URL:** <https://phaser.discourse.group/t/any-ideas-why-my-code-is-spawning-a-shadow-bullet/10949>\
**Category:** Phaser 3\
**Created:** [January 1, 2022, 4:18pm UTC](https://phaser.discourse.group/t/any-ideas-why-my-code-is-spawning-a-shadow-bullet/10949 "2022-01-01T16:18:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![E-C-Carter](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/e-c-carter/32/6051_2.png) [@E-C-Carter](https://phaser.discourse.group/u/E-C-Carter)\
**Post date:** [January 1, 2022, 4:18pm UTC](https://phaser.discourse.group/t/any-ideas-why-my-code-is-spawning-a-shadow-bullet/10949/1 "2022-01-01T16:18:53Z")

</div>

Hi there this is my first post(I made this account purely to get help with this, and probelbly more in the comeing weeks). I have the code set up and running to allow my bullets to fire however when I do I get a bullet shooting along the path it is meant to and a bullet that stays in place for until the bullet instance gets destoryed. I can’t figure out why. Any level of input would be very much apperciated.

Sorry if I have posted this incorrectly it my first time posting.

Within the Scene:

```javascript
  create() {
    //-unrelated code-
    this.projectiles = this.physics.add.group({
      classType: Bullet,
      maxSize: 1,
      runChildUpdate: true,
    });
  }

  update(time, delta) {
    if (this.mouse.isDown && time > gameSetting.lastFired) {
      if (gameSetting.playerIsDashing == true) {
        return;
      } else {
        this.shoot(time);
      }
    }
  }

  shoot() {
    let projectile = this.projectiles.get();

    if (projectile) {
      var bullet = new Bullet(this);
    }
  }

```

Now the bullet class:

```javascript
class Bullet extends Phaser.Physics.Arcade.Sprite {
  constructor(scene, x, y) {
    super(scene, x, y, "bullet");

    this.spawn = 0;
    this.speed = 500;
    this.setActive(true);
    this.setVisible(true);
    scene.add.existing(this);
    this.play("bullet_anim");
    scene.physics.world.enableBody(this);
    this.setRotation(scene.player.rotation);
    this.x = scene.player.x + 20 * Math.cos(this.rotation);
    this.y = scene.player.y + 20 * Math.sin(this.rotation);
    this.body.setVelocityX(this.speed * Math.cos((Math.PI * this.angle) / 180));
    this.body.setVelocityY(this.speed * Math.sin((Math.PI * this.angle) / 180));
    //scene.projectiles.add(this);
  }

  update(time, delta) {
    console.log("this is working");

    this.spawn += delta;

    if (this.spawn > 1000) {
      this.destroy(true);
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [January 1, 2022, 7:17pm UTC](https://phaser.discourse.group/t/any-ideas-why-my-code-is-spawning-a-shadow-bullet/10949/2 "2022-01-01T19:17:35Z")

</div>

👋

I think one is `projectile` and one is `bullet`. If you want to use `this.projectiles` (physics group) then remove

```
var bullet = new Bullet(this);

```

> [@E-C-Carter](#):
>
> ```javascript
> this.setRotation(scene.player.rotation);
> this.x = scene.player.x + 20 * Math.cos(this.rotation);
> this.y = scene.player.y + 20 * Math.sin(this.rotation);
> this.body.setVelocityX(this.speed * Math.cos((Math.PI * this.angle) / 180));
> this.body.setVelocityY(this.speed * Math.sin((Math.PI * this.angle) / 180));
> 
> ```

Remove these from the `Bullet` constructor and put them in a new method, e.g. `Bullet#shoot()`. Then call it as `projectile.shoot()`.

---

<div class="post-metadata">

**Author:** ![E-C-Carter](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/e-c-carter/32/6051_2.png) [@E-C-Carter](https://phaser.discourse.group/u/E-C-Carter)\
**Post date:** [January 3, 2022, 3:19pm UTC](https://phaser.discourse.group/t/any-ideas-why-my-code-is-spawning-a-shadow-bullet/10949/3 "2022-01-03T15:19:12Z")

</div>

Hi there thanks for the response. I was in the process of writing a post about how this was throwing errors back at me but I was not sure what I had done wrong. I forgot to refernece the scene in the method.Coding does nto come easy to me.  
I have another question, if your willing to awnser(don’t worry if not you have already helped me). I would like the bullets that are spawned to bounce of the edge of the map. How would I do this?

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [January 3, 2022, 4:48pm UTC](https://phaser.discourse.group/t/any-ideas-why-my-code-is-spawning-a-shadow-bullet/10949/4 "2022-01-03T16:48:45Z")

</div>

In `add.group({…})` add `collideWorldBounds: true, bounceX: 1, bounceY: 1`.

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [January 3, 2022, 6:18pm UTC](https://phaser.discourse.group/t/any-ideas-why-my-code-is-spawning-a-shadow-bullet/10949/5 "2022-01-03T18:18:12Z")

</div>

A post was split to a new topic: [How would I create collisions between these and the bullets](https://phaser.discourse.group/t/how-would-i-create-collisions-between-these-and-the-bullets/10964)
