# Creating an enemy class

**URL:** <https://discourse.processing.org/t/creating-an-enemy-class/17223>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [January 21, 2020, 12:27pm UTC](https://discourse.processing.org/t/creating-an-enemy-class/17223 "2020-01-21T12:27:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lupos](https://avatars.discourse-cdn.com/v4/letter/l/d9b06d/32.png) [@lupos](https://discourse.processing.org/u/lupos)\
**Post date:** [January 21, 2020, 12:27pm UTC](https://discourse.processing.org/t/creating-an-enemy-class/17223/1 "2020-01-21T12:27:36Z")

</div>

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

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

  }
}
}

```

---

<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:** [January 21, 2020, 1:30pm UTC](https://discourse.processing.org/t/creating-an-enemy-class/17223/2 "2020-01-21T13:30:32Z")

</div>

I think you need to look at the tutorial

[https://www.processing.org/tutorials/objects/](https://www.processing.org/tutorials/objects/)

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [January 21, 2020, 2:47pm UTC](https://discourse.processing.org/t/creating-an-enemy-class/17223/3 "2020-01-21T14:47:48Z")

</div>

You also need to know about the difference between setup and draw. This [tutorial](https://www.youtube.com/watch?v=o8dffrZ86gs) looks at program flow inside Processing.

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

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

---

<div class="post-metadata">

**Author:** ![lupos](https://avatars.discourse-cdn.com/v4/letter/l/d9b06d/32.png) [@lupos](https://discourse.processing.org/u/lupos)\
**Post date:** [January 21, 2020, 9:34pm UTC](https://discourse.processing.org/t/creating-an-enemy-class/17223/4 "2020-01-21T21:34:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lupos](https://avatars.discourse-cdn.com/v4/letter/l/d9b06d/32.png) [@lupos](https://discourse.processing.org/u/lupos)\
**Post date:** [January 21, 2020, 10:14pm UTC](https://discourse.processing.org/t/creating-an-enemy-class/17223/5 "2020-01-21T22:14:30Z")

</div>

never mind the tutorial helped

---

<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:** [January 22, 2020, 2:33pm UTC](https://discourse.processing.org/t/creating-an-enemy-class/17223/6 "2020-01-22T14:33:56Z")

</div>

show your code and we will have a look.

Maybe there is room for improvement

---

<div class="post-metadata">

**Author:** ![lupos](https://avatars.discourse-cdn.com/v4/letter/l/d9b06d/32.png) [@lupos](https://discourse.processing.org/u/lupos)\
**Post date:** [January 30, 2020, 10:53pm UTC](https://discourse.processing.org/t/creating-an-enemy-class/17223/7 "2020-01-30T22:53:12Z")

</div>

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

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

```
