//class Name, captialize to distinguish from lowercase variables class Baby { /*class data - collection of variables referred to as instance variables since each instance of a class contains this set of variables.*/ float xpos, ypos, yspeed; color c; //CONSTRUCTOR /* The constructor is a special function inside of a class that creates object itself. It is where you give the instructions on how to set up the object. It is just like Processing's setup( ) function, only here it is used to create an individual object within the sketch, whenever a new object is created from this class. It always has the same name as the class and is called by invoking the new operator: Car myCar = new Car(); */ Baby() { //new block of code beyond setup() and draw() xpos = random(400); ypos = random(400); c = color(random(255), random(255), random(255), random(20,230)); yspeed = .5; } /* Add functionality to an object by writing methods as part of the contstructor. These are done in the same way as described in Chapter 7, with a return type, (function) name, arguments, and a body of code. */ void display() { ellipse(xpos, ypos, 20, 30); ellipse(xpos, ypos-20, 15, 15); ellipse(xpos-13, ypos-8, 14, 5); ellipse(xpos+13, ypos-8, 14, 5); } void fly() { ypos = ypos - yspeed; if(ypos < 0) { ypos = 400; } } } //May be its own tab Baby myBaby; //declare as a global variable void setup() { size(400,400); smooth(); myBaby = new Baby(); //initialize object or instance of Baby, "myBaby" by calling the class Baby and all data } void draw() { background(255); //operate baby object in draw by calling object methods (functions) using the dot syntax myBaby.fly(); myBaby.display(); }