Creating an enemy class

I am working on a school project and I am having trouble creating multiple enemies form one class. when I create an two enemies from the same class and when one gets hit they all disappear can someone help please.

here is my code

float X,Y,Xc,Yc,Xm,Ym,Xl,Xr,Yt,Yb;
float H1,H2,H3,H1x,H1y,Ev;
float Hbar=3;
float Pdamx=100*width/1000, pdamy=100*height/1000;
color H1G, H1B;
int EM =1;
int EMi =1;
void setup(){
   H1G= color(255,0,0,255);
   H1B = color(0,0,0,0);
  Ev=255;
size(1000,1000);
X=2;Y=2;Yc=500*height/1000;Xc=500*width/1000;Xl=300*width/1000;Xr=700*width/1000;Yt=700*height/1000;Yb=300*height/1000;

}

void draw(){
  enamy E1= new enamy(300,700);
    enamy E3= new enamy(300,300);
  enamy2 E2= new enamy2(700,700);
  background(150);
  rectMode(CENTER);
 if(X==2){Xm=Xc;}
 if(X==1){Xm=Xl;}
 if(X==3){Xm=Xr;}
 if(Y==2){Ym=Yc;}
 if(Y==1){Ym=Yb;}
 if(Y==3){Ym=Yt;}
//health
print(Hbar);
if(Hbar==3){
 fill(H1G);
  rect(25,25,50,50);
}
if(Hbar==2){
  fill(0,255,0);
   rect(25,25,50,50);
}
if(Hbar==1){
  fill(255,150,255);
   rect(25,25,50,50);
}
if(Hbar==0){
  fill(0);
   rect(25,25,50,50);
}
 

//board
  fill(255);
rect(500*width/1000,500*height/1000,200*width/1000,200*height/1000);
rect(700*width/1000,500*height/1000,200*width/1000,200*height/1000);
rect(500*width/1000,700*height/1000,200*width/1000,200*height/1000);
rect(700*width/1000,700*height/1000,200*width/1000,200*height/1000);
rect(500*width/1000,300*height/1000,200*width/1000,200*height/1000);
rect(300*width/1000,500*height/1000,200*width/1000,200*height/1000);
rect(300*width/1000,300*height/1000,200*width/1000,200*height/1000);
rect(700*width/1000,300*height/1000,200*width/1000,200*height/1000);
rect(300*width/1000,700*height/1000,200*width/1000,200*height/1000);
//player
  fill(0);
  rect(Xm,Ym,100*width/1000,100*height/1000);
  //enamy
E1.move();
E2.movee();
E3.move();
}
//movement
void keyReleased(){
if (key == 'a') {
    X=X-1;
    
  }
  if (key == 'd'){
    X=X+1;
  }
  if (key == 'w'){
    Y=Y-1;
  }
  if (key == 's') {
    Y=Y+1;
  }
}
class enamy {
  float Xe,Ye;
  enamy(float x,float y){Xe=x;Ye=y;}
  void move(){
  switch(EM){
case 1:
  if(Xm==Xe && Ym==Ye){ EM=2;Hbar=Hbar-1;}
  fill(150,100,0,Ev);
  rect(Xe,Ye,150*width/1000,150*height/1000);
  case 2:
  Xe=0;
  Ye =0;


  }
}
}
class enamy2 {
  float Xe2,Ye2;
  enamy2(float x,float y){Xe2=x;Ye2=y;}
  void movee(){
  switch(EMi){
case 1:
  if(Xm==Xe2 && Ym==Ye2){ EMi=2;Hbar=Hbar-1;}
  fill(150,100,0,Ev);
  rect(Xe2,Ye2,150*width/1000,150*height/1000);
  case 2:
  Xe2=0;
  Ye2 =0;


  }
}
}
1 Like

I think you need to look at the tutorial

https://www.processing.org/tutorials/objects/

1 Like

You also need to know about the difference between setup and draw. This tutorial looks at program flow inside Processing.

Since the draw method is executed ~60 times a second you want to avoid statemnts like

enamy E1= new enamy(300,700);
enamy E3= new enamy(300,300);
enamy2 E2= new enamy2(700,700);

inside draw. These statements from your draw method are creating 180 enemies a second which I am sure is not what you want to do.

3 Likes

its still not working. Im so confused. I tried fallowing it but I ran into the same problem where both enemies disappear when one is hit

never mind the tutorial helped

1 Like

show your code and we will have a look.

Maybe there is room for improvement

i AM STILL HAVING Trouble with hit boxes, the enemy only stops when it gets to s specific point not when it touches the hole enemy and i can figure out how

float X,Y,Xc,Yc,Xm,Ym,Xl,Xr,Yt,Yb;
float H1,H2,H3,H1x,H1y,Ev;
float Hbar=3;
float Pdamx=100*width/1000, pdamy=100*height/1000;
color H1G, H1B;
int EM =1;
int EMi =1;
enemy em1;
enemy em2;
void setup(){
  em1 = new enemy(color(255,0,0),0,300,2);
    em2 = new enemy(color(255,0,0),0,700,2);
   H1G= color(255,0,0,255);
   H1B = color(0,0,0,0);
  Ev=255;
size(1000,1000);
X=2;Y=2;Yc=500*height/1000;Xc=500*width/1000;Xl=300*width/1000;Xr=700*width/1000;Yt=700*height/1000;Yb=300*height/1000;

}

void draw(){

  background(150);
  rectMode(CENTER);
 if(X==2){Xm=Xc;}
 if(X==1){Xm=Xl;}
 if(X==3){Xm=Xr;}
 if(Y==2){Ym=Yc;}
 if(Y==1){Ym=Yb;}
 if(Y==3){Ym=Yt;}
//health
print(Hbar);
if(Hbar==3){
 fill(H1G);
  rect(25,25,50,50);
}
if(Hbar==2){
  fill(0,255,0);
   rect(25,25,50,50);
}
if(Hbar==1){
  fill(255,150,255);
   rect(25,25,50,50);
}
if(Hbar==0){
  fill(0);
   rect(25,25,50,50);
}
 

//board
  fill(255);
rect(500*width/1000,500*height/1000,200*width/1000,200*height/1000);
rect(700*width/1000,500*height/1000,200*width/1000,200*height/1000);
rect(500*width/1000,700*height/1000,200*width/1000,200*height/1000);
rect(700*width/1000,700*height/1000,200*width/1000,200*height/1000);
rect(500*width/1000,300*height/1000,200*width/1000,200*height/1000);
rect(300*width/1000,500*height/1000,200*width/1000,200*height/1000);
rect(300*width/1000,300*height/1000,200*width/1000,200*height/1000);
rect(700*width/1000,300*height/1000,200*width/1000,200*height/1000);
rect(300*width/1000,700*height/1000,200*width/1000,200*height/1000);
//player
  fill(0);
  rect(Xm,Ym,100*width/1000,100*height/1000);
  //enamy
em1.drive();
em1.display();
em1.hit();
em2.drive();
em2.display();
em2.hit();



}
//movement
void keyReleased(){
if (key == 'a') {
    X=X-1;
    
  }
  if (key == 'd'){
    X=X+1;
  }
  if (key == 'w'){
    Y=Y-1;
  }
  if (key == 's') {
    Y=Y+1;
  }
}
class enemy { 
  color c;
  float xpos;
  float ypos;
  float xspeed;
  int boxX=200;
  int boxY=100;
  // The Constructor is defined with arguments.
  enemy(color tempC, float tempXpos, float tempYpos, float tempXspeed) { 
    c = tempC;
    xpos = tempXpos;
    ypos = tempYpos;
    xspeed = tempXspeed;
  }

  void display() {
    stroke(0);
    fill(c);
    rectMode(CENTER);
    rect(xpos,ypos,boxX,boxY);
  }

  void drive() {
    xpos = xpos + xspeed;
    if (xpos > width) {
      xpos = 0;
    }
  }
  void hit(){
   if(xpos==Xm&&ypos==Ym){
    Hbar=Hbar-1; 
    xpos=0;
    xspeed=0;
   }
  }
}