Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Arraylist处理中的所有元素都会更改_Arrays_Arraylist_Processing - Fatal编程技术网

Arrays Arraylist处理中的所有元素都会更改

Arrays Arraylist处理中的所有元素都会更改,arrays,arraylist,processing,Arrays,Arraylist,Processing,我的程序是一个僵尸生存游戏,设置在一个2d区块数组中。 我第一次尝试使用arraylist来存储僵尸,每个tern“检查”它上面、下面和周围是否有一个块,以检测它的移动 我将在这里发布相关代码,并单独上传草图文件夹 ArrayList zombies; void setup() { zombies = new ArrayList(); } void draw() { for (int i = 0; i < zombies.size(); i++) { Zombi

我的程序是一个僵尸生存游戏,设置在一个2d区块数组中。 我第一次尝试使用arraylist来存储僵尸,每个tern“检查”它上面、下面和周围是否有一个块,以检测它的移动

我将在这里发布相关代码,并单独上传草图文件夹

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);
    }
  }
ArrayList僵尸;
无效设置(){
zombies=newarraylist();
}
作废提款(){
对于(int i=0;iposX){
posX+=1;
}
}
}
类网格{
无效块检查(整数x,整数y,整数e){
对于(int i=0;i
//使用泛型保持简单。此列表中仅使用Zoombies
ArrayList samp=新的ArrayList();
无效设置(){
//使用i作为id的常规for循环
对于(int i=0;i<100;i++){
samp.add(新Zoombie(i));
}
//只跑一次
noLoop();
}
作废提款(){
//增强的for,无需使用get()
//z将表示集合中的每个元素
用于(Zoombie z:samp){
z、 d();
}
println(“完成”);
}
//虚拟类;)
Zoombie类{
int-id;
Zoombie(int\u id)
{
id=_id;
}
无效d()
{
println(id);
}
}

我赶紧补充一点,我遇到的部分问题是不知道问题是什么!您可能需要修复缩进,缩进到处都是=)另外,使用泛型:ArrayList zombies=new ArrayList(),这样您就不必不断地将从缩进中获得的对象强制转换为(Zombie)。最后,您可能需要使用(Zombie z:zombies){[…在这里使用z…]}因为您不关心迭代器变量。谢谢,但我仍然不明白如何使用您建议的代码。我已经编辑了我的答案以显示我在做什么。再次感谢!也许。对于(Zombie z:zombies){z.draw();}我很抱歉,我仍然不明白。它们仍然都是僵尸立即“fall”,并通过添加每一个来加速。我正试图通过按“z”键向数组中添加一个新的僵尸,但这部分代码与您的不同。我是否缺少一些知识?很抱歉,我没有深入研究这个问题,只解决了“无法从元素类型对象转换为僵尸。僵尸”“错误。你摆脱了这个,我不再收到这个错误消息,但是僵尸们的行为和以前一样:所有的僵尸”当一个人检测到它应该倒下时倒下。每增加一个僵尸,僵尸的速度就会增加。另一个问题,因为代码不会编译,很难说。你能不能分离一个可编译的代码片段?我不这么认为,但我已经把所有与僵尸有关的代码都放在了我的原始帖子中。谢谢你的耐心。
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;
    }
  }
}
//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);
  }
}