public class Game {
public Game(
boolean createstage, //For sorting purposes
int slength,
int sheight,
boolean createplayer,
int plength,
int pheight,
boolean playersprite,
BufferedImage psprite,
boolean defaultcontrols,
String pcontrols,
boolean test
) {
if(test == true) { //if test is true, test
new Test();
}else{ //otherwise create a stage is createstage is true and
if(createstage == true) {
StageObj gamestage = new StageObj(slength, sheight);
}
if(createplayer==true) {
PlayerObj player = new PlayerObj(plength, pheight, psprite, pcontrols);
}
}
}
public Game() {
new StageObj(100, 100);
new PlayerObj(10, 10);
}
public StageObj givestageobj() {
return gamestage;
}
public PlayerObj giveplayerobj() {
return player;
}
}
So goes the code of my constructor and two variables designed to return the variables created in the constructor. The problem is that the method giveplayerobj and givestageobj both don’t find the variables gamestage and player. This makes sense, but how do I create the variables in the constructor and then somehow pass them to the giveplayerobj() and the givestageobj() variables so someone could theoretically go Game.giveplayerobj() which returns the playerobj created in the constructor?
Thanks
-JXP
You need to declare them as class attributes and not inside the constructor for that to work. So, you code should look like what is shown below.
Changes:
Added assignment to the overidden default constructor. (Probably what you were trying to achieve with the default constructor)