Help with classes/arrays
in
Programming Questions
•
10 months ago
HELLO!
I got this "explosion simulator" and I would like to know how I can keep making new explosions when I click the mouse instead of replacing the original explosion like it does now. Also, Is it possible to have a class create an array of itself?
PS: I changed the shape to rectangles because they look cooler.
I got this "explosion simulator" and I would like to know how I can keep making new explosions when I click the mouse instead of replacing the original explosion like it does now. Also, Is it possible to have a class create an array of itself?
PS: I changed the shape to rectangles because they look cooler.
- Ball[] manyBalls = new Ball[200];
- void setup() {
- size(900, 600);
- for (int i=0; i < manyBalls.length; i++) {
- manyBalls[i] = new Ball(width/2, height/2);
- }
- }
- void draw() {
- fill(255, 50);
- rect(0, 0, width, height);
- for (int i=0; i < manyBalls.length; i++) {
- manyBalls[i].move();
- }
- }
- void mousePressed() {
- for (int i=0; i < manyBalls.length; i++) {
- manyBalls[i] = new Ball(mouseX, mouseY);
- }
- }
- class Ball {
- float ballx;
- float bally;
- int ballSize = (int)random(5, 60);
- float ballspeed = random(2, 5);
- float ballxdirection = random(-1, 1);
- float ballydirection = random(-1, 1);
- int r = (int)random(254);
- int g = (int)random(254);
- int b = (int)random(254);
- int a = (int)random(50, 150);
- Ball(float tempballx, float tempbally) {
- ballx = tempballx;
- bally = tempbally;
- }
- void move() {
- if (ballx > width-ballSize/2 || ballx < ballSize/2)
- ballxdirection *= -1;
- if (bally > height-ballSize/2 || bally < ballSize/2)
- ballydirection *= -1;
- ballx += ballspeed*ballxdirection;
- bally += ballspeed*ballydirection;
- fill(r, g, b, a);
- noStroke();
- rect(ballx, bally, ballSize, ballSize);
- a -= 4;
- }
- }
1