
If you're wondering why I havn't posted much on processing in a while it's because I am currently learning probably the most challenging and uncreative part of the language. I am learning syntax essentially and how to make the code easier, and to be able to move further on with the code.
The two bits I am racking my brain to try and thoroughly understand atm are objects and functions. A function in processing is something like a square or a circle, something that processing knows how to draw. Creating your own functions means you could create a collection of shapes or a new shape, then make it into a function so that it can be called like a normal function.
For example, if I made a collection of shapes to represent a cat, and made it into a function I could then tell processing to drawCat; appose to having to write rect(100, 100, 200, 100); etc.
Objects I'm still just beggining to fathom. The idea is you can put the variables (attributes of something, for example the attribute of a cat may be it's size and colour) the setup (start the cat) and the draw (draw the cat, it's colour and size) into one code instead of putting it into the processing code in seperate areas. In other words it's all about keeping code clean, tidy and easier to understand and edit.
Example:
//Declare the cat exists
Cat myCat;
//Start object
void setup() {
size(400, 400);
myCat = new Cat();
}
//Draw background
void draw() {
background(255);
//Draw cat
myCat.display();
}
//Cat variables
class Cat {
color c;
float xpos;
float ypos;
float xspeed;
// Giving the variables values
Cat() {
c = color(0);
// dividing the width and height by 2 makes the position the center of the canvas
xpos = width/2;
ypos = height/2;
}
//Then add a function to it to draw the cat
void display() {
ellipseMode(CENTER);
fill(c);
ellipse(xpos, ypos, 100, 100);
ellipse(xpos, ypos+120, 170, 250);
triangle(xpos-20,ypos-30,xpos-35,ypos-60, xpos-45, ypos-20);
triangle(xpos+20,ypos-30,xpos+35,ypos-60, xpos+45, ypos-20);
}
}
This will give you this:
Which fair enough we could get ordinarily anyway by using just the simple draw function. However you can now load that object as many times as you want, and you can create a new processing file specifically for the cat so that it doesn't clutter up your main proccessing document. Just a way to keep more organized and make it easier. If you also change the cat code to include arguments (similar to variables or attributes) you will then be able to load the object in again and also change aspects of it. This is what I am learning atm. It all seems very complicated for the sake of it but trying to power through in the hope it will make my life easier when it comes to making more complex things later.