-1

我希望能够从我在舞台上单击的任何位置创建新粒子。我只是坚持添加鼠标部分,我试图添加/传递参数,但我在尝试设置参数时总是出错。有什么建议么?

这是我当前的代码:

float parSpeed = 1; //speed of particles
int   nParticles = 1000; //# of particles
Particle[] particles;

void setup() {
  size(700,700); //size of image (8.5 x 11 @ 300px) is 3300,2550)
  frameRate(60); //framerate of stage
  background(0); //color of background
  particles = new Particle[nParticles];

//start particle array
for(int i=0; i<nParticles; i++) {
    particles[i] = new Particle();
  }
}

void draw() {
  fill(0,0,0,5); //5 is Alpha
  rect(0,0,width,height); //color of rectangle on top?
  translate(width/2, height/2); //starting point of particles

//start particle array
  for(int i=0; i<nParticles; i++) {
    particles[i].update();
    particles[i].show();
  }
}

//Particle Class
class Particle {
  PVector pos; //position
  float angle; //angle
  float dRange; //diameter range
  float dAngle; //beginning angle?
  color c; //color

  Particle() {
    pos = new PVector(0,0);//new position for the particles.
    angle  = 1; //controls randomization of direction in position when multiplied
    dRange = 0.01; // how big of a circle shold the particles rotate on
    dAngle = 0.15; // -- maximum angle when starting
    c = color(0,0,random(100, 255)); //set color to random blue
  }

  void update() {
    float cor = .25*dRange*atan(angle)/PI; 
    float randNum = (random(2)-1)*dRange-cor;  //Random number from (-dRange, dRange)
    dAngle+=randNum;                       //We don't change the angle directly
                                       //but its differential - source of the smoothness!

    angle+=dAngle;                         //new angle is angle+dAngle -- change angle each frame

    pos.x+=parSpeed*cos(angle);//random direction for X axis multiplied by speed
    pos.y+=parSpeed*sin(angle);//rabdin durectuib for y axis multiplied by speed
  }

void show() {
    fill(c); //fill in the random color
    noStroke(); //no stroke
    ellipse(pos.x,pos.y,10,10); //make the shape
    smooth(); //smooth out the animation
  }
}

void keyPressed() {
  print("pressed " + int(key) + " " + keyCode);
  if (key == 's' || key == 'S'){
    saveFrame("image-##.png");
  }
}

void mouseReleased() {
   print("mouse has been clicked!"); 
}
4

1 回答 1

0

覆盖mouseReleased ()方法:

在那里你需要:

  1. 捕捉鼠标的位置
  2. 创建新粒子
  3. 更新新创建的粒子的位置。
  4. 将其添加到数组(粒子系统)

这可能看起来很简单,但您必须记住数组不能更改大小。我建议您创建一个 ParticleSystem 类,该类负责在系统中添加和删除粒子。

编辑:您可能需要考虑使用 ArrayList 而不是粒子数组。看看这个

在伪代码中,它看起来像这样:

void mouseReleased() {
    int particleX = mouseX;
    int particleY = mouseY;

    Particle P = new Particle(); 
    P.setPos ( new PVector ( particleX, particleY ) ); // this needs to be implemented

    ParticleSystem.add ( P ); // this needs to be implemented
}

我希望这将是一个好的开始。

于 2013-01-03T04:18:25.523 回答