one element in the array taking another ones place
in
Programming Questions
•
1 month ago
hi everyone,
how do i setup condition that when after say 10 balls are added to the sketch
ball 11 kills ball 0 and takes its place, ball 12 kills ball 1 and takes it's place and so on
therefore never letting balls to exceed 10 ball limit?
//balls
how do i setup condition that when after say 10 balls are added to the sketch
ball 11 kills ball 0 and takes its place, ball 12 kills ball 1 and takes it's place and so on
therefore never letting balls to exceed 10 ball limit?
//balls
- final static ArrayList<Ball> balls = new ArrayList();
- int counter;
- void setup() {
- size(600, 600);
- smooth();
- }
- void draw() {
- background(0);
- counter+=1;
- for (Ball b: balls) b.run();
- //create new ball with random location
- if (counter%50==0) {
- balls.add( new Ball(random(width), random(height)) );
- }
- }
- //BALL CLASS
- class Ball {
- float x, y;
- float speedX=20;
- float speedY=5;
- Ball(float _x, float _y) {
- x=_x;
- y=_y;
- }
- void run() {
- display ();
- move();
- bounce();
- gravity();
- }
- void gravity() {
- speedY+=0.2;
- }
- void bounce() {
- if (x>width) {
- speedX=speedX*-1;
- }
- if (x<0) {
- speedX=speedX*-1;
- }
- if (y>height) {
- speedY=speedY*-1;
- }
- if (y<0) {
- speedY=speedY*-1;
- }
- }
- void move() {
- x=x+speedX;
- y=y+speedY;
- }
- void display() {
- ellipse(x, y, 20, 20);
- }
- }
1