Trying to make and display "room" objects
in
Programming Questions
•
7 months ago
Hi there,
So, I am trying to make a roguelike in Processing. The first thing I am working on is building a map. My end goal is to have rooms that have random locations and dimensions, and (eventually) themes. They would also be lit or dark, have different environmental statuses, etc. For now, they just have random x,y,w,h. In any case, I started out by building off of the array of objects example. Below is my code so far:
Room[] map;
int roomnumber = 10;
float xloc;
float yloc;
float w;
float h;
String roomname;
void setup() {
size(1000, 1000);
map = new Room[10];
for (int i = 0; i < roomnumber; i++) {
xloc = random(700);
yloc = random(700);
w = random(50);
h = random(50);
roomname = "room" + String.valueOf(i);
map[i] = new Room(roomname, xloc, yloc, w, h);
}
}
void draw() {
background(0);
Room.display();
}
class Room {
String name;
float xpos;
float ypos;
float xdim;
float ydim;
Room (String tempname, float tempx, float tempy, float tempxdim, float tempydim) {
name = tempname;
xpos = tempx;
ypos = tempy;
xdim = tempxdim;
ydim = tempydim;
}
void display() {
stroke(255);
fill('.');
rectMode(CENTER);
rect(xpos, ypos, xdim, ydim);
}
}
The idea here was to make an array of room objects, and then display them. I get an error with the Room.display() line in draw(). "Cannot make a static reference to the non-static method display()..." Not sure why I am getting this error.
Also, I wasn't sure if objects could supply their own parameters. In other words, should my code for deciding on the random x,y,w,h be a method inside the class or should it be outside (as I attempted, here). Anyway, advise would be appreciated. Thanks!
So, I am trying to make a roguelike in Processing. The first thing I am working on is building a map. My end goal is to have rooms that have random locations and dimensions, and (eventually) themes. They would also be lit or dark, have different environmental statuses, etc. For now, they just have random x,y,w,h. In any case, I started out by building off of the array of objects example. Below is my code so far:
Room[] map;
int roomnumber = 10;
float xloc;
float yloc;
float w;
float h;
String roomname;
void setup() {
size(1000, 1000);
map = new Room[10];
for (int i = 0; i < roomnumber; i++) {
xloc = random(700);
yloc = random(700);
w = random(50);
h = random(50);
roomname = "room" + String.valueOf(i);
map[i] = new Room(roomname, xloc, yloc, w, h);
}
}
void draw() {
background(0);
Room.display();
}
class Room {
String name;
float xpos;
float ypos;
float xdim;
float ydim;
Room (String tempname, float tempx, float tempy, float tempxdim, float tempydim) {
name = tempname;
xpos = tempx;
ypos = tempy;
xdim = tempxdim;
ydim = tempydim;
}
void display() {
stroke(255);
fill('.');
rectMode(CENTER);
rect(xpos, ypos, xdim, ydim);
}
}
The idea here was to make an array of room objects, and then display them. I get an error with the Room.display() line in draw(). "Cannot make a static reference to the non-static method display()..." Not sure why I am getting this error.
Also, I wasn't sure if objects could supply their own parameters. In other words, should my code for deciding on the random x,y,w,h be a method inside the class or should it be outside (as I attempted, here). Anyway, advise would be appreciated. Thanks!
1