如何在Processing中创建类的新实例

时间:2014-10-11 19:58:58

标签: arrays class instance processing

我在Processing工作,我正在尝试在我的代码中创建类Place的新实例。 Place(x,y)放置一个新的"炮塔"在我的网格上的某个地方。我创建了一个大小为10的数组,并将其分配给" turret"。当我点击" q"时,我想要它创建一个新的。我运行我的程序,当我尝试按下' q'它崩溃并说'Null Pointer异常,并用//注释突出显示下面的行。

PImage grid;
Place[] turret;
int g=0;
int i;

void setup()
{ 
  size(1501, 811);
  background(255);
  grid = loadImage("Grid.png");
  turret = new Place[10];
}
void draw()
{
  image(grid, 0, 0, 1503, 811);
  if (keyPressed)
    if (key == 'q')
    {
      turret[i] = new Place(1157, 405);  // Hitting 'q' on the keyboard causes a Null Pointer exception that points to this line
      i++;
      g=1;
    }
  if(g==1)
  {
    turret[i].circle();
    turret[i].keyPressed();
  }
}

class Place
{ 
  int xPos, yPos;
  Place(int x, int y)
  {
    xPos = x;
    yPos = y;
  }

  void circle()
  {
    ellipse(xPos, yPos, 30, 30);
  }

  void move(int amount, char xy)
  {
    frameRate(10);
    pushMatrix();
    translate(xPos, yPos);
    if (xy == 'x')
      xPos+=amount;
    if (xy == 'y')
      yPos+=amount;
    circle();
    popMatrix();
  }

  int v=0;
  void keyPressed()
  {
    if (keyPressed)
    {
      if (key == 'd' && v==0)
        move(30, 'x');
      if (key == 'a' && v==0)
        move(-30, 'x');
      if (key == 's' && v==0)
        move(30, 'y');
      if (key == 'w' && v==0)
        move(-30, 'y');
      if (key == 'x' && v==0)
      {
        v=1;
        fill(0);
      }
    }
  }
}

1 个答案:

答案 0 :(得分:1)

首先,draw()是一个循环,通过deafult,每秒被调用60次。因此,在抽奖中检查keyPressedmousePressed有点不精确。请参阅yoursef:

    int i; void draw(){if(keyPressed)i ++;的println(ⅰ);}

只要你按一个键,它就会增加i每次按下一个以上,更糟糕的是,每次都会有不同的数量。

因此,您最好使用回调函数keyPressed()代替

int i; void draw(){println(i);} void keyPressed(){i++;}

另一件事是,我认为逻辑有点奇怪。你增加我的方式的原因......

// i = 0
 if (keyPressed)
    if (key == 'q')
    {
     // you add a turret at index 0
      turret[i] = new Place(1157, 405);  // Hitting 'q' on the keyboard causes a Null Pointer exception that points to this line

// here ideally i = 1
 i++;
      g=1;
    }
  if(g==1)
  {

   // here you call circle and keyPressed on an empty slot in the array
   // turret [1] have no instance... hence the NPE
    turret[i].circle();
    turret[i].keyPressed();
  }
}

然后i永远不会重置,所以它会超出你的数组长度......

对于在运行时动态修改的列表,我更喜欢使用List,如ArrayList。但是你也可以使用数组来完成它。然后,您必须检查并将“迭代器”限制为阵列中占用的插槽。

以下是我认为您正在寻找使用ArrayList的内容: 关于keyPressed可能同样的事情对于类中的键处理是正确的:

ArrayList<Place> turret = new ArrayList<Place>();

void setup()
{ 
  size(1501, 811);
  background(255);
}
void draw()
{
  //if the list is not empty
  if (turret.size() > 0) {
    //it could be a regular for loop also
    for (Place p : turret) {
      p.circle();
      p.keyHandle();
    }
  }
}


void keyPressed(){
  // you can add as much Places as you want :)
  if(key == 'q'){
    turret.add(new Place(int(random(width)), int(random(height))));
  }
}

class Place
{ 
  int xPos, yPos;
  Place(int x, int y)
  {
    xPos = x;
    yPos = y;
  }

  void circle()
  {
    ellipse(xPos, yPos, 30, 30);
  }

  void move(int amount, char xy)
  {
    frameRate(10);
    pushMatrix();
    translate(xPos, yPos);
    if (xy == 'x')
      xPos+=amount;
    if (xy == 'y')
      yPos+=amount;
    circle();
    popMatrix();
  }

  int v=0;
  // you don't want to override processing keyPressed
  // unles you register it, so I'm changing the name
  void keyHandle()
  {
    if (keyPressed)
    {
      if (key == 'd' && v==0)
        move(30, 'x');
      if (key == 'a' && v==0)
        move(-30, 'x');
      if (key == 's' && v==0)
        move(30, 'y');
      if (key == 'w' && v==0)
        move(-30, 'y');
      if (key == 'x' && v==0)
      {
        v=1;
        fill(0);
      }
    }
  }
}

我真的会选择ArrayList,但这里有一个与array相同的例子。

PImage grid;
Place[] turret;
int nextInsert = 0;

void setup()
{ 
  size(1501, 811);
  background(255);
  //grid = loadImage("Grid.png");
  turret = new Place[10];
}
void draw()
{

  for(int i =0 ; i < turret.length; i++){ 
    if(turret[i] != null){
      turret[i].circle();
      turret[i].keyHandle();
    }

  }

}

void keyPressed(){
  if(key == 'q'){
    if(nextInsert < turret.length){
      turret[nextInsert] = new Place(int(random(width)), int(random(height)));
      nextInsert++;
    }
  }
}


class Place
{ 
  int xPos, yPos;
  Place(int x, int y)
  {
    xPos = x;
    yPos = y;
  }

  void circle()
  {
    ellipse(xPos, yPos, 30, 30);
  }

  void move(int amount, char xy)
  {
    frameRate(10);
    pushMatrix();
    translate(xPos, yPos);
    if (xy == 'x')
      xPos+=amount;
    if (xy == 'y')
      yPos+=amount;
    circle();
    popMatrix();
  }

  int v=0;
  void keyHandle()
  {
    if (keyPressed)
    {
      if (key == 'd' && v==0)
        move(30, 'x');
      if (key == 'a' && v==0)
        move(-30, 'x');
      if (key == 's' && v==0)
        move(30, 'y');
      if (key == 'w' && v==0)
        move(-30, 'y');
      if (key == 'x' && v==0)
      {
        v=1;
        fill(0);
      }
    }
  }
}