|
Author |
Topic: Unlimited instances of a class on mousePressed? (Read 1070 times) |
|
Porkchop
|
Unlimited instances of a class on mousePressed?
« on: Feb 7th, 2005, 3:02am » |
|
Hi, I'm doing an exercice for a class, where you have to "stamp" a static image on the screen everytime the mouse is Pressed. I decided to push it a little further so it would stamp "animations" each time the mouse is Pressed. I know in flash I could have done something like this : Code: onClipEvent (mouseDown) { _root.attachMovie("logo", "logo"+counter, counter); _root["logo"+counter]._x = _xmouse; _root["logo"+counter]._y = _ymouse; _root["logo"+counter]._visible = true; counter++; } |
| However in processing (quite new to it and programming in general), I didn't find a function that would let me generate unlimited instances of an animation. So I started playing with classes and arrays. This is what I have, but I had to set a limit in the amount of objects the array has or else the program crashes : Code: /*****Global Variable declaration*****/ int timesPressed = 0; boolean arrayIsFilled = false; Logo[] myLogos; void setup(){ size(250, 250); background(0); noFill(); stroke(255); myLogos = new Logo[25]; } /*****Mouse Event*****/ void mousePressed(){ myLogos[timesPressed] = new Logo(); myLogos[timesPressed].x = mouseX; myLogos[timesPressed].y = mouseY; if(timesPressed < 24){ timesPressed++; }else{ timesPressed = 0; arrayIsFilled = true; //when the array has been filled, the previous elements will be assigned new coordinates. } } /*****Runs the program*****/ /* Since the program wont display array elements that didn't get their variables passed on (which results in an error) I made it so that it will only display the elements that have got their variables from the mouse event by checking if the array is filled. If its not, it will only display the elements up until they are equal to the timesPressed variable. */ void loop() { background(0); if(arrayIsFilled){ for (int i = 0; i < 25; i++) { myLogos[i].run(); } }else{ for (int i = 0; i < timesPressed; i++) { myLogos[i].run(); } } } /*****Defines the Class Logo*****/ class Logo{ //Private Variables, undefined ones are set during the mouse Event. float x; float y; int siz = 25; float rotation = 0.00; //Draws the logo and animate it void run(){ drawLogo(); if (rotation > TWO_PI){ rotation = 0; }else{ rotation+=0.01; } } //Draws the logo void drawLogo(){ ellipseMode(CENTER_DIAMETER); for(int i = 0 ; i < 3 ; i++){ push(); translate(x,y); rotateX(3*rotation); push(); rotateY(rotation*(i+3)); ellipse(0,0,siz/(i+1),siz/(i+1)); pop(); pop(); } } } |
| This works good, but I'd really like to be able to make the array continuously expand to accept new data(mouseX and mouseY for each instances). Am I approaching the problem from the right direction or should I turn around and do it in a completely different manner ?
|
|
|
|
st33d
|
Re: Unlimited instances of a class on mousePressed
« Reply #1 on: Feb 7th, 2005, 12:44pm » |
|
First, modulo. Code: int r = 7; //instead of if (r>TWOPI) r = 0; r += 3; r %= 8; //or r = (r+1)%8; //Look up modulo in reference - you'll need it println(r); |
| Dynamic arrays are bad practice. I harrassed the forum for some time about it and am only just getting out of the habit of using them. In Java you have to do some deep copy shenannigans in order to pull it off. The whole debate is here: http://processing.org/discourse/yabb/board_Syntax_action_displa_y_num_1091820063.html Arrays up to 3 Dimensions, and dynamic. That was about the point I learned about classes.
|
« Last Edit: Feb 7th, 2005, 12:45pm by st33d » |
|
I could murder a pint.
|
|
|
fry
|
Re: Unlimited instances of a class on mousePressed
« Reply #2 on: Feb 8th, 2005, 5:11pm » |
|
i don't think dynamic arrays themselves are bad practice, though multidimensional (2D and 3D) arrays tend to not be a great idea, since it's so hard to keep track of things. 1D dynamic arrays (a single set of brackets) works well, and is faster than using the Vector class, for instance. so in this example, a dynamic array of Logo[] objects would be fine.. Code:void mousePressed() { // use logoCount instead of timesPressed if (logoCount == myLogos.length) { // if the array is full, make one twice its size Logo temp[] = new Logo[logoCount * 2]; // copy its contents to the new array System.arraycopy(myLogos, 0, temp, 0, logoCount); // replace the old array with the new one myLogos = temp; } // then the rest procedes normally myLogos[logoCount] = new Logo(); myLogos[logoCount].x = mouseX; myLogos[logoCount].y = mouseY; logoCount++; } |
| the other option is to only allow 25 of them, and use the modulo operator as pointed out by st33d: Code:void mousePressed() { // if myLogos.length is 25, logoIndex will always // be a number between 0 and 24 as expected. int logoIndex = timesPressed % myLogos.length; myLogos[logoIndex] = new Logo(); myLogos[logoIndex].x = mouseX; myLogos[logoIndex].y = mouseY; timesPressed++; } |
|
|
« Last Edit: Feb 9th, 2005, 4:10pm by fry » |
|
|
|
|
Porkchop
|
Re: Unlimited instances of a class on mousePressed
« Reply #3 on: Feb 9th, 2005, 3:41am » |
|
hey thanx alot guys, I think I would have stayed in the dark for a long time I f I never knew about the arraycopy method. Also thanx for the modulo reference ! I though it made things spin really fast until I realised that the rotate function acepter only radians so I changed it into degrees : Code: int rotation = 359 rotation +=1; rotation %= 360; rotateX(radians(rotation)); |
| It works good, it does a one degree rotation per cycle ! Thanks again !
|
|
|
|
|