# Random position of class objects

**URL:** <https://discourse.processing.org/t/random-position-of-class-objects/24950>\
**Category:** Coding Questions\
**Created:** [October 27, 2020, 4:05pm UTC](https://discourse.processing.org/t/random-position-of-class-objects/24950 "2020-10-27T16:05:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MaxMax](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@MaxMax](https://discourse.processing.org/u/MaxMax)\
**Post date:** [October 27, 2020, 4:05pm UTC](https://discourse.processing.org/t/random-position-of-class-objects/24950/1 "2020-10-27T16:05:37Z")

</div>

Hi, I managed to make my player ( the car ) collide with the obstacles that are falling down. I tried to make a class so I would be able to make the position, speed, and size of each obstacle random. But it doesn’t work and I don’t see why.

here is my code for the collision:  
if (dist(obstacleXPos, obstacleYPos, mouseX, y) \< 60) {  
background (0);  
noLoop();  
textSize(80);  
fill(#3279D8);  
text(“You lose!”, 180, height/2);  
}

This is the class I made:  
class ObstacleMovement {

int x;  
int y;  
int size;  
int obstacleXPos;

int speed;  
ObstacleMovement( int newX, int newY, int newSize, int newobstacleXPos, int newobstacleY, int newSpeed ) {  
x = newX;  
y = newY;  
size = newSize;  
obstacleXPos = newobstacleXPos;  
speed = newSpeed;  
}

void move() {  
obstacleYPos = obstacleYPos + speed;  
if (obstacleYPos \> height) {  
obstacleYPos = -20;  
}  
}

void display() {

fill (random(255), random(255), 255);  
rect(obstacleXPos, obstacleYPos, 65, 80); // obstacle rectangle  
//fill(255);  
//ellipse(obstacleXPos+400, obstacleYPos, 72, 72); // Obstacle circle

}

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

}

and this is how I create a new obstacle with the class:  
ObstacleMovement obstacle1 = new ObstacleMovement(100, 200, 80, 100, 10, 3);

If someone knows how to help me I would be very grateful

---

<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:** [October 27, 2020, 5:42pm UTC](https://discourse.processing.org/t/random-position-of-class-objects/24950/2 "2020-10-27T17:42:19Z")

</div>

> [@MaxMax](#):
>
> if (dist(obstacleXPos, obstacleYPos, mouseX, y) \< 60) {

here you don’t use the object obstacle1 that you derived from the class

in the class: what is the difference of x,y and obstacleXPos,obstacleYPos

Please show your **entire, run-able code**.

Chrisir

---

<div class="post-metadata">

**Author:** ![MaxMax](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@MaxMax](https://discourse.processing.org/u/MaxMax)\
**Post date:** [October 27, 2020, 5:44pm UTC](https://discourse.processing.org/t/random-position-of-class-objects/24950/3 "2020-10-27T17:44:10Z")

</div>

ObstacleMovement obstacle1 = new ObstacleMovement(100, 200, 80, 100, 10, 3);  
ObstacleMovement obstacle2 = new ObstacleMovement(100, 200, 30, 350, 10, 5);  
ObstacleMovement obstacle3 = new ObstacleMovement(100, 200, 30, 600, 10, 5);  
Car carmain = new Car();  
//SlowObjects slowobject1 = new SlowObjects(100, 300, 30, 240, 10, 9);  
StripeMovement stripe1 = new StripeMovement(1, 1, 1, 1);

float stripeYPos;  
float b;  
float obstacleYPos;  
float obstacleXPos;  
float obstaclePos;  
float obstacle1Pos;

float obstacleXxPos;  
float obstacleYyPos;

float size;  
float speed;

int x = 0;  
int y = 450;

void setup() {  
size (720, 600);  
obstacleYPos = -80;

obstacleXPos = 115;  
obstaclePos = 350;  
obstacle1Pos = 600;  
obstacleXxPos = 240;  
obstacleYyPos = -80;  
}

void keyPressed() {  
if ( key == ‘d’ ) {  
x = x + 50;  
}  
if ( key == ‘a’)  
x = x - 50;  
}

void draw () {  
background (#484646);  
smooth();  
strokeWeight(2);

line(240, 0, 240, 600);  
line(480, 0, 480, 600);

stripe1.run();  
obstacle1.run();  
obstacle2.run();  
obstacle3.run();  
carmain.run();  
[//slowobject1.run1](https://slowobject1.run1)();

if (dist(obstacleXPos, obstacleYPos, mouseX, y) \< 60) {  
background (0);  
noLoop();  
textSize(80);  
fill(#3279D8);  
text(“You lose!”, 180, height/2);  
}

if (dist(obstaclePos, obstacleYPos, mouseX, y) \< 60) {  
background (0);  
noLoop();  
textSize(80);  
fill(#3279D8);  
text(“You lose!”, 180, height/2);  
}  
if (dist(obstacle1Pos, obstacleYPos, mouseX, y) \< 60) {  
background (0);  
noLoop();  
textSize(80);  
fill(#3279D8);  
text(“You lose!”, 180, height/2);  
}

}`

---

<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:** [October 27, 2020, 9:13pm UTC](https://discourse.processing.org/t/random-position-of-class-objects/24950/4 "2020-10-27T21:13:39Z")

</div>

CODE not running, no car class, no stripe class…

* * *

**Remarks**

> [@MaxMax](#):
>
> float obstacleYPos;  
> float obstacleXPos;  
> float obstaclePos;  
> float obstacle1Pos;
> 
> float obstacleXxPos;  
> float obstacleYyPos;

this is before setup() - seems unnecessary since you have a class now!!

> [@MaxMax](#):
>
> obstacleYPos = -80;
> 
> obstacleXPos = 115;  
> obstaclePos = 350;  
> obstacle1Pos = 600;  
> obstacleXxPos = 240;  
> obstacleYyPos = -80;

this is in setup() - same thing!!!

* * *

\*\*Remark II \*\*

> if (dist(obstacleXPos, obstacleYPos, mouseX, y) \< 60) {

this is in draw, it must refer to the class like below

Please do the tutorial for classes: [Objects / Processing.org](https://www.processing.org/tutorials/objects/)

Chrisir

```auto

ObstacleMovement obstacle1 = new ObstacleMovement(100, 200, 10, 3);
ObstacleMovement obstacle2 = new ObstacleMovement(350, 200, 10, 5);
ObstacleMovement obstacle3 = new ObstacleMovement(600, 200, 10, 5);

// float b;

int x = 0;
int y = 450;

void setup() {
  size (720, 600);
}

void keyPressed() {
  if ( key == 'd' ) {
    x = x + 50;
  } else if ( key == 'a')
    x = x - 50;
}

void draw () {
  background (#484646);

  // CAR
  fill(255, 0, 0); 
  rect (mouseX, y, 15, 15); 

  smooth();
  strokeWeight(2);

  line(240, 0, 240, 600);
  line(480, 0, 480, 600);

  obstacle1.run();
  obstacle2.run();
  obstacle3.run();

  if (dist(obstacle1.x, obstacle1.y, mouseX, y) < 60) {
    background (0);
    noLoop();
    textSize(80);
    fill(#3279D8);
    text("You lose!", 180, height/2);
  }

  if (dist(obstacle2.x, obstacle2.y, mouseX, y) < 60) {
    background (0);
    noLoop();
    textSize(80);
    fill(#3279D8);
    text("You lose!", 180, height/2);
  }
  if (dist(obstacle3.x, obstacle3.y, mouseX, y) < 60) {
    background (0);
    noLoop();
    textSize(80);
    fill(#3279D8);
    text("You lose!", 180, height/2);
  }
}
// ===========================================================================

class ObstacleMovement {

  int x;
  int y;
  int size;
  color col1 = color ( random(255), random(255), 255);
  float type=random(1); 

  int speed;
  ObstacleMovement( int newX, int newY, 
    int newSize, 
    int newSpeed ) {
    x = newX;
    y = newY;
    size = newSize;
    speed = newSpeed;
  }

  void move() {
    y = y + speed;
    if (y > height) {
      y = -20;
    }
  }

  void display() {
    if (type>0.5) {
      fill (col1);
      rect(x, y, 
        65, 80); // obstacle rectangle
    } else {
      fill(0, 255, 0);
      ellipse(x, y, 72, 72); // Obstacle circle
    }
  }

  void run() {
    move();
    display();
  }
}//class
//

```
