Cannot invoke movingstuff() on the array type anarray.shapes[]

Hey Guys, do you know of a way I can overcome this error? Thanks.
//shapes shapedimentions = new shapes[2];
//squares squaredimentions = new squares[2];
shapes anyshapevariable = new shapes[4];

void setup() {

size(500, 400);
for (int i = 0; i < anyshapevariable.length; i++){
anyshapevariable[i] = new squares(40,0);

}
//shapedimentions[0]= new shapes(33);
//shapedimentions[1] = new shapes(50);
//squaredimentions[0] = new squares(30,50);
//squaredimentions[1] = new squares(30,90);

}
int u = 0;

void draw() {
background(255);
int i;

//if (u>=0&&u<5){
// u++;
//for (i = 0; i < shapedimentions.length; i++) {
// shapedimentions[i].myshapyshenanigans();
// squaredimentions[i].mysquaryshenanigans();
//}}
for ( shapes shape : anyshapevariable ) {
anyshapevariable.movingstuff();
//}
}}

abstract class shapeystuff{
int x;
int y;
}

class shapes extends shapeystuff {
shapes(int x2) {
x = x2;
}

void myshapyshenanigans() {
circle(x, 50, 30);
}
int y;

}

class square extends shapeystuff{

square(int x2, int y2) {
//still not 100% how this works.
y = y2;
x = x2;

}
void mysquaryshenanigans(){
square(x,y,50);
}

}

  • this can’t work, because with anyshapevariable.movingstuff(); you refer to anyshapevariable (an array) but you need to refer to shape which is the for-loop-variable

  • you haven’t defined a function movingstuff(); in the class that’s why he can’t invoke it

  • the for-loop reads: for each shape in anyshapevariable do something by the way

Problem

This line won’t work because anyshapevariable is of type shapes not squares.

Consider anyshapevariable[i] = new shapes(40, 0);

Remarks

  • anyshapevariable is not a good name. It sounds like a variable but is a list of shapes.
  • shapes is not a good name, it suggests plural but it’s only one shape
  • class name are starting with a Capital letter (convention)

Example:


// Demo for classes

//shapes shapedimentions = new shapes[2];
//squares squaredimentions = new squares[2];

shapes[] anyshapevariable = new shapes[4];
int u = 0;

void setup() {
  size(500, 400);

  for (int i = 0; i < anyshapevariable.length; i++) {
    anyshapevariable[i] = new shapes(40, 0);
  }
  //shapedimentions[0]= new shapes(33);
  //shapedimentions[1] = new shapes(50);
  //squaredimentions[0] = new squares(30,50);
  //squaredimentions[1] = new squares(30,90);
}

void draw() {
  background(255);
  int i;

  //if (u>=0&&u<5){
  // u++;
  //for (i = 0; i < shapedimentions.length; i++) {
  // shapedimentions[i].myshapyshenanigans();
  // squaredimentions[i].mysquaryshenanigans();
  //}}
  for ( shapes shape : anyshapevariable ) {
    shape.movingstuff();
  }
}

// ============================================================================

abstract class shapeystuff {
  int x;
  int y;

  void movingstuff() {
    square(x, y, 50);
    x++;
  }
}

class shapes extends shapeystuff {
  shapes(int x2) {
    x = x2;
  }

  shapes(int x2, int y2) { // ????
    x = x2;
  }

  void myshapyshenanigans() {
    circle(x, 50, 30);
  }
  int y;
}

class square extends shapeystuff {

  square(int x2, int y2) {
    //still not 100% how this works.
    y = y2;
    x = x2;
  }
  void mysquaryshenanigans() {
    square(x, y, 50);
  }
}
//

2 Likes