Hi all,
My understanding of OOP is that in order to create a Class, you need to define 1) variables, 2) constructor and 3) methods. In the example below though, variables x and theta are 1) defined, their values assigned in 2) the constructor, however the x formula to determine its value must be placed as a method. If I put it in the constructor the oscillator won’t move, but if I move it inside void.display() it will.
Why is that??
Thanks!
class Oscillator {
//Variables
float theta; //oscillation angle
float x;
//Constructor
Oscillator() {
theta = 0;
}
//Methods
void move() {
//Increment theta with each cycle to create movement
theta += 0.05;
}
void display() {
//draw the ellipse at the value produced by sine
x = map(sin(theta),-1,1,0,width); // this can't go in the constructor?!?
fill(0);
stroke(0);
line(width/2,0,x,height/2);
ellipse(x,height/2,20,20);
}
}