Array of animated objects
in
Programming Questions
•
3 months ago
Hi,
I'm trying to create an array of objects that are created at mouse position and animated on the x axis.
My problem is that each time I press the mouse the previous object disappear.
How can I make all the objects in the array co exist on-screen?
- Anim[] anim; // object declaration
- boolean animExist; // checks if the objects exist
- int animIndex; // array index for anim object
- void setup()
- {
- size (600, 600);
- animIndex = -1; // as the mouse is pressed this is increased to 0 (first object in the array)
- anim = new Anim [10]; // size of the array;
- animExist = false; // the object does not exist until the mouse is pressed
- }
- void draw()
- {
- background (0);
- if (animExist) // if the mouse is clicked at least once...
- {
- anim[animIndex].run(); // the object is created
- }
- }
- void mousePressed()
- {
- animExist = true; // the mouse was pressed so the object can be created
- if (animIndex >= anim.length - 1) // avoid the number of objects to exceed the size of the array
- animIndex = -1;
- animIndex++;
- anim [animIndex] = new Anim (mouseX, mouseY);
- }
- public class Anim
- {
- int x;
- int y;
- int mov;
- int movmax;
- Anim (int _x, int _y)
- {
- x = _x;
- y = _y;
- movmax = 300;
- }
- void run()
- {
- rect (x, y, 20, 20);
- x++;
- }
- }
Another problem I would like to solve is to delete the object as the x position reaches x + movmax...
How can I delete an object after a specific condition is met?
Thank you
1