I created 4 classes (Mage, Rouge, Warrior & dool) that extend the 'Character' class. Each of them has its own combat methods within its classes. However, since only one of these objects is built inside the if / else statement, java does not recognize them as existing. The problem is that I need to call a class method different from the if / else statement. I tried to initialize 4 objects as null first, then construct / overwrite them, but when I call the method, it still refers to them as zero. The formatting is a bit confusing, but here:
EDIT: Thanks for the tip!
public class Rpg {
public static Warrior wplayer = null;
public static Rouge rplayer = null;
public static Mage mplayer = null;
public static Fool fplayer = null;
public static void main(String[] args){
a2 = scan.nextInt();
if (a2 == 1){
Warrior wplayer = new Warrior();
} else if (a2 == 2) {
Rouge rplayer = new Rouge();
} else if (a2 == 3) {
Mage mplayer = new Mage();
} else {
Fool fplayer = new Fool();
while (!notdone){
System.out.println("1: Arena");
System.out.println("2: Blacksmith");
System.out.println("3: Shop");
System.out.println("4: Leave town");
System.out.println("5: Save and Quit");
int choice = scan.nextInt();
if (choice == 1 && wplayer != null){
wplayer.fightEnemy();
} else if (choice == 1 && rplayer != null){
rplayer.fightEnemy();
} else if (choice == 1 && mplayer != null){
mplayer.fightEnemy();
} else {
fplayer.fightEnemy();
}
}
}
}
}
Mike source
share