I need help figuring out how to do something

edited October 2015 in Questions about Code

For my class we need to use 2 for loops one in the setup and one draw, and use one array. Our goal is to create an explosion where you click your mouse. I was able to create the Explosion but I'm confused on how I can get the explosion to be where the mouse is clicked. Here's my code explosioncode

that's all the code except for 2 curly brackets at the end.

Tagged:

Answers

  • Can you format your code using the code button?

  • I just edited it

  • What you have posted now is EVEN LESS helpful. We can't copy the code out of an image!

    FFS

    int num=100; 
    int [] x; 
    int [] y; 
    color [] c;
    
    void setup() { 
      size(800, 800);
    
      x=new int[num]; 
      y=new int[num]; 
      c = new color[num]; 
      for (int i=0; i<x.length; i++) { 
        x[i]=int(random(width)); 
        y[i]=int(random(height)); 
        c[i]=color(random(255), random(255), random(255));
      } 
      println(x.length);
    }
    
    void draw() { 
      background(0); 
      if (mouseButton==LEFT) { 
        for (int i=0; i<x.length; i++) { 
          fill(c[i]); 
          ellipse(x[i], y[i], 30, 30); 
          if (x[i]<width/2) { 
            x[i]--;
          } 
          if (mouseX>width/2) { 
            x[i]++;
          } 
          if (y[i]<height/2) { 
            y[i]--;
          } 
          if (y[i]>height/2) { 
            y[i]++;
          }
        }
      }
    }
    
  • sorry I'm still new to this site, how do I format the code on here?

  • Right, now that we have your code, we can start working on it. You have several arrays of variables, where the i'th variable in one of those arrays belongs to the i'th ellipse. While this works just fine, it's not good OOP. Instead of doing it this way, let's teach you some actual programming and move ahead to using Object Orientated Programming, with real objects.

    class Dot {
      float x, y, dx, dy;
      color c;
      Dot() {
        x = -200;
        y = -200;
        dx = 0;
        dy = 0;
        c = color(random(255), random(255), random(255));
      }
      void draw() {
        x+=dx;
        y+=dy;
        noStroke();
        fill(c);
        ellipse(x, y, 5, 5);
      }
      void reset(){
        x = mouseX;
        y = mouseY;
        float r = random(TWO_PI);
        float s = random(2,20);
        dx = s * cos(r);
        dy = s * sin(r);
        c = color(random(255), random(255), random(255));
      }
    }
    
    int num = 100;
    Dot[] dots;
    
    void setup() { 
      size(800, 800);
      dots = new Dot[num];
      for (int i=0; i<num; i++) dots[i] = new Dot();
    }
    
    void draw() { 
      background(0);
      for (int i=0; i<num; i++) dots[i].draw();
    } 
    
    void mousePressed(){
      for (int i=0; i<num; i++) dots[i].reset();
    }
    
  • Well, you can either read the sticky about how to format code... http://forum.processing.org/two/discussion/8045/how-to-format-code-and-text#latest

    Or, the short version: Highlight code, press Ctrl+o.

  • My class is actually going to be learning OOP this week after our lab is due.

  • Then you'll have to make the transition from arrays of variables to objects, but in reverse. You'll need an array for each variable in the class...

  • So you mean I need to set a for loop for each axis?

  • You should only need one for loop each in draw(), mousePressed(), and setup().

    You'll probably need five arrays. Let's say they're called x, y, dx, dy, and c.

    In setup(), your for loop - lets say it loops using the variable i - should setup the values for each of the arrays of variables you'll have. That is, it'll set the values for x[i], y[i], dx[i], dy[i], and c[i] for each value of i.

    The draw() function's loop will update the values in x[i] and y[i] (for each value of i) based on the values of dx[i] and dy[i]. Then it will use the i'th variables from all of the arrays to render the dot.

    The loop in mousePressed() should set all the values for all the dots to some new values... values that will cause each dot's position to be set to where the mouse was clicked, and give it a new, random color (the c array) and velocity (which is what dx and dy arrays are).

    Try this yourself now, and post the code of your attempt for more help, if you need it.

  • So I'm very close I think, the only problem is the dots are present when the program begins.

      int num=100;
    float [] x;
    float [] y;
    float [] dx;
    float [] dy;
    color [] c;
    void setup() {
      size(800,800);
     x=new float[num];
     y=new float[num];
      dx=new float[num];
     dy=new float[num];
        c = new color[num];
     for(int i=0; i<x.length;i++){
       x[i]=(random(width));
       y[i]=(random(height));
       dx[i]=(random(-10,10));
       dy[i]=(random(-10,10));
       c[i]=color(random(255),random(255),random(255));
    }
     println(x.length);
    }
    void draw() {
      background(0);
    
      for(int i=0; i<x.length;i++){
       x[i]+=dx[i];
       y[i]+=dy[i];
         fill(c[i]);
        ellipse(x[i], y[i],30,30);
    
    } 
    }
    
    void mousePressed() {
      for(int k=0; k<x.length;k++){
    
        ellipse(x[k],y[k],30,30);
      x[k]=mouseX;
      y[k]=mouseY;
      dx[k]=(random(-20,20));
       dy[k]=(random(-20,20));
       c[k]=color(random(255),random(255),random(255));
    }
    
    }
    
Sign In or Register to comment.