reach an item in array
in
Programming Questions
•
1 year ago
Hi everybody
i am trying to reach a certain item in an array.
For exemple, my array display 10 ball and i want the ball n°2 to be fill with a different color
- //Declare
- Ball [] ballCollection = new Ball[200];
- void setup(){
- size(600,600);
- smooth();
- //Initialize
- for( int i = 0 ; i < ballCollection.length ; i ++){
- ballCollection[i] = new Ball(random(0,width),random(0,200));
- }
- }
- void draw(){
- background(0);
- //Call Functionality
- for( int i = 0 ; i < ballCollection.length ; i ++){
- ballCollection[i].run();
- // stroke (255,0,0);
- // ballCollection[5].run();
- // noStroke();
- }
- }
- // and the class
- class Ball {
- // Global variables
- float x = 0;
- float y = 0;
- float speedX = random(-4,4);
- float speedY = random(-1,1);
- // Constructor
- Ball(float _x, float _y) {
- x = _x;
- y = _y;
- }
- //Functions
- void run() {
- // fill(random(0,255),0,0);
- 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 += speedX;
- y += speedY;
- }
- void display() {
- ellipse(x, y, 20, 20);
- }
- }
the three commented line is one of my attempt to do it. Any clue ?
Thank you !
1