Lots and Lots of Things

Arrays

Arrays are a convient way to handle multiple items of the same data type (integers, objects or anything else).

This is a representation of an int array containing 9 elements:

------------------------------------------
| 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128 | 256 |
------------------------------------------

The code to create this array might be something like the following:

int[] myArray = {1,2,4,8,16,32,64,128,256};

or it could be done using a for loop

int[] myArray = new int[9];
for (int i = 0; i < myArray.length; i++)
{
	myArray[i] = pow(2,i);
}

nameOfArray.length gives you the number of elements in the array.

nameOfArray[indexNumber] gives you the individual element of the array at a given index number. Remember, index numbers start at 0 so the first index of the array is 0 and the last one in our array is 8.

pow(base, exponent) is a built-in processing function for the math expression of power. 2 to the power of 2 is 4 (in other words squared). 2 power of 3 is 8 (in other words, 2 cubed). Anyh base to the power of exponent is the base multiplied by itself the exponent number of times. 2 to the power of 4 is 2 x 2 x 2 x 2 or 16.

For Loops, as you can see above, are great for dealing with arrays. Start at 0 and go to the length of the array to perform the same operation on each element in the array.

Arrays and Objects

If we develop a Ball Class such as follows:

// The name of our class
class Ball
{
  // our Class variables.  
  int wWidth;
  int wHeight;
  int bSize;
  
  int x, y;
  int xDirection, yDirection;
  
  // Constructor.  This gets called when we create a "new Ball"
  Ball(int windowWidth, int windowHeight, int ballSize)
  {
    wWidth = windowWidth;
    wHeight = windowHeight;
    bSize = ballSize;
    x = 1;
    y = 1;
    xDirection = 1;
    yDirection = 1;
  }

  Ball(int windowWidth, int windowHeight, int ballSize, int xPosition, int yPosition, int xDir, int yDir) 
  {
    this(windowWidth, windowHeight, ballSize);
    x = xPosition;
    y = yPosition;
    xDirection = xDir;
    yDirection = yDir;
  }
  
  // Our method to determine current position of the ball.
  void compute()
  {
    if (x < wWidth && x > 0)
    {
      // Move along x axis
      x += xDirection;
    }
    else
    {
      // Change direction, from positive to negative and vice versa
      xDirection = xDirection * -1;
      x += xDirection;
    }
    
    if (y < wHeight && y > 0)
    {
      y += yDirection;    
    }
    else
    {
      yDirection = yDirection * -1;
      y += yDirection;
    }
  }
  
  // This actually displays our ball, it gets called in the main draw function
  void display()
  {
    compute();  // first we run the computation
    ellipseMode(CENTER);
    ellipse(x,y,bSize,bSize);   
  }

}

And decide that we want multiple Ball's, we can use an "array" of Ball objects like so:

Ball[] ourBalls;  // Declare array of Ball objects called ourBalls
int numBalls = 50;  // An integer to hold the number of Ball objects we are going to create

void setup()
{
  size(500,500);
  ourBalls = new Ball[numBalls];  // Initalize the array, passing in the brackets the number of Ball objects
  for (int i = 0; i < ourBalls.length; i++)  // Use our nifty for loop, starting at 0 and going to the number of objects in the array.  Incrementing i by 1 each time.
  {
	// Using the overloaded constructor
	// Ball(int windowWidth, int windowHeight, int ballSize, int xPosition, int yPosition, int xDirection, int yDirection)
	// Picking some values just to see what happens    
	// Create a Ball object for each element in the array.
	ourBalls[i] = new Ball(width, height, i+10, i+1, i-1, i, 1 - i);
  }
}

void draw()
{
  background(0);
  // Use another for loop to tell each of the Ball objects to display
  for (int i = 0; i < ourBalls.length; i++)
  {
	ourBalls[i].display();
  }
}