So I am trying to create a game and the user can has to choose 1 hero from a pool of heroes. How should I create the object of the user's chosen hero? Basically I am trying to create an object according to the user input
Edit: I realize that just putting one paragraph is not enough and that I should add my code structure as well. Currently I have a hero class and every other hero extends from this class, so I am trying to do something like this
class Hero {
}
class Bob extends Hero{ //Bob is one of the heroes that the user can choose
def skill1() { //the skills that Bob can use
}
}
class Player() {
val hero //player's hero option will be stored here
}
class Game { //gamedata will be stored here
val player: Player = new Player()
}
class Controller {
def selectHero { //this is where the user inputs a number from 1 to 10 and the app will create a hero object
}
}
I am stuck at the selectHero
method and do not know how to proceed. I tried doing something like this:
val _hero1: Hero = (
if (option1 == 1) {
new Bob()
}
else if (option1 == 2) {
new James()
}
else if (option1 == 3) {
new Jimmy()
}
else {
null
}
)
But I ended up not being able to access their skills since parent classes cannot access the methods of child classes, can anyone help?