追加不适用于mousepressed?

时间:2013-05-02 17:23:37

标签: arrays append processing

我的项目存在问题,无法弄明白。处理告诉我我的不是一个数组,但我不知道它是怎么回事。此外,仅当我单击鼠标以尝试使其他内容出现时才会出现此问题。

int numParticles = 200;

GenParticle [] p = new GenParticle[numParticles];

float r=170;
float g=150;
float b=85;
float velX;
float velY;

void setup(){
  size(500,500);
  noStroke();
  smooth();
  frameRate(30);
  for (int i=0; i<p.length; i++){
     velX = random(-1,1);
     velY = -i;
    p[i]=new GenParticle(width/2,height/2,velX,velY,5.0,width/2,height/2);
  }
}

void draw(){
  fill(0,20);
  rect(0,0,width,height);

  fill(255,60);
  for(int i=0; i<p.length; i++){
    p[i].update();
    p[i].regenerate();
    p[i].display();
  }
}

void mousePressed() { 
    GenParticle gp = new GenParticle(mouseX,mouseY, velX, velY,5.0,mouseX,mouseY);
    p = (GenParticle[]) append(gp,p);
}


class GenParticle extends Particle{
  float originX,originY;


  GenParticle(int xIn,int yIn,float vxIn,float vyIn,float r,float ox,float oy){
    super(xIn, yIn, vxIn, vyIn, r);
    originX=ox;
    originY=oy;
  }

  void regenerate(){
    if ((x>width+radius) || (x<-radius) || (y>height+radius) || (y<-radius)){
      x=originX;
      y=originY;
      vx=random(-1.0,1.0);
      vy=random(-4.0,-2.0);
    }
  }
}


class Particle{
  float x, y;
  float vx,vy;
  float radius;
  float gravity=0.1;
  float r=0;
  float g=0;
  float b=0;

  Particle(int xpos,int ypos,float velx,float vely,float r){
    x=xpos;
    y=ypos;
    vx=velx;
    vy=vely;
    radius=r;
  }

  void update(){
    vy=vy+gravity;
    y += vy;
    x += vx;
  }

  void display(){
    fill(r,g,b);
    ellipse(x, y, radius*3,radius*3);

  if(mouseX>width/2){
    r=r+9;
     }else{
      g=g+6;
     }

  if(mouseY>height/2){
    b=b+7;
     }else{
      b=b-3;
     }

  if(keyPressed) {
    g=g+1;
     }else{
      g=g-5;
     }

  r=constrain(r,0,255);
  g=constrain(g,0,255);
  b=constrain(b,0,255);
  }
}

1 个答案:

答案 0 :(得分:1)

语法错误:数组是第一个参数,您想要追加的对象是append()函数的第二个参数

您的mousePressed应如下所示:

void mousePressed() { 
    GenParticle gp = new GenParticle(mouseX,mouseY, velX, velY,5.0,mouseX,mouseY);
    p = (GenParticle[]) append(p,pg);
}

注意数组p先行,然后是对象(附加到数组p object pg)。