OOP random x-position problem
in
Programming Questions
•
1 year ago
Hi,
In this programme all spots have the same x-position. I would like them to oscillate about random x-positions. But that not working?
Line 38 is I think where the problem is.
I am new to OOP so any help would be greatly appreciated.
Thanks,
Shane
In this programme all spots have the same x-position. I would like them to oscillate about random x-positions. But that not working?
- int numSpots = 4;
- int amplitude = 30;
- float frequency = PI/120;
- float angle;
- //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++) {
- float x = random(50, width-amplitude);
- float y = random(30, height/2);
- //Create each object
- spots[i] = new Spot(x, y, 3, frequency);
- }
- }
- void draw() {
- fill(0, 12);
- 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 ypos, float dia, float fr) {
- x = xpos;
- y = ypos;
- diameter = dia;
- frequency = fr;
- }
- void move() {
- x =+ (sin(angle) + HALF_PI)*amplitude;
- angle += frequency;
- }
- void display() {
- ellipse(x, y, diameter, diameter);
- }
- }
Line 38 is I think where the problem is.
I am new to OOP so any help would be greatly appreciated.
Thanks,
Shane
1