(Beginner) How do I make objects of different classes interact?

Hello, I started processing about one and a half weeks ago with the first goal being to make a really, really simple game. The goal is basically to jump over cars (so far only a car but that’s not the problem right now), however I don’t understand how to reset my players (class run) xpos to 0 again once he hits the car. As for the ‘sun’ and ‘moon’ part, please just skip that part, its just there to remind me to do it. Any help would be appreciated greatly, here is my code:

//Font
PFont f;

void setup() {
  size(1920,1080);
  //parameters (color, then xpos, ypos and speed)
  myob4 = new ob(color(#FF0808), 0, 900, 4); //low, red
  myob5 = new ob(color(#FF0808),0,900,4);   // in progress second OB
  myrun = new run(color(#6C00FC),0,1000,3,0); //runner (last value is yspeed)
  mysun = new sun(color(#FCD700),0,300,3,1); //sun (color, xpos, ypos, xspeed, yspeed)
  mysun1 = new sun(color(#C1BDB2),0,300,3,1); //moon

}

void draw() {
  background(#12D9FF);
  if (mouseButton == RIGHT) {
    background(#6F6E6E); }
  myob4.drive();
  myob4.display();
  myrun.drive();
  myrun.display();
  if(mouseButton == RIGHT) {
      mysun1.drive();
      mysun1.display(); }  
  else {
     mysun.drive();
     mysun.display();
  }
  fill(#5F5656);
  noStroke();
  rect(0,1200,4000,400);
  f = createFont("Times New Roman",64,true);
}

ob myob4;
ob myob5;
class ob {
  color c1;
  float xpos1;
  float ypos1;
  float xspeed1;
  
  //Constructor
  ob (color tempC1, float tempXpos1, float tempYpos1, float tempXspeed1) {
    c1 = tempC1;
    xpos1 = tempXpos1;
    ypos1 = tempYpos1;
    xspeed1 = tempXspeed1;
  }
  void display() {
    noStroke();
    fill(c1);
    rectMode(CENTER);
    rect(xpos1,ypos1,500,120);
    fill(0);
    ellipse(xpos1 - 170, ypos1 + 60, 100,100);
    ellipse(xpos1 + 170, ypos1 + 60, 100,100);  
    triangle(xpos1 - 140, ypos1 - 60, xpos1 - 70, ypos1 - 110, xpos1 - 70, ypos1 - 60);
}
  
  void drive() {
    xpos1 = xpos1 - xspeed1;
    if (xpos1 < 0) {
      xpos1 = random(1920,2400);
      String xposition = "Position of the obstacle is " + xpos1;
      println(xposition);  
      c1 = (color(random(255), random(255), random(255)));
    }
  }
}
//Runner
run myrun;
class run {
  color c2;
  float xpos2;
  float ypos2;
  float xspeed2;
  int yspeed2;
  
  //Constructor
  run (color tempC2, float tempXpos2, float tempYpos2, float tempXspeed2, int tempYspeed2) {
    c2 = tempC2;
    xpos2 = tempXpos2;
    ypos2 = tempYpos2;
    xspeed2 = tempXspeed2;
    yspeed2 = tempYspeed2;
  }
  void display() {
    stroke(0);
    fill(c2);
    rectMode(CENTER);
    rect(xpos2,ypos2,50,50);
  }
  void drive() {
    xpos2 = xpos2;
    ypos2 = ypos2 - yspeed2;
    if (ypos2 < 600) {
      ypos2 = 1000;
    }
    if (ypos2 > 1080) {
      ypos2 = 1000;
    }
    if ((keyPressed == true) && (key == 'w')) {
      yspeed2 = 2;
    }
    if ((keyPressed == true) && (key == 's')) {
      yspeed2 = -2;
    }      
    if (yspeed2 > 0) {
      textAlign(CENTER);
      textSize(100);
      text("Going up",width/2,height/2); //xpos (half of 1920), ypos
    }
    if (yspeed2 < 0) {
      textAlign(CENTER);
      textSize(100);
      text("Going Down",width/2,height/2);
    }
  }
}
//sun
sun mysun;
sun mysun1;
class sun {
  color c3;
  float xpos3;
  float ypos3;
  int xspeed3;
  int yspeed3;
  
  //Constructor
  sun (color tempC3, float tempXpos3, float tempYpos3, int tempXspeed3, int tempYspeed3) {
    c3 = tempC3;
    xpos3 = tempXpos3;
    ypos3 = tempYpos3;
    xspeed3 = tempXspeed3;
    yspeed3 = tempYspeed3;
  }
  void display() {
    stroke(c3);
    fill(c3);
    ellipse(xpos3,ypos3,150,150);
  }
  void drive() {
    xpos3 = xpos3 + xspeed3;
    ypos3 = ypos3 - yspeed3;
    if (xpos3 > 1920) {
      xpos3 = 0;
    }
    if (xpos3 > width/2) {
      yspeed3 = -1;
    }
    if(xpos3 < width/2) {
      yspeed3 = 1;
    }
  }
}
1 Like

Nice Sketch!

Outside the class it’s with the dot . :

reset xpos2:

myrun.xpos2=0;


check the player against the car , e.g. in draw:

  if (myrun.xpos2+50>myob4.xpos1-170 && 
    myrun.ypos2>myob4.ypos1 - 30) {
    println("Hit");
    myob4.xpos1=width-177;
  }

And a warm welcome to the forum!

Regards, Chrisir

2 Likes

this line belongs into setup() :

f = createFont("Times New Roman", 64, true);


this line does nothing:

xpos2 = xpos2;


this line

if ((keyPressed == true) && (key == 'w')) {

is the same as

if ((keyPressed) && (key == 'w')) {

2 Likes