# Enemy object not moving!

**URL:** <https://discourse.processing.org/t/enemy-object-not-moving/11814>\
**Category:** Coding Questions\
**Created:** [June 3, 2019, 9:15pm UTC](https://discourse.processing.org/t/enemy-object-not-moving/11814 "2019-06-03T21:15:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sc4p3g0a7](https://avatars.discourse-cdn.com/v4/letter/s/db5fbb/32.png) [@sc4p3g0a7](https://discourse.processing.org/u/sc4p3g0a7)\
**Post date:** [June 3, 2019, 9:15pm UTC](https://discourse.processing.org/t/enemy-object-not-moving/11814/1 "2019-06-03T21:15:45Z")

</div>

Hi. i decided one day to make the dinosaur game in the chrome browser. it all worked fine, until i made the cactus.(i suck at programming, so don’t judge) The problem is that he( the cactus) is not moving, even if i assigned a velocity, gravity, and a location.

here is the code for the cactus

```auto
PVector locat2;
PVector velocityObst;
PVector gravity2;

class Cactus 
{
  {
    locat2 = new PVector(1000, 500);
    gravity2 = new PVector(0, 0.2);
    velocityObst = new PVector(-5, 0);

    velocityObst.add(gravity2);

    locat2.add(velocityObst);

    if (locat2.y > 500) {
      locat2.y = 499;
    }

    stroke(255);
    strokeWeight(2);
    fill(0);
    rect(locat2.x, locat2.y, 39, 56);
  }
}

```

and if you need the code for the main character, here it is:

```auto
PVector location;
PVector velocity;  
PVector gravity;   
PVector upforce;

void setup() {
  size(1100, 600);
  location = new PVector(50, 500);
  velocity = new PVector(0, 0);
  gravity = new PVector(0,0.2);
  upforce = new PVector(0, -5.4); 
  
}

void jump() {
  location.add(upforce);
}

void draw() {
  background(255);
  
  new Cactus();

  location.add(velocity);

  velocity.add(gravity);

  if ((location.x > width) || (location.x < 0)) {
    velocity.x = 0;
  }

  if (location.y > 500) {
    velocity.y = 0;
  }
  
  stroke(255);
  strokeWeight(2);
  fill(127);
  rect(location.x, location.y, 39, 56);

  if (locat2.x == location.x){
    jump();
  }
 
  
  if (keyPressed) {
    if (key == ' ') {
      jump();
    }
  } else {
  }

}

```

---

<div class="post-metadata">

**Author:** ![sc4p3g0a7](https://avatars.discourse-cdn.com/v4/letter/s/db5fbb/32.png) [@sc4p3g0a7](https://discourse.processing.org/u/sc4p3g0a7)\
**Post date:** [June 3, 2019, 9:17pm UTC](https://discourse.processing.org/t/enemy-object-not-moving/11814/2 "2019-06-03T21:17:29Z")

</div>

by the way, i want the cactus to move toward the player, and not the other way around.

---

<div class="post-metadata">

**Author:** ![8BitRift](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/8bitrift/32/3495_2.png) [@8BitRift](https://discourse.processing.org/u/8BitRift)\
**Post date:** [June 3, 2019, 11:25pm UTC](https://discourse.processing.org/t/enemy-object-not-moving/11814/3 "2019-06-03T23:25:03Z")

</div>

Well keeping your code as similar as possible move the line locat2 outside the class so it looks like this:

```auto
PVector locat2 = new PVector(1000, 500);;
PVector velocityObst;
PVector gravity2;

class Cactus 
{
  {
    gravity2 = new PVector(0, 0.2);
    velocityObst = new PVector(-5, 0);

    velocityObst.add(gravity2);

    locat2.add(velocityObst);

    if (locat2.y > 500) {
      locat2.y = 499;
    }

    stroke(255);
    strokeWeight(2);
    fill(0);
    rect(locat2.x, locat2.y, 39, 56);
  }
}

```

Because every time you create a new class instance it still has the same location of the old one resulting in the cactus not moving at all.

---

<div class="post-metadata">

**Author:** ![sc4p3g0a7](https://avatars.discourse-cdn.com/v4/letter/s/db5fbb/32.png) [@sc4p3g0a7](https://discourse.processing.org/u/sc4p3g0a7)\
**Post date:** [June 3, 2019, 11:27pm UTC](https://discourse.processing.org/t/enemy-object-not-moving/11814/4 "2019-06-03T23:27:26Z")

</div>

i’m going try it out

---

<div class="post-metadata">

**Author:** ![sc4p3g0a7](https://avatars.discourse-cdn.com/v4/letter/s/db5fbb/32.png) [@sc4p3g0a7](https://discourse.processing.org/u/sc4p3g0a7)\
**Post date:** [June 3, 2019, 11:29pm UTC](https://discourse.processing.org/t/enemy-object-not-moving/11814/5 "2019-06-03T23:29:31Z")

</div>

thank you, you helped me

---

<div class="post-metadata">

**Author:** ![sc4p3g0a7](https://avatars.discourse-cdn.com/v4/letter/s/db5fbb/32.png) [@sc4p3g0a7](https://discourse.processing.org/u/sc4p3g0a7)\
**Post date:** [June 3, 2019, 11:31pm UTC](https://discourse.processing.org/t/enemy-object-not-moving/11814/6 "2019-06-03T23:31:21Z")

</div>

how do i close off a topic

---

<div class="post-metadata">

**Author:** ![sc4p3g0a7](https://avatars.discourse-cdn.com/v4/letter/s/db5fbb/32.png) [@sc4p3g0a7](https://discourse.processing.org/u/sc4p3g0a7)\
**Post date:** [June 3, 2019, 11:36pm UTC](https://discourse.processing.org/t/enemy-object-not-moving/11814/7 "2019-06-03T23:36:38Z")

</div>

how would i make a new cactus if the locat2.x == 250
