Hello, I’m having problems with getting the correct value for screenwidth using width from a class within a class.
I modified the example code that I found on this forum to illustrate the problem.
If i call width from the setup it gives the correct value of 1280, but when i call it from the Class room it always gives 100.
Is this a known bug or am I getting something wrong?
House myHouse = new House();
int myblock;
void setup() {
size (1280, 720);
//*** THIS GIVES CORRECT VALUE FOR WIDTH (1280)***
myblock = width;
myHouse.rooms[0].name="";
myHouse.rooms[0].wifi = true;
myHouse.rooms[0].isDefined = true;
myHouse.rooms[1].name="Bedroom";
myHouse.rooms[1].wifi = false;
myHouse.rooms[1].isDefined = true;
myHouse.rooms[1].colour=color(5, 0, 255);
myHouse.rooms[2].name="Child 1";
myHouse.rooms[2].wifi = false;
myHouse.rooms[2].isDefined = true;
myHouse.rooms[2].windows=5;
myHouse.rooms[2].colour=color(5, 210, 255);
background(9);
}//function
void draw() {
background(9);
myHouse.display();
}//function
// ==========================================
class House {
// default values for a house
color colour=color(0, 255, 0);
int size=310;
int houseNumber=1;
String street="Correct blocksize from setup";
String name="House of family Stone";
// array of rooms in the house
Room[] rooms = new Room [22];
// constructor
House () {
// pre init rooms
for (int i =0; i< rooms.length; i++) {
rooms[i] = new Room();
}
}//constructor
// method
void display() {
// display house
fill(colour);
text(myblock+"\n"+street+" "+houseNumber, 44, 44);
float verticalDistanceRooms = 60;
int i=0;
for (Room currentRoom : rooms) {
if (currentRoom.isDefined) {
currentRoom.display(244, i*verticalDistanceRooms+88 );
stroke(255);
// horizontal line
line (66, i*verticalDistanceRooms+88,
210, i*verticalDistanceRooms+88);
// vertical line
line (66, 0*verticalDistanceRooms+88-20,
66, i*verticalDistanceRooms+88);
i++;
}
}//for
}//method
//
}//class
// =====================================================
class Room {
// default values for a room
color colour=color(255, 0, 0);
int windows=2;
String name="";
boolean wifi=false;
boolean isDefined=false;
//*** THIS GIVES WRONG VALUE FOR WIDTH (always 100)***
int block = width;
// no constructor
// method
void display(float x, float y) {
// display room
// check wifi
String hasWifiText = "Wrong blocksize from Class room"; // default text
if (wifi) {
hasWifiText = "Wrong blocksize from Class room"; // text for room that has wifi
}
// display room using color, name and wifi text etc.
fill(colour);
text(block
+"\n"
+hasWifiText
,
x, y);
}
//
} //class
//