OOP problem with new variable for each object
in
Programming Questions
•
1 year ago
Hi,
I am having a problem with an OOP. Each object should have a different xRand value but this is not happening.
Does anyone have any ideas.
Thanks,
Shane
I am having a problem with an OOP. Each object should have a different xRand value but this is not happening.
Does anyone have any ideas.
Thanks,
Shane
- int numSpots = 2;
- int amplitude = 30;
- float frequency = PI/24;
- float angle;
- float x, xRand, y;
- //Declare and create the array
- Spot[] spots = new Spot[numSpots];
- void setup() {
- size(600, 300);
- smooth();
- noStroke();
- for (int i = 0; i < spots.length; i++) {
- x = random(50, width-amplitude);
- xRand = x;
- y = random(30, height/2);
- //Create each object
- spots[i] = new Spot(x, xRand, y, 15, frequency);
- }
- }
- void draw() {
- //frameRate(24);
- fill(0);
- rect(0, 0, width, height);
- fill(255);
- for (int i = 0; i < spots.length; i++) {
- spots[i].move(); //Move each object
- spots[i].display(); //Display each object
- }
- }
- class Spot {
- float x, y, diameter, frequency;
- int direction = -1;
- //Constructor
- Spot(float xpos, float xRandpos, float ypos, float dia, float fr) {
- x = xpos;
- xRand = xRandpos;
- y = ypos;
- diameter = dia;
- frequency = fr;
- }
- void move() {
- x = xRand + (sin(angle))*amplitude;
- angle += frequency;
- println(xRand);
- }
- void display() {
- ellipse(x, y, diameter, diameter);
- }
- }
- boolean bStop;
- void mousePressed()
- {
- bStop = !bStop;
- if (bStop)
- noLoop();
- else
- loop();
- }
1