a friend of mine came up with an idea for a racing game, and i'm trying to create that in java. now, i have made 3 classes for cars, 1 for player cars, 1 for computer(ai)cars and a main one that holds some variables like location (x,y on the screen) and name to name a few. the first 2 inherit from the last one. i hoped this would allow me to create one array with both the players and the computer players in it. this however doesnt work, and now my question:
is there any way it is possible to have an array with different kinds of objects in them, and if this is possible, how can i do it or are there any tutorials on it?
i have looked at interfaces, but i dont think that would do the trick, but please correct me if im wrong.
this was the idea that i had:
MainCar[] carsArray = new MainCar[totalPlayers];
for(int i = 0; i < totalHumanPlayers; i++)
{
carsArray[i] = new PlayerCar();
}
for(int i = 0; i < totalComputerPlayers; i++)
{
carsArray[i] = new ComputerCar();
}
the idea with it is that i can loop through all the players(human and computer) to draw them at their locations and to decide whos turn it is next turn
many thanks, and please forgive my english, i don't know if it's correct or not, not my first language :)
PlayerCarandComputerCarboth extendMainCar. What exact problems do you have?