Issues with array and mouse pressed
in
Programming Questions
•
5 months ago
I have been to the wikipedia page, researched it within the processing website, and looked at examples. I cannot figure this problem out. My goal is to make multiple "fireworks" when I click the mouse, but I keep getting an error that something is wrong with the array, but it looks like all of the examples I have looked at!
- 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