# Need help with making a bullet that shoots towards the mouse

**URL:** <https://discourse.processing.org/t/need-help-with-making-a-bullet-that-shoots-towards-the-mouse/40626>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [January 17, 2023, 10:30pm UTC](https://discourse.processing.org/t/need-help-with-making-a-bullet-that-shoots-towards-the-mouse/40626 "2023-01-17T22:30:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![UltimateDoug](https://avatars.discourse-cdn.com/v4/letter/u/ba9def/32.png) [@UltimateDoug](https://discourse.processing.org/u/UltimateDoug)\
**Post date:** [January 17, 2023, 10:30pm UTC](https://discourse.processing.org/t/need-help-with-making-a-bullet-that-shoots-towards-the-mouse/40626/1 "2023-01-17T22:30:56Z")

</div>

I’m making a zombie survival game and I’ve come across a problem when I was making my ArrayList of bullets. The bullets show up on the screen but wont move. With some trouble shooting it looks like the variables “d” and “t” stay null. Some help would be appreciated.

```auto
class Bullet
{
  PVector pos;
  PVector spd;
  float damage;
  PVector t = null;
  PVector d;

  //constructor
  Bullet (float x, float y)
  {
    pos = new PVector (x, y);
  }

  void update ()
  {
    display();
    move();
  }

  //methods
  void display ()
  {
    fill (#EDED54);
    ellipse (pos.x, pos.y, 7, 7);
  }
 
  void move ()
  { 
    if (t != null)
    {
      d = PVector.sub(t, pos);
      d.setMag(3);
      pos.add(d);
    }
  }

  boolean gone()
  {
    if (pos.y < 0)
      return true;

    return false;
  }
 
  void keyReleased ()
  {
    if (key == ' ')
    {
      t = new PVector(mouseX, mouseY);
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 17, 2023, 11:49pm UTC](https://discourse.processing.org/t/need-help-with-making-a-bullet-that-shoots-towards-the-mouse/40626/2 "2023-01-17T23:49:53Z")

</div>

> [@UltimateDoug](#):
>
> ```auto
> keyReleased ()
> {
> 
> ```

Won’t work inside the class. You need to call it from keyReleased outside the class

---

<div class="post-metadata">

**Author:** ![UltimateDoug](https://avatars.discourse-cdn.com/v4/letter/u/ba9def/32.png) [@UltimateDoug](https://discourse.processing.org/u/UltimateDoug)\
**Post date:** [January 18, 2023, 12:26am UTC](https://discourse.processing.org/t/need-help-with-making-a-bullet-that-shoots-towards-the-mouse/40626/3 "2023-01-18T00:26:45Z")

</div>

thanks man appreciate it.
