How can I make multiple objects of the same class to execute the same method at the same time?

Hello, I am working on a 3D game and in the world of the game are trees. The character class has a method, where its xPos, yPos and zPos are made equal with the variables ax, by and cz, which count for the whole program. The tree class has a method, where it feels if ax or cz are in the area around the tree xPos and zPos, and when this is true, it sets a variable called “bouncer” to 5. Normally bouncer is 0. The program then makes the character class execute a “bounceBack” method, which makes the character to bounce back, so that she can not go through the trees.
The problem is, that one tree can make the character bounce back, but when I create multiple trees, it only makes the last tree change the bouncer variable. So how can I make multiple objects of the same class do the same method at the same time?
The Main Program:

Startscreen startscreen;
Helpmenu helpmenu;
Controlmenu controlmenu;
Part1 part1;
float part=0;
void setup()
{
  size(1300,700,P3D);
  startscreen=new Startscreen(#000000,#000000,#000000);
  helpmenu=new Helpmenu(#000000,#000000,#000000);
  controlmenu=new Controlmenu(#B84EC6);
  part1=new Part1();
}
void draw()
{
  if(starter==0)
  {
  startscreen.createStartscreen();
  startscreen.change();
  startscreen.decoration();
  }
  if(help==5)
  {
    helpmenu.createHelpmenu();
    helpmenu.change();
  }
  if(help==10)
  {
    controlmenu.createControlmenu();
    controlmenu.change();
  }
  if(part==0&starter==5&help==0)
  {
    part1.createPart1();
  }
}
void keyPressed()
{
  helpmenu.callhelp();
  if(part==0&starter==5&help==0)
  {
    part1.Part1keyPressed();
  }
}
void keyReleased()
{
  if(part==0&starter==5&help==0)
  {
    part1.Part1keyReleased();
  }
}
void mousePressed()
{
  if(starter==0)
  {
    startscreen.Play();
    startscreen.Delete();
    startscreen.Exit();
  }
  if(help==5)
  {
    helpmenu.backgame();
    helpmenu.backmain();
    helpmenu.Controls();
  }
  if(help==10)
  {
    controlmenu.backgame();
  }
  if(part==0&starter==5&help==0)
  {
    part1.Part1mousePressed();
  }
}
void mouseReleased()
{
  if(part==0&starter==5&help==0)
  {
    part1.Part1mouseReleased();
  }
}
void mouseMoved()
{
  if(part==0&starter==5&help==0)
  {
    part1.Part1mouseMoved();
  }
}

The part 1 tab:

class Part1
{
  Marcy marcy;
  Campfire campfire;
  Tree t1;
  Tree t2;
  Tree t3;
  Tree t4;
  Tree t5;
  
  Part1()
  {
    marcy=new Marcy(650,440,0,0,0,0,640);
    campfire=new Campfire(650,-100);
    t1=new Tree(300,300);
    t2=new Tree(600,1000);
    t3=new Tree(650,1500);
    t4=new Tree(-400,1250);
    t5=new Tree(600,2000);
  }
  void createPart1()
  {
    ambientLight(0,0,0);
    pointLight(brightlight,brightlight,brightlight,xLight,yLight,zLight);
    background(#0D053B);
    pushMatrix();
    translate(650,650,0);
    fill(#114310);
    box(4000,20,4000);
    popMatrix();
    pushMatrix();
    translate(650,650,4000);
    fill(#114310);
    box(4000,20,4000);
    popMatrix();
    pushMatrix();
    translate(650,650,8000);
    fill(#114310);
    box(4000,20,4000);
    popMatrix();
    pushMatrix();
    translate(650,650,12000);
    fill(#114310);
    box(4000,20,4000);
    popMatrix();
    pushMatrix();
    translate(650,650,19000);
    fill(#114310);
    box(4000,20,10000);
    popMatrix();
    fill(#A09E9C);
    beginShape();
    vertex(-1350,640,-2000);
    vertex(2650,640,-2000);
    vertex(2650,100,-2000);
    vertex(-1350,100,-2000);
    endShape();
    beginShape();
    vertex(-1350,640,24000);
    vertex(2650,640,24000);
    vertex(2650,100,24000);
    vertex(-1350,100,24000);
    endShape();
    beginShape();
    vertex(-1350,640,-2000);
    vertex(-1350,640,24000);
    vertex(-1350,100,24000);
    vertex(-1350,100,-2000);
    endShape();
    beginShape();
    vertex(2650,640,-2000);
    vertex(2650,640,24000);
    vertex(2650,100,24000);
    vertex(2650,100,-2000);
    endShape();
    campfire.drawCampfire();
    marcy.drawMarcy();
    marcy.moveMarcy();
    marcy.gravity();
    marcy.cam();
    marcy.jump();
    marcy.flashlight();
    marcy.sprint();
    marcy.moveLegs();
    t1.drawTree();
    t2.drawTree();
    t3.drawTree();
    t4.drawTree();
    t5.drawTree();
    if((ax<-1350||ax>2650)||(cz<-2000||cz>24000))
    {
      marcy.bounceBack();
    }
    if(bouncer==true)
    {
      marcy.bounceBack();
    }
  }
  void Part1keyPressed()
  {
    marcy.pressedkeys();
  }
  void Part1keyReleased()
  {
    marcy.releasedkeys();
  }
  void Part1mousePressed()
  {
  }
  void Part1mouseReleased()
  {
  }
  void Part1mouseMoved()
  {
    marcy.mouseturn();
  }
}

The protagonist tab(sorry for the many shapes and variables):

float a1=0;
float c1=0;
float cam1=0;
float jump1=0;
float sprinter=0;
float stamina=100;
float jumpfactor=-5;
float mouseturnX=0;
float mouseturnY=0;
float camdist=200;
float brightlight=0;
float lightcount=0;
float xLight=0;
float yLight=0;
float zLight=0;
float ax=0;
float by=0;
float cz=0;
float knee1=12.5;
float m=5;
float knee2=12.5;
float n=5;
float ankle1=200;
float w=-5;
float ankle2=200;
float v=-5;
float legswitch=0;
class Marcy
{
  float xPos;
  float yPos;
  float zPos;
  float xCam;
  float yCam;
  float zCam;
  float floor;
  
  Marcy(float x,float y,float z,float xc,float yc,float zc,float f)
  {
    xPos=x;
    yPos=y;
    zPos=z;
    xCam=xc;
    yCam=yc;
    zCam=zc;
    floor=f;
  }
  void drawMarcy()
  {
    ax=xPos;
    by=yPos;
    cz=zPos;
    pushMatrix();
    translate(xPos,yPos,zPos);
    if(c1!=0||a1!=0)
    {rotateY(mouseturnX);}
    beginShape();
    fill(#FAEABD);//head
    vertex(0,0,0);
    vertex(0,30,0);
    vertex(20,0,-10);
    endShape();
    beginShape();
    vertex(0,0,0);
    vertex(0,30,0);
    vertex(-20,0,-10);
    endShape();
    beginShape();
    vertex(0,0,0);
    vertex(0,-20,0);
    vertex(20,0,-10);
    endShape();
    beginShape();
    vertex(0,0,0);
    vertex(0,-20,0);
    vertex(-20,0,-10);
    endShape();
    beginShape();
    vertex(0,30,0);
    vertex(-20,0,-10);
    vertex(20,0,-10);
    endShape();
    beginShape();//neck
    vertex(0,0,-10);
    vertex(10,50,-10);
    vertex(-10,50,-10);
    endShape();
    beginShape();
    vertex(0,0,-5);
    vertex(10,50,-5);
    vertex(-10,50,-5);
    endShape();
    beginShape();
    vertex(-10,50,-5);
    vertex(0,0,-5);
    vertex(0,0,-10);
    vertex(-10,50,-10);
    endShape();
    beginShape();
    vertex(10,50,-5);
    vertex(0,0,-5);
    vertex(0,0,-10);
    vertex(10,50,-10);
    endShape();
    beginShape();//nose
    vertex(0,0,0);
    vertex(0,15,7.5);
    vertex(0,15,0);
    endShape();
    fill(#EFF05C);//hair
    beginShape();
    vertex(0,-20,0);
    vertex(0,-25,0);
    vertex(30,10,-15);
    endShape();
    beginShape();
    vertex(0,-20,0);
    vertex(0,-25,0);
    vertex(-30,10,-15);
    endShape();
    beginShape();
    vertex(0,-25,0);
    vertex(-20,0,-10);
    vertex(20,0,-10);
    endShape();
    beginShape();
    vertex(20,0,-10);
    vertex(0,15,-15);
    vertex(-20,0,-10);
    endShape();
    beginShape();
    vertex(-5,12.5,-12.5);
    vertex(5,12.5,-12.5);
    vertex(0,60,-15);
    endShape();
    fill(#1D7116);//Pullover
    beginShape();
    vertex(40,40,-7.5);
    vertex(-40,40,-7.5);
    vertex(-40,60,-27.5);
    vertex(40,60,-27.5);
    endShape();
    beginShape();
    vertex(40,40,-7.5);
    vertex(-40,40,-7.5);
    vertex(-40,60,12.5);
    vertex(40,60,12.5);
    endShape();
    beginShape();
    vertex(40,60,12.5);
    vertex(-40,60,12.5);
    vertex(-20,80,2.5);
    vertex(20,80,2.5);
    endShape();
    beginShape();
    vertex(40,60,-27.5);
    vertex(-40,60,-27.5);
    vertex(-20,80,-17.5);
    vertex(20,80,-17.5);
    endShape();
    beginShape();
    vertex(40,40,-7.5);
    vertex(40,60,12.5);
    vertex(40,60,-27.5);
    endShape();
    beginShape();
    vertex(-40,40,-7.5);
    vertex(-40,60,12.5);
    vertex(-40,60,-27.5);
    endShape();
    beginShape();
    vertex(40,60,12.5);
    vertex(40,60,-27.5);
    vertex(20,80,-17.5);
    vertex(20,80,2.5);
    endShape();
    beginShape();
    vertex(-40,60,12.5);
    vertex(-40,60,-27.5);
    vertex(-20,80,-17.5);
    vertex(-20,80,2.5);
    endShape();
    beginShape();
    vertex(-20,80,2.5);
    vertex(20,80,2.5);
    vertex(40,100,12.5);
    vertex(-40,100,12.5);
    endShape();
    beginShape();
    vertex(-20,80,-17.5);
    vertex(20,80,-17.5);
    vertex(40,100,-27.5);
    vertex(-40,100,-27.5);
    endShape();
    beginShape();
    vertex(20,80,-17.5);
    vertex(20,80,2.5);
    vertex(40,100,12.5);
    vertex(40,100,-27.5);
    endShape();
    beginShape();
    vertex(-20,80,-17.5);
    vertex(-20,80,2.5);
    vertex(-40,100,12.5);
    vertex(-40,100,-27.5);
    endShape();
    beginShape();//Arms
    vertex(-40,50,-5);
    vertex(-40,50,-10);
    vertex(-40,100,-7.5);
    endShape();
    beginShape();
    vertex(-45,50,-5);
    vertex(-45,50,-10);
    vertex(-45,100,-7.5);
    endShape();
    beginShape();
    vertex(-45,50,-5);
    vertex(-40,50,-5);
    vertex(-40,50,-10);
    vertex(-45,50,-10);
    endShape();
    beginShape();
    vertex(-45,50,-5);
    vertex(-40,50,-5);
    vertex(-40,100,-7.5);
    vertex(-45,100,-7.5);
    endShape();
    beginShape();
    vertex(-45,50,-10);
    vertex(-40,50,-10);
    vertex(-40,100,-7.5);
    vertex(-45,100,-7.5);
    endShape();
    beginShape();//other arm
    vertex(40,50,-5);
    vertex(40,50,-10);
    vertex(40,100,-7.5);
    endShape();
    beginShape();
    vertex(45,50,-5);
    vertex(45,50,-10);
    vertex(45,100,-7.5);
    endShape();
    beginShape();
    vertex(45,50,-5);
    vertex(40,50,-5);
    vertex(40,50,-10);
    vertex(45,50,-10);
    endShape();
    beginShape();
    vertex(40,50,-5);
    vertex(45,50,-5);
    vertex(45,100,-7.5);
    vertex(40,100,-7.5);
    endShape();
    beginShape();
    vertex(40,50,-10);
    vertex(45,50,-10);
    vertex(45,100,-7.5);
    vertex(40,100,-7.5);
    endShape();
    fill(#6E95B2);//Trousers tigh1
    beginShape();
    vertex(0,100,12.5);
    vertex(0,100,-27.5);
    vertex(0,150,knee1);
    endShape();
    beginShape();
    vertex(-40,100,12.5);
    vertex(-40,100,-27.5);
    vertex(-40,150,knee1);
    endShape();
    beginShape();
    vertex(0,100,12.5);
    vertex(-40,100,12.51);
    vertex(-40,150,knee1);
    vertex(0,150,knee1);
    endShape();
    beginShape();
    vertex(-40,100,-27.5);
    vertex(0,100,-27.5);
    vertex(0,150,knee1);
    vertex(-40,150,knee1);
    endShape();
    beginShape();//tigh2
    vertex(0,100,12.5);
    vertex(0,100,-27.5);
    vertex(0,150,knee2);
    endShape();
    beginShape();
    vertex(40,100,12.5);
    vertex(40,100,-27.5);
    vertex(40,150,knee2);
    endShape();
    beginShape();
    vertex(0,100,12.5);
    vertex(40,100,12.5);
    vertex(40,150,knee2);
    vertex(0,150,knee2);
    endShape();
    beginShape();
    vertex(0,100,-27.5);
    vertex(40,100,-27.5);
    vertex(40,150,knee2);
    vertex(0,150,knee2);
    endShape();
    beginShape();//lowerknee1
    vertex(0,150,knee1);
    vertex(0,ankle1,12.5);
    vertex(0,ankle1,-27.5);
    endShape();
    beginShape();
    vertex(-40,150,knee1);
    vertex(-40,ankle1,12.5);
    vertex(-40,ankle1,-27.5);
    endShape();
    beginShape();
    vertex(0,150,knee1);
    vertex(-40,150,knee1);
    vertex(-40,ankle1,12.5);
    vertex(0,ankle1,12.5);
    endShape();
    beginShape();
    vertex(0,150,knee1);
    vertex(-40,150,knee1);
    vertex(-40,ankle1,-27.5);
    vertex(0,ankle1,-27.5);
    endShape();
    beginShape();//lowerknee2
    vertex(0,150,knee2);
    vertex(0,ankle2,12.5);
    vertex(0,ankle2,-27.5);
    endShape();
    beginShape();
    vertex(40,150,knee2);
    vertex(40,ankle2,12.5);
    vertex(40,ankle2,-27.5);
    endShape();
    beginShape();
    vertex(0,150,knee2);
    vertex(40,150,knee2);
    vertex(40,ankle2,12.5);
    vertex(0,ankle2,12.5);
    endShape();
    beginShape();
    vertex(0,150,knee2);
    vertex(40,150,knee2);
    vertex(40,ankle2,-27.5);
    vertex(0,ankle2,-27.5);
    endShape();
    fill(#BD0CF0);//eyes
    beginShape();
    vertex(-5,5,0);
    vertex(-5,-5,0);
    vertex(-10,0,-2.5);
    endShape();
    beginShape();
    vertex(5,5,0);
    vertex(5,-5,0);
    vertex(10,0,-2.5);
    endShape();
    popMatrix();
  }
  void mouseturn()
  {
    if(mouseX>650)
    {mouseturnX=(mouseX-650)*0.01;}
    if(mouseX<650)
    {mouseturnX=-(-mouseX+650)*0.01;}
    
    if(mouseY>350)
    {mouseturnY=(mouseY-350)*0.5;}
    if(mouseY<350)
    {mouseturnY=-(-mouseY+350)*0.5;}
  }
  void pressedkeys()
  {
    if(key=='w')
    {a1=-5;}
    if(key=='s')
    {a1=5;}
    if(key=='a')
    {c1=5;}
    if(key=='d')
    {c1=-5;}
    if(key=='c')
    {cam1=5;}
    if(key=='x')
    {jump1=5;}
    if(key=='e')
    {lightcount=5;}
    if(key=='q')
    {sprinter=5;}
  }
  void releasedkeys()
  {
    if(key=='w')
    {a1=0;}
    if(key=='s')
    {a1=0;}
    if(key=='a')
    {c1=0;}
    if(key=='d')
    {c1=0;}
    if(key=='c')
    {cam1=0;}
    if(key=='x')
    {jump1=0;}
    if(key=='e')
    {lightcount=0;}
    if(key=='q')
    {sprinter=0;}
  }
  void moveMarcy()
  {
    if(a1<0||a1>0)
    {
      xPos=(cos(mouseturnX)*a1)+xPos;
      zPos=(sin(mouseturnX)*a1)+zPos;
    }
    if(c1<0||c1>0)
    {
      xPos=(sin(mouseturnX)*c1)+xPos;
      zPos=(cos(mouseturnX)*c1)+zPos;
    }
  }
  void jump()
  {
    if(jump1>0)
    {
      yPos=yPos-jumpfactor;
      if(yPos>floor-175||yPos<floor-400)
      {jumpfactor=-jumpfactor;}
    }
  }
  void gravity()
  {
    if(yPos<floor-200)
    {yPos++;}
    if(yPos>floor-200)
    {yPos--;}
  }
  void cam()
  {
    xCam=(cos(mouseturnX)*camdist)+xPos;
    if(cam1==0)
    {yCam=yPos+mouseturnY;}
    zCam=(sin(mouseturnX)*camdist)+zPos;
    camera(xCam,yCam,zCam,xPos,yPos,zPos,0,1,0);
    if(cam1==0)
    {camdist=200;}
    if(cam1>0)
    {
    camdist=10;
    yCam=yPos;
    }
  }
  void flashlight()
  { 
   
    
    xLight=(cos(mouseturnX)*-200)+xPos;
    yLight=yPos;
    zLight=(sin(mouseturnX)*-200)+zPos;
  
    if(lightcount==0)
    {brightlight=0;}
    if(lightcount>0)
    {brightlight=255;}
  }
  void sprint()
  {
    if(sprinter>0&stamina>10)
    {
    a1=-15;
    }
    if(sprinter>0)
    {stamina=stamina-5;}
    if(stamina<100)
    {stamina=stamina+1;}
    pushMatrix();
    hint(DISABLE_DEPTH_TEST);
    camera();
    noLights();
    rectMode(CORNER);
    fill(#00FF00);
    rect(0,350,stamina,10);
    hint(ENABLE_DEPTH_TEST);
    popMatrix();
  }
  void bounceBack()
  {
    zPos=(sin(mouseturnX)*-a1)+zPos;
    xPos=(cos(mouseturnX)*-a1)+xPos;
    zPos=zPos-(zPos*0.01);
    xPos=xPos-(xPos*0.01);
  }
  void moveLegs()
  {
    if(c1==0&a1==0&jump1==0)
    {
      knee1=12.5;
      knee2=12.5;
      ankle1=200;
      ankle2=200;
      legswitch=0;
    }
    if(c1!=0||a1!=0)
    {
      knee1=knee1+m;
      ankle1=ankle1+w;
      if(knee1<12.5||knee1>37.5)
      {m=-m;}
      if(ankle1>200||ankle1<175)
      {w=-w;}
      if(knee1>37)
      {legswitch=1;}
      if(legswitch==1)
      {
        knee2=knee2+n;
        ankle2=ankle2+v;
        if(knee2<12.5||knee2>37.5)
        {n=-n;}
        if(ankle2>200||ankle2<175)
        {v=-v;}
      }
    }
    if(jump1!=0)
    {
      if(yPos>floor-200)
      {
        knee1=knee1+m;
        ankle1=ankle1+w;
        if(knee1<12.5||knee1>37.5)
        {m=-m;}
        if(ankle1>200||ankle1<175)
        {w=-w;}
        knee2=knee2+n;
        ankle2=ankle2+v;
        if(knee2<12.5||knee2>37.5)
        {n=-n;}
        if(ankle2>200||ankle2<175)
        {v=-v;}
      }
    }
  }
}

the tree tab:

boolean bouncer=false;
class Tree
{
  float xPos;
  float zPos;
  
  Tree(float x,float z)
  {
    xPos=x;
    zPos=z;
  }
  void drawTree()
  {
    pushMatrix();
    translate(xPos,240,zPos);
    fill(#8E8671);
    box(20,800,20);
    popMatrix();
    pushMatrix();
    translate(xPos,240,zPos);
    box(80,10,10);
    box(10,10,80);
    popMatrix();
    pushMatrix();
    translate(xPos,200,zPos);
    box(100,10,10);
    box(10,10,100);
    popMatrix();
    pushMatrix();
    translate(xPos,160,zPos);
    box(120,10,10);
    box(10,10,120);
    popMatrix();
    pushMatrix();
    translate(xPos,120,zPos);
    box(140,10,10);
    box(10,10,140);
    popMatrix();
    pushMatrix();
    translate(xPos,80,zPos);
    box(120,10,10);
    box(10,10,120);
    popMatrix();
    pushMatrix();
    translate(xPos,40,zPos);
    box(100,10,10);
    box(10,10,100);
    popMatrix();
    pushMatrix();
    translate(xPos,0,zPos);
    box(80,10,10);
    box(10,10,80);
    popMatrix();
    pushMatrix();
    translate(xPos,-40,zPos);
    box(60,10,10);
    box(10,10,60);
    popMatrix();
    pushMatrix();
    translate(xPos,-80,zPos);
    box(40,10,10);
    box(10,10,40);
    popMatrix();
    
    if((ax<xPos-40||ax>xPos+40)||(cz<zPos-40||cz>zPos+40))
    {bouncer=false;}
    if((ax>xPos-40&ax<xPos+40)&(cz>zPos-40&cz<zPos+40))
    {bouncer=true;}
  }  
}

do you need to execute them in the same millisecond? If so you can just call the function and wait until a global boolean is triggered.

I made the bouncer variable a boolean variable, but it did not work. Could you please explain to me what you meant, because I think I did not understand it.

well I did it in a different way and they are both executed at the “exact” same time

boolean toggle = false, toggle2 = false;
int triggerAt = 1000;
void setup() {
}
void draw() {
  f1();
  f2();
}
void f1() {
  if (millis() >= triggerAt && !toggle) {
    println(millis());
    toggle = !toggle;
  }
}
void f2() {
  if (millis() >= triggerAt && !toggle2) {
    println(millis());
    toggle2 = !toggle2;
  }
}

image

with the boolean I was thinking along the lines of:

boolean doStuff = false;
void draw() {
   if(frameCount == 100) {
      doStuff = true;
  } else { doStuff = false; }
f1();
f2();
}
void f1() {
   if(doStuff) {

   }
}
void f2() {
   if(doStuff) {

   }
}

I still can not understand it, but thank you for your work. I think I am going to edit my question with some code.

I’m not sure if this will solve your issue, but your conditional for collision checking below is definitely wrong.

Replace this:

With something like this:

bouncer = (ax > xPos - 40 && ax < xPos + 40) && (cz > zPos - 40 && cz < zPos + 40);

Thank you, I’m going to try it.

Unfortuneately, it still does not work, but thank you.

Okay, I think I will try it with an array. Unfortuneately it does not work as intended. I asked already a question to the array problem. You can find it here:
https://discourse.processing.org/t/problems-with-arrays/26933