Need some help with dynamic object creation.

Hey guys, im not really that far with processing yet so bear with me!

So I have this code:

// create an empty array for 10 MovingCircle objects
MovingCircle[] myCircleArray = new MovingCircle[10];

void setup() {
  size(400, 400);
  smooth();




}

void draw() {
     for(int i=0; i<myCircleArray.length; i++) {
    myCircleArray[i] = new MovingCircle(10,10+i*10,10);  
  }
  background(0);

  // iterate through every moving circle
  for(int i=0; i<myCircleArray.length; i++) {

    myCircleArray[i].update();
    myCircleArray[i].drawCircle();


  }


}

void mousePressed() { // if mouse pressed
println("changed");
myCircleArray = new MovingCircle[4]; //create new circles
}


class MovingCircle {

  float x;
  float y;


  float circleSize;

  MovingCircle(float xpos, float ypos, float csize) {
    x = xpos;
    y = ypos;
    circleSize = csize;
  }

  void update() {
  }


  void drawCircle() {

    fill(255);
    ellipse(x, y, circleSize, circleSize);

  }


}

Basically what is does is that it's an object based (I used an example and fitted it to my needs) script, it can generate the amount of circles I put in on the beginning at "new MovingCircle[10];"
I already moved this piece of code

for(int i=0; i<myCircleArray.length; i++) {
myCircleArray[i] = new MovingCircle(10,10+i*10,10);

from setup to draw so it can check it every time.

Im asking for help on the part at void mousePressed(), I figured out how to change the amount of objects but I want it to be one more each time I press the mouse button. Like "++".

I hope you guys understand, im not a native speaker! Thanks for the help, Ruzzle.

Comments

  • edited February 2015

    you wrote:

    I already moved this piece of code from setup to draw so it can check it every time.

    it really should stay in setup()...

    Your question: adding elements

    look at ArrayList: here you can easily add elements to your list of objects...

    ArrayList<MovingCircle> myCircleArray = new ArrayList();

    in setup():

        for(int i=0; i<myCircleArray.length; i++) {
            myCircleArray.add ( new MovingCircle(10,10+i*10,10) ); 
        }
    

    in mousePressed:

     myCircleArray.add ( new MovingCircle(mouseX,mouseY,10) ); 
    

    in draw()

      // iterate through every moving circle
      for (int i=0; i<myCircleArray.size(); i++) {
    
        MovingCircle myCircle = myCircleArray.get(i);
        myCircle.update();
        myCircle.drawCircle();
    
    
      }
    

    ;-)

  • In that case, you should either use a variable for the amount of circles you want, or use some trickery like this: myCircleArray = newMovingCircle[myCircleArray.length + 1];

    But suppose you wanted to use a variable. Every time you clicked, you would use the increment (++) operator on the variable, and then just make the array of that size:

    int arraySize = 1;
    arraySize++;
    myCircleArray = newMovingCircle[arraySize]; //Your array is now 2 elements long.
    
  • Why are you creating the array at the beginning of draw() each time?

    This sounds like a job for an ArrayList of MovingCircles.

  • Okay I think I understand @Chrisir (does this work here? :p) But the code still doesn't seem to work...

    // create an empty array for 100 MovingCircle objects
    MovingCircle[] myCircleArray = new MovingCircle[10];
    
    void setup() {
      size(400, 400);
      smooth();
    
      // and actually create the objects and populate the
      // array with them
    
     for(int i=0; i<myCircleArray.length; i++) {
        myCircleArray.add ( new MovingCircle(10,10+i*10,10) ); 
    }
    }
    
    void draw() {
    
      background(0);
    
    // iterate through every moving circle
    for (int i=0; i<myCircleArray.size(); i++) {
    
      MovingCircle myCircle = myCircleArray.get(i);
      myCircle.update();
      myCircle.drawCircle();
    
    
    }
    
      }
    
    void mousePressed() {
    myCircleArray.add ( new MovingCircle(mouseX,mouseY,10) ); 
    }
    
    
    class MovingCircle {
    
      float x;
      float y;
      float xSpeed;
      float ySpeed;
    
      float circleSize;
    
      MovingCircle(float xpos, float ypos, float csize) {
        x = xpos;
        y = ypos;
    
    
        circleSize = csize;
    
      }
    
      void update() {
        x += xSpeed;
        y += ySpeed; 
      }
    
    
      void drawCircle() {
    
        fill(255);
        ellipse(x, y, circleSize, circleSize);
    
      }
    
    
    }
    

    It gives me the error:

    Cannot invoke add (Project.MovingCircle) on the array type Project.MovingCircle[]

  • That's because you're mixing ArrayList code with an array.

    The get() and add() functions belong to the ArrayList class. They don't work with arrays.

    Use an ArrayList instead.

  • ArrayList<MovingCircle> myCircleArray = new ArrayList();

  • Okay so I change the array MovingCircle[] myCircleArray = new MovingCircle[10];

    to

    ArrayList myCircleArray = new ArrayList();

    ?

    I'm sorry im not really that far with arrays and stuff. Thanks for the help anyway!

  • edited February 2015

    It doesn't work... It gives the error: myCircleArray.length cannot be resolved or is not a field

  • Oh I get it, It was suppose to be size() not length thanks for the help

  • yeah, size()

    ;-)

  • And so the lesson here is, if you want to create objects dynamically, use a structure that allows for dynamic object creation!

  • Wow thanks so much TfGuy44, unfortunately i'm not that advanced with Processing, this is however a forum for questions about processing. So if you don't like amateurs: leave.

  • Whoa, easy tiger...

    TfGuy didn't want to be rude.... or sarcastic....

    this is a forum for beginners and TfGuy is really helping a lot of people here....

  • @Ruzzle: Nobody has been rude to you. You have no reason to tell anybody to leave. People here are helping, for free, in their spare time.

    I suggest you try not to be so defensive. This is a technical forum, so we don't really have time for immature flame wars.

Sign In or Register to comment.