You can create an instance of a class at any point in your program. In the demo progam you cite, the first line just
declares the objects. The instances are actually created in lines 4-7 of
setup().
For your tank program, presumably you don't know in advance how many tank objects will be created (depends on how many times the user presses the mouse). You will therefore need to store the tank objects in some kind of dynamic collection that can grow as more objects are added. The easiest to use is probably the Java
Vector. The code below provides a simple example.
Code:Vector tanks;
void setup()
{
tanks = new Vector();
size(400,400);
}
void draw()
{
Iterator i = tanks.iterator();
while (i.hasNext())
{
Tank tank = (Tank)i.next();
tank.display();
}
if (mousePressed)
{
tanks.add(new Tank(random(10,width-10),random(10,height-10)));
}
}
class Tank
{
float x,y; // Tank location.
Tank(float x, float y)
{
this.x = x;
this.y = y;
}
// Draws the tank at its current location.
void display()
{
rect(x,y,10,10);
}
}
Obviously, your tank class will be more sophisticated than mine, but I hope you get the idea.
Oh, and just because it might help in getting your head round OO concepts, you create instances of
classes, not objects. An object is itself an instance of a class.