0

My program is a zombie survival game, set in a 2d array of blocks. I use an arraylist - my first attempt - to store the zombies, and each in tern "checks" if there is a block above, below and around it, to detect it's movement.

I'll post the relevant code here, and upload the sketch folder separately.

ArrayList zombies;

void setup() {
  zombies = new ArrayList();
}

void draw() {
      for (int i = 0; i < zombies.size(); i++) {
    Zombie zombie = (Zombie) zombies.get(i);
      zombie.draw();
}
}

void keyPressed() {
  if (key == 'z') {
    zombies.add(new Zombie());
}
}

class Zombie
{
  int posX = 20;
  int posY = 10;
  boolean fall;
  boolean playerOnBlock;

  Zombie() {
    posX = 10;
    posY = 590;
    fall = false;
  }

  void draw() {
    grid.blockCheck(posX, posY, 2);
    fill(0, 255, 0);
    rect(posX, posY, 10, 10);
  }

  void fall() {
    posY += 5;
    println("zombiefall"+posY);
  }

  void move(boolean left, boolean right, boolean above) {
    if (left == true && player.playerX < posX) {
      posX -= 1;
    }
    if (right == true && player.playerX > posX) {
      posX += 1;
    }
  }
}

class Grid {
  void blockCheck(int x, int y, int e) {
    for (int i = 0; i < l; i++)
      for (int j = 0; j < h; j++)
      {
        grid[i][j].aroundMe (x, y, i, j, e);
      }
  }
}

class Block {
  void aroundMe(int _x, int _y, int _i, int _j, int entity) {
    int pGX = _x/10;
    int pGY = _y/10;

    if (entity == 1) {
      if (pGY+1 == _j && pGX == _i && state == 4) {
        player.fall();
      }

      if (pGX == _i && pGX-1 <= _i && _y == posY && state == 4) {
        leftOfMe = true;
      }
      else
      {
        leftOfMe = false;
      }

      if (pGX+1 == _i && _y == posY && state == 4) {
        rightOfMe = true;
      }
      else
      {
        rightOfMe = false;
      }
      if (pGY-1 == _j && _x == posX && state ==4) {
        aboveOfMe = true;
      }
      else
      {
        aboveOfMe = false;
      }

      player.controls(leftOfMe, rightOfMe, aboveOfMe);
    }

    if (entity == 2) {

      if (pGY+1 == _j && pGX == _i && state == 4) {
              for (int i = 0; i < zombies.size(); i++) {
    Zombie zombie = (Zombie) zombies.get(i);
        zombie.fall();
      }
      }

      if (pGX == _i && pGX-1 <= _i && _y == posY && state == 4) {
        ZleftOfMe = true;
      }
      else
      {
        ZleftOfMe = false;
      }

      if (pGX+1 == _i && _y == posY && state == 4) {
        ZrightOfMe = true;
      }
      else
      {
        ZrightOfMe = false;
      }

      if (pGY-1 == _j && _x == posX && state ==4) {
        ZaboveOfMe = true;
      }
      else
      {
        ZaboveOfMe = false;
      }
      for (int i = 0; i < zombies.size(); i++) {
    Zombie zombie = (Zombie) zombies.get(i);
      zombie.move(ZleftOfMe, ZrightOfMe, ZaboveOfMe);
    }
  }

Sketch is here: http://www.mediafire.com/?u5v3117baym846v

I believe the problem lies in specifying which element of an arraylist I am referring to, as I can observe the issues to be:

All "zombies" fall when one detects that it should fall.

Zombie's speed increases with each additional zombie added - somehow treating all the zombie elements as one zombie object?

This might be a similar issue: All elements of An ArrayList change when a new one is added?

But I've fiddled with my project and I can't seem to get it working still.

Please don't hesitate to ask for more information on my project. I will be with my computer all evening so should be able to reply quickly. Thanks in advance.

Thanks for your help. I'm using it like this:

ArrayList <Zombie> zombies =  new ArrayList <Zombie>();
-------------------------------------------
    void setup(){
  zombies = new ArrayList();
-------------------------------------------

void draw(){
  for (Zombie z:zombies) {
    z.draw();
  }
}

-------------------------------------------

void keyPressed() {
  if (key == 'z') {
    for (int i = 0; i< 1; i++) {
      zombies.add(new Zombie(i));
    }
  }

-------------------------------------------

class Zombie
{
  int posX = 20;
  int posY = 10;
  boolean fall;
  boolean playerOnBlock;
  int z;

  Zombie(int _z) {
    posX = 10;
    posY = 590;
    fall = false;
    z = _z;
  }

  void draw() {
    grid.blockCheck(posX, posY, 2);
    fill(0, 255, 0);
    rect(posX, posY, 10, 10);
  }

  void fall() {
    posY += 5;
    println("zombiefall"+posY);
  }

  void move(boolean left, boolean right, boolean above) {
    if (left == true && player.playerX < posX) {
      posX -= 1;
    }
    if (right == true && player.playerX > posX) {
      posX += 1;
    }
  }
}
Community
  • 1
  • 1
GLOCKA
  • 1
  • 1
  • I hasten to add that part of the problem I'm having is not knowing what the problem is! – GLOCKA Mar 13 '13 at 20:04
  • you may want to fix your indentation, it's all over the place =) Also, use generics: ArrayList zombies = new ArrayList(), so that you don't have to constantly cast the objects you get from it as (Zombie). Last, with that, you might want to use for(Zombie z: zombies) { [... use z here ...] } since you don't care about the iterator variable. – Mike 'Pomax' Kamermans Mar 13 '13 at 22:17
  • Thank you, but I still don't understand how to use the code you have suggested. I have edited my answer to show what I am doing. Thanks again! – GLOCKA Mar 14 '13 at 09:31
  • Maybe. for (Zombie z:zombies) { z.draw(); } } – v.k. Mar 14 '13 at 10:49

1 Answers1

0

Here is the idea :)

//use generics to keep it simple. Only Zoombies in this list
ArrayList <Zoombie> samp =  new ArrayList <Zoombie>();

void setup() {
  //a regular for loop to use the i as id
  for (int i = 0; i< 100; i++) {
    samp.add(new Zoombie(i));

  }
  //run just once
  noLoop();
}


void draw() {

  //a enhanced for, no need for use  get()
  // z will represent each element in the collection
  for (Zoombie z:samp) {
    z.d();   
  }

  println("done");
}

// a dummy class ;)
class Zoombie {
  int id;
  Zoombie (int _id)
  {
    id =_id;
  }

  void d()
  {
    println(id);
  }
}
v.k.
  • 2,826
  • 2
  • 20
  • 29
  • I'm sorry, I still don't understand. They zombies still all "fall" at once, and accelerate with the addition of each one. I am trying to add a new zombie to the array with the press of the 'z' key, but that part of the code is different to yours. Am I missing some knowledge here? – GLOCKA Mar 14 '13 at 15:24
  • Well, I'm sorry i didn't dig in the question and only addressed the "Cannot convert from element type Object to zombs.Zombie" error. You got rid of this didn't – v.k. Mar 14 '13 at 15:28
  • Yes, I no longer get this error message, yet the zombies are behaving as they did before: All "zombies" fall when one detects that it should fall. Zombie's speed increases with each additional zombie added. – GLOCKA Mar 14 '13 at 15:33
  • The other issue, as the code will not compile, is harder to say. Could you perhaps isolate a compileble snippet? – v.k. Mar 14 '13 at 15:34
  • I don't think so, but I've put all the code pertaining to zombies in my original post. Thanks for your patience. – GLOCKA Mar 14 '13 at 15:47
  • I did download the code and run, but failed to get what is the expected behaviour or the problem :( sorry. – v.k. Mar 14 '13 at 18:25