# I cant create a new Object when the old one hits a certain x value

**URL:** <https://discourse.processing.org/t/i-cant-create-a-new-object-when-the-old-one-hits-a-certain-x-value/11820>\
**Category:** Coding Questions\
**Created:** [June 4, 2019, 1:13am UTC](https://discourse.processing.org/t/i-cant-create-a-new-object-when-the-old-one-hits-a-certain-x-value/11820 "2019-06-04T01:13:07Z")\
**Posts on this page:** 5\
**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 4, 2019, 1:13am UTC](https://discourse.processing.org/t/i-cant-create-a-new-object-when-the-old-one-hits-a-certain-x-value/11820/1 "2019-06-04T01:13:07Z")

</div>

Hello, so i am making a game, and there are two main objects, there is you, and the obstacle.

i want to crate a new obstacle, when the previous one gets to a certain space,

Here is the code for the obstacle

```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);
  }
}

```

and here is the code for the main character:

```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 (keyPressed) {
    if (key == ' ') {
      jump();
    }
  } else {
  }

}

```

---

<div class="post-metadata">

**Author:** ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)\
**Post date:** [June 4, 2019, 3:55pm UTC](https://discourse.processing.org/t/i-cant-create-a-new-object-when-the-old-one-hits-a-certain-x-value/11820/2 "2019-06-04T15:55:31Z")

</div>

Hey There!

Rather than add one you can just return the one which just went off screen back to the start or something.

Here’s the code.

```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.x < 0) {
      locat2.x = width;
    }

    stroke(255);
    strokeWeight(2);
    fill(0);
    rect(locat2.x, locat2.y, 39, 56);
  }
}
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 (keyPressed) {
    if (key == ' ') {
      jump();
    }
  } else {
  }
}

```

So where in the cactus you have if it’s position is \< than 0 return back to width where it is the start of the screen on the right.  
( Also commented out the enemies velocity as this causes it to go off screen but you do whatever you like )

---

<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 25, 2019, 7:47pm UTC](https://discourse.processing.org/t/i-cant-create-a-new-object-when-the-old-one-hits-a-certain-x-value/11820/3 "2019-06-25T19:47:38Z")

</div>

sorry i have not responded, i was really caught up with school, visiting relatives, and trying to fix my desktop, which i might have accidentally reformated.

---

<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 25, 2019, 7:50pm UTC](https://discourse.processing.org/t/i-cant-create-a-new-object-when-the-old-one-hits-a-certain-x-value/11820/4 "2019-06-25T19:50:07Z")

</div>

Yes, but here is the problem, i want to be able to have multiple of them on the screen at once

---

<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:** [June 25, 2019, 8:19pm UTC](https://discourse.processing.org/t/i-cant-create-a-new-object-when-the-old-one-hits-a-certain-x-value/11820/5 "2019-06-25T20:19:20Z")

</div>

```auto

ArrayList<Cactus> cList = new ArrayList();

void setup() {
  size(1100, 600);

  for (int i=0; i<6; i++) {
    Cactus c = new Cactus(); 
    c.locat2 = new PVector(random(width), random(height));
    c.velocity = new PVector(0, 0);
    c.gravity = new PVector(0, 0.2);
    c.upforce = new PVector(0, -5.4);

    cList.add(c);
  }
}

void draw() {
  background(255);

  for (Cactus c : cList) { 
    c.run();
  }

  if (keyPressed) {
    if (key == ' ') {
      //jump();
    }
  } else {
  }
}

//===================================================================

class Cactus {
  PVector locat2 = new PVector(1000, 500);
  PVector velocityObst;
  PVector gravity2;

  // PVector location;
  PVector velocity;  
  PVector gravity;   
  PVector upforce;

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

    // velocityObst.add(gravity2);
  }

  void run() {

    //location.add(velocity);
    //velocity.add(gravity);

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

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

    locat2.add(velocityObst);

    if (locat2.x < 0) {
      locat2.x = width;
    }

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

  void jump() {
    //c.location.add(c.upforce);
  }
}
//===================================================================

```
