We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
you wrote:
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():
in mousePressed:
in draw()
;-)
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:
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...
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!
yes.
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()
;-)
see also reference
https://www.processing.org/reference/ArrayList.html
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.