Okay, it’s been a while, but I’m thinking about getting back into programming.
Anyway, This code takes the variable data from it’s own class instead of it’s child classes even when called from the child class. How do i get it so it uses the child’s variable data instead of it’s own when called by the child?
public class TestRPG1 {
static Player hero;
static Enemy dragon;
public static void main(String[] args) {
hero = new Player();
dragon = new Enemy();
while(dragon.hp > 0){
int choice = (int) (Math.random() * 2);
if(choice == 0)
hero.attack(dragon);
else
hero.magic(dragon);
}
System.exit(0);
}
}
public class Combatant {
int hp = 100;
int mp = 100;
int attack = 15;
int magic = 25;
int defence = 15;
int damage = 0;
String name = "null";
public void attack(Combatant target){
damage = (int) (Math.random() * attack);
System.out.println(name + " attacked the " + target.name + " for " + damage + " damage!");
target.hp -= damage;
System.out.println(target.name + " has " + target.hp + " HP left!");
}
public void magic(Combatant target){
damage = (int) (Math.random() * magic);
System.out.println(name + " shot a fireball at " + target.name + " for " + damage + " damage!");
target.hp -= damage;
System.out.println(target.name + " has " + target.hp + " HP left!");
}
}
public class Enemy extends Combatant{
String name = "Dragon";
}
public class Player extends Combatant{
String name = "Hero";
}
… I should have read better