Hi,
I’ve probably not described the problem in the title properly. The error I get is
‘sx cannot be resolved or is not a field’
Me again. I’m adapting an old sketch just as a practice tool to help cement the various concpets of using sine fucntions, classes, and arrays, in my head. The code I have works until I place the Flower class in an array and try to call flower.sx in the Petal array class. It’s a bit clear if I post the code.
Flower [] flower = new Flower[10];
Petal [] petal = new Petal[10];
void setup(){
size(400,400);
for(int i = 0; i <flower.length; i++){
for(int j = 0; j < petal.length; j++){
flower[i] = new Flower();
petal[j] = new Petal();
}
}
}
void draw(){
background(85,135,200);
for(int i = 0; i < flower.length; i++){
flower[i].stem();
}
for(int i = 0; i < petal.length; i++){
petal[i].oscillate();
petal[i].display();
}
}
//----classes ----
class Flower{
//location -stem
float sx;
float sy;
//size- stem
float st;
float slen;
color sc;
Flower(){
//location shape colour
sx = random(40, width -40);
sy = random(100, height- 100);
st = 3;
slen = 60;
sc = color (10, 255, 85);
}
void stem(){
strokeWeight(st);
stroke(sc);
line(sx, sy,sx, sy +slen);
}
}
//---petal class---
class Petal{
float xtheta;
float ytheta;
float dxtheta;
float dytheta;
Petal(){
xtheta = 0;
ytheta = 0;
//increment values
dxtheta = random(-0.03,0.03);
dytheta = random(-0.03,0.03);
}
void oscillate() {
// Increment angles
xtheta += dxtheta;
ytheta += dytheta;
}
void display() {
// Map results of sine / cosine to length of petal
float x = map(sin(xtheta), -1,1, -30,30);
float y = map(cos(ytheta), -1,1, -30,30);
stroke(10,255,85);
strokeWeight(2);
// draw circle and line
//the flower.sx worked until tried to make an array of the flower class.
line(width/2, height/2, flower.sx +x,flower.sy +y); //flower.sx doesn't work!!
fill(200,85,10, 150);
noStroke();
ellipse(flower.sx +x,flower.sy +y,20,20);
}
}//clss
Again, it’s probably some basic syntax I’m not understanding. I’ve had a look at soe tutorials but nothing covers this specific case - that I’ve found. Again calling flower.sx worked until I created an array of the Flower class.
Thanks again for any help!