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

**URL:** https://discourse.processing.org/t/how-can-i-make-multiple-objects-of-the-same-class-to-execute-the-same-method-at-the-same-time/26654
**Category:** Coding Questions
**Created:** [December 31, 2020, 6:27pm UTC](https://discourse.processing.org/t/how-can-i-make-multiple-objects-of-the-same-class-to-execute-the-same-method-at-the-same-time/26654 "2020-12-31T18:27:42Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![SailorToei](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@SailorToei](https://discourse.processing.org/u/SailorToei)
#### Post date: [December 31, 2020, 6:27pm UTC](https://discourse.processing.org/t/how-can-i-make-multiple-objects-of-the-same-class-to-execute-the-same-method-at-the-same-time/26654/1 "2020-12-31T18:27:42Z")

</div>

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:

```auto
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:

```auto
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):

```auto
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:

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

```

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [December 31, 2020, 7:49pm UTC](https://discourse.processing.org/t/how-can-i-make-multiple-objects-of-the-same-class-to-execute-the-same-method-at-the-same-time/26654/2 "2020-12-31T19:49:57Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![SailorToei](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@SailorToei](https://discourse.processing.org/u/SailorToei)
#### Post date: [January 1, 2021, 4:16pm UTC](https://discourse.processing.org/t/how-can-i-make-multiple-objects-of-the-same-class-to-execute-the-same-method-at-the-same-time/26654/3 "2021-01-01T16:16:44Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [January 1, 2021, 4:30pm UTC](https://discourse.processing.org/t/how-can-i-make-multiple-objects-of-the-same-class-to-execute-the-same-method-at-the-same-time/26654/4 "2021-01-01T16:30:34Z")

</div>

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

```auto
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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/9df5ccdb61768b37aed47ac7b6ab5c6057c2ae74.png)

with the boolean I was thinking along the lines of:

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

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

   }
}

```

---

<div class="post-metadata">

### Author: ![SailorToei](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@SailorToei](https://discourse.processing.org/u/SailorToei)
#### Post date: [January 1, 2021, 4:45pm UTC](https://discourse.processing.org/t/how-can-i-make-multiple-objects-of-the-same-class-to-execute-the-same-method-at-the-same-time/26654/5 "2021-01-01T16:45:38Z")

</div>

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

---

<div class="post-metadata">

### Author: ![rbrauer](https://avatars.discourse-cdn.com/v4/letter/r/838e76/32.png) [@rbrauer](https://discourse.processing.org/u/rbrauer)
#### Post date: [January 2, 2021, 1:24am UTC](https://discourse.processing.org/t/how-can-i-make-multiple-objects-of-the-same-class-to-execute-the-same-method-at-the-same-time/26654/6 "2021-01-02T01:24:43Z")

</div>

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

Replace this:

> [@SailorToei](#):
>
> ```auto
> 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;}
> 
> ```

With something like this:

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

```

> **[&& (logical AND) / Reference](https://processing.org/reference/logicalAND.html)**
>
> Compares two expressions and returns true only if both evaluate to true. Returns false if one or both evaluate to false. The following list shows all possible combinations:…

> **[& (bitwise AND) / Reference](https://processing.org/reference/bitwiseAND.html)**
>
> Compares each corresponding bit in the binary representation of the values. For each comparison two 1's yield 1, 1 and 0 yield 0, and two 0's yield 0. This is easy to see when we look at the binary re…

---

<div class="post-metadata">

### Author: ![SailorToei](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@SailorToei](https://discourse.processing.org/u/SailorToei)
#### Post date: [January 2, 2021, 10:54am UTC](https://discourse.processing.org/t/how-can-i-make-multiple-objects-of-the-same-class-to-execute-the-same-method-at-the-same-time/26654/7 "2021-01-02T10:54:23Z")

</div>

Thank you, I’m going to try it.

---

<div class="post-metadata">

### Author: ![SailorToei](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@SailorToei](https://discourse.processing.org/u/SailorToei)
#### Post date: [January 2, 2021, 1:37pm UTC](https://discourse.processing.org/t/how-can-i-make-multiple-objects-of-the-same-class-to-execute-the-same-method-at-the-same-time/26654/8 "2021-01-02T13:37:00Z")

</div>

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

---

<div class="post-metadata">

### Author: ![SailorToei](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@SailorToei](https://discourse.processing.org/u/SailorToei)
#### Post date: [January 9, 2021, 8:03pm UTC](https://discourse.processing.org/t/how-can-i-make-multiple-objects-of-the-same-class-to-execute-the-same-method-at-the-same-time/26654/9 "2021-01-09T20:03:11Z")

</div>

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](https://discourse.processing.org/t/problems-with-arrays/26933)

---

<div class="post-metadata">

### Author: ![SailorToei](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@SailorToei](https://discourse.processing.org/u/SailorToei)
#### Post date: [February 10, 2021, 11:15am UTC](https://discourse.processing.org/t/how-can-i-make-multiple-objects-of-the-same-class-to-execute-the-same-method-at-the-same-time/26654/10 "2021-02-10T11:15:54Z")

</div>

My teacher gave me the solution: The trees are the ones, which should have the bounce method. In that way it works. I made a new “game” where i have walls, and it works how it should. Thank you all for your help and your work. 🙂

```auto
Girl Mel;
Wall wall1;
Wall wall2;
void setup()
{
  size(1200,700,P3D);
  Mel=new Girl(0,695,0,0,0);
  wall1=new Wall(300,350,500,1000,700,20);
  wall2=new Wall(-200,350,-500,20,700,1000);
}
void draw()
{
  background(#17BFFA);
  pushMatrix();
  translate(0,700,0);
  fill(#1CB714);
  box(5000,10,5000);
  popMatrix();
  Mel.drawGirl();
  Mel.moveGirl();
  Mel.cam();
  Mel.Gravity();
  wall1.drawWall();
  wall1.bouncer();
  wall2.drawWall();
  wall2.bouncer();
}
void keyPressed()
{
  Mel.pressedkeysGirl();
}
void keyReleased()
{
  Mel.releasedkeysGirl();
}
void mouseMoved()
{
  Mel.mouseturn();
}
void mousePressed()
{
}
void mouseReleased()
{
}

```

```auto
float a1=0;
float b1=0;
float c1=0;
float xPos=0;
float yPos=455;
float zPos=0;
float cammeter=0;
float jumpmod=5;
float mouseturnX=0;
float mouseturnY=0;
color colour=#22EA11;
class Girl
{
  float camdist;
  float floor;
  float xCam;
  float yCam;
  float zCam;
  
  Girl(float cd,float f,float xc,float yc,float zc)
  {
    camdist=cd;
    floor=f;
    xCam=xc;
    yCam=yc;
    zCam=zc;
  }
  void drawGirl()
  {
    pushMatrix();
    translate(xPos,yPos,zPos);
    //if(a1!=0||c1!=0)
    //{rotateY(mouseturnX);}//if other figur shall back bounce, then use variables as summands and if the touch the object the variable is -variable
    fill(#F5C372);//Face
    beginShape();
    vertex(10,0,5);
    vertex(0,20,0);
    vertex(0,-10,0);
    endShape();
    beginShape();
    vertex(-10,0,5);
    vertex(0,20,0);
    vertex(0,-10,0);
    endShape();
    fill(colour);//Eyes
    beginShape();
    vertex(5,0,0);
    vertex(7.5,2.5,2.5);
    vertex(7.5,-2.5,2.5);
    endShape();
    beginShape();
    vertex(-5,0,0);
    vertex(-7.5,2.5,2.5);
    vertex(-7.5,-2.5,2.5);
    endShape();
    popMatrix();
  }
  void pressedkeysGirl()
  {
    if(key=='w')
    {c1=-5;}
    if(key=='s')
    {c1=5;}
    if(key=='a')
    {a1=5;}
    if(key=='d')
    {a1=-5;}
    if(key=='x')
    {b1=5;}
    if(key=='c')
    {cammeter=5;}
    
  }
  void releasedkeysGirl()
  {
    if(key=='w')
    {c1=0;}
    if(key=='s')
    {c1=0;}
    if(key=='a')
    {a1=0;}
    if(key=='d')
    {a1=0;}
    if(key=='x')
    {b1=0;}
    if(key=='c')
    {cammeter=0;}
  }
  void mouseturn()
  {
    if(mouseX<500)
    {mouseturnX=(-(-mouseX+500))*0.01;}
    if(mouseX>500)
    {mouseturnX=(mouseX-500)*0.01;}
    if(mouseY<350)
    {mouseturnY=-(-mouseY+350)*0.5;}
    if(mouseY>350)
    {mouseturnY=(mouseY-350)*0.5;}
  }
  void moveGirl()
  { 
    if(c1<0||c1>0)
    {
    xPos=(sin(mouseturnX)*c1)+xPos;
    zPos=(cos(mouseturnX)*c1)+zPos;
    }
    if(a1<0||a1>0)
    {
    xPos=(cos(mouseturnX)*a1)+xPos;
    zPos=(sin(mouseturnX)*a1)+zPos;
    }
    if(b1!=0)
    {
      yPos=yPos+jumpmod;
      if(yPos<floor-500||yPos>floor-225)
      {jumpmod=-jumpmod;}
    }
  }
  void cam()
  {
    xCam=(sin(mouseturnX)*camdist)+xPos;
    zCam=(cos(mouseturnX)*camdist)+zPos;
    if(cammeter>0)
    {
    camdist=10;
    yCam=yPos;
    }
    if(cammeter==0)
    {
    camdist=250;
    yCam=yPos+mouseturnY;
    }
    camera(xCam,yCam,zCam,xPos,yPos,zPos,0,1,0);
  }
  void Gravity()
  {
    if(yPos<floor-250)
    {yPos=yPos+2.5;}
    if(yPos>floor-250)
    {yPos=yPos-2.5;}
  }
}

```

```auto
class Wall
{
  float xWall;
  float yWall;
  float zWall;
  float xWidth;
  float yWidth;
  float zWidth;
  
  Wall(float x,float y,float z,float xw,float yw,float zw)
  {
    xWall=x;
    yWall=y;
    zWall=z;
    xWidth=xw;
    yWidth=yw;
    zWidth=zw;
  }
  void drawWall()
  {
    pushMatrix();
    fill(#FFFFFF);
    translate(xWall,yWall,zWall);
    box(xWidth,yWidth,zWidth);
    popMatrix();
  }
  void bouncer()
  {
    if((xPos>xWall-xWidth*0.5&xPos<xWall+xWidth*0.5)&(yPos>yWall-yWidth*0.5&yPos<yWall+yWidth)&(zPos>zWall-zWidth*0.5&zPos<zWall+zWidth*0.5))
    {
      xPos=(sin(mouseturnX)*-c1)+xPos;
      zPos=(cos(mouseturnX)*-c1)+zPos;
      xPos=(cos(mouseturnX)*-a1)+xPos;
      zPos=(sin(mouseturnX)*-a1)+zPos;
    }
  }
}

```

---

<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: [February 10, 2021, 12:30pm UTC](https://discourse.processing.org/t/how-can-i-make-multiple-objects-of-the-same-class-to-execute-the-same-method-at-the-same-time/26654/11 "2021-02-10T12:30:07Z")

</div>

> [@SailorToei](#):
>
> The trees are the ones, which should have the bounce method.

I don’t understand. Can you post the Tree class too?

Your Sketch runs but I can’t see trees.

---

<div class="post-metadata">

### Author: ![SailorToei](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@SailorToei](https://discourse.processing.org/u/SailorToei)
#### Post date: [February 11, 2021, 11:54am UTC](https://discourse.processing.org/t/how-can-i-make-multiple-objects-of-the-same-class-to-execute-the-same-method-at-the-same-time/26654/12 "2021-02-11T11:54:11Z")

</div>

There are no trees in this program. They were in the old program, which is now unuseable because of my experiments with arrays. So I made a new program, but instead of trees, I used walls. You can replace the term “Trees” with “Objects from whom the figur bounces away”. I used the term “Trees” to adapt to the question, one can say, the walls are the trees. in the old program it were the trees which should have had the bouncing method.

---

<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: [February 11, 2021, 12:22pm UTC](https://discourse.processing.org/t/how-can-i-make-multiple-objects-of-the-same-class-to-execute-the-same-method-at-the-same-time/26654/13 "2021-02-11T12:22:20Z")

</div>

> [@SailorToei](#):
>
> replace “Trees” with “Objects from whom the figure bounces away”

I understand now. Thank you!

when you have Wall1, Wall2 etc. and you end up with 100 walls, you’ll get into trouble. An Array or ArrayList would be good then. I am sorry it didn’t work out.

**Remark**

Also when you move your mouse right, the player goes left. Normally it’s right / right behaviour.

Warm regards,

Chrisir
