Constructor "undefined"
in
Programming Questions
•
2 years ago
Hi everybody!
I'm trying to use OOP for a game, but I'm not familiar with it. So I googled for an example for constructors and found this:
http://lernprocessing.wordpress.com/2010/01/06/objekte-und-klassen/
I tried to adapt the class part to get an easy constructor for my game, but somehow it doesn't work. When I extract the part I'm interested in from the working program, it works (I deleted the stuff I didn't need):
class Food {
float xPos;
float yPos;
Food (float hxPos, float hyPos) {
xPos = hxPos;
yPos = hyPos;
}
void gibFutter() {
Food [] Futter;
Futter = new Food[50];
for (int f=0; f<Futter.length; f++) {
float xPos = random(10);
float yPos = random(10);
color clr = color(50,200,0,120);
Futter[f] = new Food(xPos,yPos);
}
}
}
I wanted to write the same code with other variabels for my program, but I won't work. It looks like this at the moment:
class Hindernisse {
float hPosx;
float hPosy;
float hHoehe;
color hFarbe;
Hindernisse (float hPosx1, float hPosy1, int hHoehe1, color hFarbe1) {
hPosx = hPosx1;
hPosy = hPosy1;
hHoehe = hHoehe1;
hFarbe = hFarbe1;
}
void makeHindernisse()
{
Hindernisse [] Obstacle;
Obstacle = new Hindernisse[5];
for (int a=0; a<Obstacle.length; a++) {
float hPosx = random(10);
float hPosy = random(10);
float hHoehe = random(2);
color hFarbe = color(255,0,0);
Obstacle[a] = new Hindernisse(hPosx,hPosy,hHoehe,hFarbe);
}
}
}
If you compare the two programs, you will see they are basically the same. That's why I don't understand why it doesn't work. I've been trying to fix this problem for a long time, but I don't know what else I could do to find a solution.
Can someone help me? I would apreciate it very much.
Thanks for your help.
mbbuehler
I'm trying to use OOP for a game, but I'm not familiar with it. So I googled for an example for constructors and found this:
http://lernprocessing.wordpress.com/2010/01/06/objekte-und-klassen/
I tried to adapt the class part to get an easy constructor for my game, but somehow it doesn't work. When I extract the part I'm interested in from the working program, it works (I deleted the stuff I didn't need):
class Food {
float xPos;
float yPos;
Food (float hxPos, float hyPos) {
xPos = hxPos;
yPos = hyPos;
}
void gibFutter() {
Food [] Futter;
Futter = new Food[50];
for (int f=0; f<Futter.length; f++) {
float xPos = random(10);
float yPos = random(10);
color clr = color(50,200,0,120);
Futter[f] = new Food(xPos,yPos);
}
}
}
I wanted to write the same code with other variabels for my program, but I won't work. It looks like this at the moment:
class Hindernisse {
float hPosx;
float hPosy;
float hHoehe;
color hFarbe;
Hindernisse (float hPosx1, float hPosy1, int hHoehe1, color hFarbe1) {
hPosx = hPosx1;
hPosy = hPosy1;
hHoehe = hHoehe1;
hFarbe = hFarbe1;
}
void makeHindernisse()
{
Hindernisse [] Obstacle;
Obstacle = new Hindernisse[5];
for (int a=0; a<Obstacle.length; a++) {
float hPosx = random(10);
float hPosy = random(10);
float hHoehe = random(2);
color hFarbe = color(255,0,0);
Obstacle[a] = new Hindernisse(hPosx,hPosy,hHoehe,hFarbe);
}
}
}
If you compare the two programs, you will see they are basically the same. That's why I don't understand why it doesn't work. I've been trying to fix this problem for a long time, but I don't know what else I could do to find a solution.
Can someone help me? I would apreciate it very much.
Thanks for your help.
mbbuehler
1