Classes, Object Orientated Programming? What’s that?
I like to think of classes as the design documents of code Robots that act on your behalf. You design these custom programming robots, that have characeristics and abilities (parameters and functions), which you can use to get things done with.
Processing can be a little tricky if your new to the idea. They basically have 4 parts. Check out the way it looks to the right.
- A name, I like to prepend the name with ‘cls’, it helps me from getting confused later on.
- Variables you need to keep track of
- The Constructor is how you initiate your class. You can write more then one constructor. Your constructors are used to set the initial conditions of your class variables.
- Functions will allow you to change your parameters of your class or other parameters relevant to your whole sketch.
Now if only someone could invent a robot, that could throw out my trash, do my laundry, cook, and greet me when I come home. wait…don’t they already have those?
Once defined, Using classes is really just 3 parts
- Give it a name
- Give Birth to it
- Use it’s functions/abilities
Look to the example on the right for these three parts. There really isn’t that much code, just a rather verbose explanation, so don’t be scared off yet.
Basic Class Definition Structure:
// 01 - We got to give the class a name
class clsRobot {
// 02 - Define Variables for Parmeters
// ( Characteristics your Robot will Have )
// 03 - Create Constructor
// ( A way make/giv birth to the Robots )
// 04 - Define Functions
// ( The abilities your Robot will Have )
}
Calling All Robots!!!
// A - We need to create something called
// - a class Object. It'll have a name.
// - In our case, 'roboticCat'
// - and it will be of the 'clsRobot' type
// - Think of it as Last Name, First Name
// - sort of like Doe, John
clsRobot roboticCat;
void setup() {
size(640,480);
// B - After you have named something
// - You have to give birth to it
// - So, in our case roboticCat,
// - is a new clsRobot object
roboticCat = new clsRobot();
// - If we had used our other constructor,
// - then we could have define its size upon birth
// - instead we'll use our default defined size
// roboticCat = new clsRobot(20);
}
void draw() {
background(0);
// C - Now that roboticCat exist
// - We can use its abilities
// - We want roboticCat to render() itself
roboticCat.render();
// Now let's have it chase the mouse
roboticCat.chaseMouse();
}
// 01
class clsRobot {
// 02
float headSize;
float posX;
float posY;
// 03a - A Constructor with out Parameters
clsRobot() {
// Default variables/Characteristics values
headSize = 10;
posX = width/2;
posY = height/2;
}
// 03b - A Constructor with a Parameter
// - Just incase we wanted to start off
// - with a different headsize
clsRobot(float hSize) {
headSize = hSize;
posX = width/2;
posY = height/2;
}
// 04a - We're gonna make our Robot draw itself
// - just to keep it simple
// - but you can get more creative
// - it just depends on your needs
void render() {
// Our robot will look like circle for now
ellipse( posX, posY, headSize, headSize );
}
// 04b - Let's make our Robot Chase the
// - mouse Cursor
void chaseMouse() {
posX = mouseX;
posY = mouseY;
}
}