Calling variables from classes.
in
Programming Questions
•
9 months ago
Hi guys, got a question about using variables declared in an object in the main code. Say for example, is it possible to extract and use the same
lenX variable from my
Deflector class in the main code?
- Deflector mouseDeflector;
void setup(){
size(500,500);
mouseDeflector = new Deflector(0.01);
}
void mousePressed(){
mouseDeflector.mousePressed();
}
void mouseReleased(){
mouseDeflector.mouseReleased();
}
void draw(){
background (128);
mouseDeflector.draw();
}
//===================================================================================
class Deflector
{
int lenX=0;
int lenY=0;
float rotRate;
float R = 0;
boolean extend = false;
boolean shrink = false;
Deflector(float _rotRate)
{rotRate = _rotRate;
}
void mousePressed(){
if (mouseButton == LEFT && !extend){
extend = true;
} else if (mouseButton == RIGHT && !shrink){
shrink = true;
}
}
void mouseReleased(){
if (extend){
extend = false;
}
if (shrink){
shrink = false;
}
}
void draw(){
background(128);
lenX += extend ? 1:0;
lenX += shrink ? -1:0;
lenY += extend ? 1:0;
lenY += shrink ? -1:0;
if (lenX<=0){
lenX=0;
}
if (lenY<=0){
lenY=0;
}
if (lenX>= width/4){
lenX=width/4;
}
if (lenY>= width/4){
lenY=width/4;
}
R = R + rotRate;
pushMatrix();
translate (mouseX, mouseY);
rotate (R);
line (0, 0, lenX, lenY);
popMatrix();
}
}
1