calling a constructor outside of the class
Ok here I am writing a game... when I got to the constructor which is called to create a character, it doesn't work. It keeps saying symbol not found when I in fact did have the same file name and parameter parsed when I called it... so what's the deal?
public class SCRpg{
public static void main(String [] args){
System.out.println("Please enter a player name between 4 and 10 characters");
String playerName = getPlayerName();
String welcomeMessage = "Welcome to SCRpg " + playerName;
printWelcome(welcomeMessage);
showMenu();
int userChoice = getUserChoice();
if (userChoice == 1){
createChar();
}
if (userChoice == 4){
printCredits();
}
else{
System.exit(0);
}
}
public static String getPlayerName(){
String myString = Keyboard.readInput();
boolean lengthIsOk = checkLength(myString);
do{
if (!lengthIsOk){
System.out.println("The player name you have entered is invalid, please reenter.");
myString = Keyboard.readInput();
}
}
while (lengthIsOk == false);
return myString;
}
public static boolean checkLength(String myString2){
boolean myBoolean = true;
if (myString2.length() > 10 || myString2.length() < 4){
myBoolean = false;
}
return myBoolean;
}
public static void printWelcome(String myString3){
System.out.println(myString3);
}
public static void showMenu(){
System.out.println("1. New Game");
System.out.println("2. Load Game");
System.out.println("3. Options");
System.out.println("4. Credits");
}
public static int getUserChoice(){
int uc = Integer.parseInt(Keyboard.readInput());
boolean choiceIsValid = true;
do{
if (uc == 0 || uc > 4){
choiceIsValid = false;
System.out.println("Your choice is invalid, please try again.");
uc = Integer.parseInt(Keyboard.readInput());
}
}
while (choiceIsValid == false);
return uc;
}
public static void createChar(){
int race = chooseRace();
int level = 1;
int damage = 10;
int armor = 0;
int hp = 100;
int energy = 100;
int plasmaShields = checkRace(race, plasmaShields);
character.playerChar1 = new character(level, damage, hp, energy, plasmaShields, armor);
}
public static int chooseRace(){
System.out.println("Please choose your race: ");
System.out.println("1. Terran/n 2. Protoss/n 3. Zerg");
int race = Integer.parseInt(Keyboard.readInput());
return race;
}
public static int checkRace(int race, int plasmaShields){
if (race == 2){
plasmaShields = 100;
}
else{
plasmaShields = 0;
}
return plasmaShields;
}
public static void printCredits()
{
System.out.println("Made by Adam Zhang");
}
}
and here's the constructor code in another class called character
public class character{
private int level;
private int damage;
private int hp;
private int energy;
private int plasmaShields;
private int armor;
public character(int level, int damage, int hp, int energy, int plasmaShields, int armor){
this.level = level;
this.hp = hp;
this.damage = damage;
this.energy = energy;
this.plasmaShields = plasmaShields;
this.armor = armor;
}
}
|