passing array values to class method
in
Programming Questions
•
1 year ago
If anyone has a suggestion...I am stuck. The problem is with the 'twinkle' method at the end of code, but I'm posting all for context. I updated this old sketch that shows the Big Dipper twinkling with each star at a slightly different magnitude. As I've updated it to learn to write classes and arrays I'm finding I can't get the twinkle at different magnitude part to work. Each star grows and ebbs at the same rate and size. If you see down to line 69, the twinkle method in the Star class is where I'm passing array values for the maxSize of each. What I am finding is all stars will set their maxSize value by the highest value in the array no matter at what index that occurs, not their own maxSize value. Note all that text in the display method is just to see what values are being passed at each moment.
Any help is appreciated!
- PFont f;
- Star[] stars = new Star[7];
- int[] xpos = {50,200,300,400,400,600,700};
- int[] ypos = {100,110,200,290,450,500,360};
- int[] r = {255,200,255,255,255,255,200};
- int[] g = {255,200,255,255,230,255,200};
- int[] b = {200,255,200,255,150,255,255};
- float[] magnitude = {0.05,0.06,0.05,0.07,0.04,0.05,0.09};
- float[] maxSize = {5,3,5,2.5,5,3,4};
- String[] name = {"Alkaid","Mizar","Alioth","Megrez","Phad","Merak","Dubhe"};
- float base;
- void setup() {
- size (800,600);
- frameRate(30);
- f = createFont("Arial", 16, true);
- base = 1.0;
- //initialize stars
- for(int i = 0;i < stars.length;i++) {
- stars[i] = new Star(xpos[i],ypos[i],r[i],g[i],b[i],magnitude[i],maxSize[i],name[i]);
- }
- }
- void draw() {
- background(0);
- textFont(f);
- //display stars and make them twinkle
- for(int i = 0;i < stars.length;i++) {
- stars[i].twinkle();
- stars[i].display();
- }
- }
- class Star {
- int xpos;
- int ypos;
- int r;
- int g;
- int b;
- float magnitude;
- float maxSize;
- String name;
- Star(int xpos_, int ypos_, int r_, int g_, int b_, float magnitude_, float maxSize_, String name_) {
- xpos = xpos_;
- ypos = ypos_;
- r = r_;
- g = g_;
- b = b_;
- magnitude = magnitude_;
- maxSize = maxSize_;
- name = name_;
- }
- void display() {
- strokeWeight(base);
- stroke(r,g,b);
- point(xpos,ypos);
- textAlign(LEFT);
- text("max " + int(maxSize),xpos-25,ypos-5);
- text("mag " + magnitude,xpos+35,ypos-5);
- text("base " + int(base),xpos,ypos+20);
- text(name,xpos,ypos-20);
- }
- void twinkle() {
- base = base + magnitude;
- if(base > maxSize || base <= 1) {
- magnitude = magnitude*=-1;
- }
- }
- }
1