# Cannot find a class or type named “Enemy” -- How to Initialize an Array of Objects

**URL:** https://discourse.processing.org/t/cannot-find-a-class-or-type-named-enemy-how-to-initialize-an-array-of-objects/40043
**Category:** Beginners
**Created:** [December 7, 2022, 12:48am UTC](https://discourse.processing.org/t/cannot-find-a-class-or-type-named-enemy-how-to-initialize-an-array-of-objects/40043 "2022-12-07T00:48:46Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![C2014](https://avatars.discourse-cdn.com/v4/letter/c/f1d935/32.png) [@C2014](https://discourse.processing.org/u/C2014)
#### Post date: [December 7, 2022, 12:48am UTC](https://discourse.processing.org/t/cannot-find-a-class-or-type-named-enemy-how-to-initialize-an-array-of-objects/40043/1 "2022-12-07T00:48:46Z")

</div>

```auto
  int spawnTime=180;
Enemy[] zombieArray;
int zombiecount;
int maxzombie=8;
int i;
 void setup()
{
  maxzombie=8;
 size(500,300); 

 zombieArray = new Enemy[maxzombie];
}
void draw(){
  background (100);
for ( Enemy zomb:zombieArray) {
    if(mousePressed=true){
    zombieArray[i].display();
    zombieArray[i].update();}
}
 
 
 
 
 
 class Enemy
{
  float x,y,speed;
  
    Enemy()
      {x=450;y=random(575,585);speed=1;}
 
  void update(){
    x-=speed;
  }
  
  void display(){
    fill (100,250,100);
      rect(x,y,5,10);}
}
}

```

---

<div class="post-metadata">

### Author: ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)
#### Post date: [December 7, 2022, 2:15am UTC](https://discourse.processing.org/t/cannot-find-a-class-or-type-named-enemy-how-to-initialize-an-array-of-objects/40043/2 "2022-12-07T02:15:36Z")

</div>

Hello @C2014 🙂

I have made comments next to the lines that need to be changed.

```auto
int spawnTime=180;
Enemy[] zombieArray; 
int zombiecount;
int maxzombie=8;
int i; // do not need this, it's declared and initialized in your for-loop(s)

void setup(){
  maxzombie=8;//delete this duplicate, you've already initialized this variable above,
  size(500, 300); 

  zombieArray = new Enemy[maxzombie]; //here you are declaring the number of Enemy object
  
  //Add code here. You need to add a for-loop to initialize the array
  
}
void draw() {
  background (100);
  
  for ( Enemy zomb : zombieArray) {.
    if (mousePressed = true) { // this means you are assigning a VALUE of true. Actually you want to indicate a STATE.
    // change from = to ==.
      zombieArray[i].display();
      zombieArray[i].update();
    }
  }

  class Enemy
  {
    float x, y, speed;

    Enemy() {
      x = 450;
      y = random(575, 585); // these values fall outside the height of your sketch,
      //in setup() you say 300 for sketch height
      speed = 1;
    }

    void update() {
      x-=speed;
    }

    void display() {
      fill (100, 250, 100);
      rect(x, y, 5, 10);
    }
  }
}

```

I have linked an excellent Coding Train youtube tutorial for initializing an array of Objects:

[![](https://img.youtube.com/vi/am6e1U2BHkA/hqdefault.jpg "9.4: Arrays and Loops - Processing Tutorial") ](https://www.youtube.com/watch?v=am6e1U2BHkA)

🤓

---

<div class="post-metadata">

### Author: ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)
#### Post date: [December 7, 2022, 6:13am UTC](https://discourse.processing.org/t/cannot-find-a-class-or-type-named-enemy-how-to-initialize-an-array-of-objects/40043/3 "2022-12-07T06:13:42Z")

</div>

You’ve defined your Enemy class inside your draw() function. This is probably not what you want to do. Check your pairs of {}!

---

<div class="post-metadata">

### Author: ![C2014](https://avatars.discourse-cdn.com/v4/letter/c/f1d935/32.png) [@C2014](https://discourse.processing.org/u/C2014)
#### Post date: [December 7, 2022, 2:32pm UTC](https://discourse.processing.org/t/cannot-find-a-class-or-type-named-enemy-how-to-initialize-an-array-of-objects/40043/4 "2022-12-07T14:32:28Z")

</div>

Thank you, it helped me!!
