Monday, May 16, 2011

Simple Class example

the pervious two example of class I showed were very complex ones so here is the code for a very basic object and class based example.
HLine h1 = new HLine(20, 2.0);
HLine h2 = new HLine(50, 2.5);
void setup()
{
size(200, 200);
frameRate(30);
}
void draw() {
background(204);
h1.update();
h2.update();
}
class HLine {
float ypos, speed;
HLine (float y, float s) {
ypos = y;
speed = s;
}
void update() {
ypos += speed;
if (ypos > width) {
ypos = 0;
}
line(0, ypos, width, ypos);
}
}

In this code two objects, h1 and h2 are created from the class HLine.
The constant in the class in the x position of the line which is from0 to the width of the screen and the variables are the y position and speed at which the lines move

No comments:

Post a Comment