Reputation: 21
Can anyone assist me?
I'm making a game on XNA.
I have a GameObject class that acts as a base class for Character.class
or Object.class
There's also Game.class
I just wanted to ask, I have a subclass of GameObject
which has a public variable called canJump
.
For some reason I can't even access the variable.
I have GameObject character = new Character();
This object can easily access all the overridden functions of the base class but it won't allow me to use public variables.
Upvotes: 0
Views: 1187
Reputation: 11
Polymorphism is the keyword here. Minitech is correct, all the compiler knows is its a GameObject, so you only have access to GameObjects interface. You can use the overidden methods because they are also part of the GameObjects interface, tho at run time you will actually get Character's version of that method. In your code you can do things like:
if(character is Character) //Character being the subclass
{
(character as Character).DoSomeCharacterSpecificStuff;
}
Upvotes: 1
Reputation: 225164
Well, yes, the compiler doesn't know that the GameObject
is actually a Character
. Unless you have a good reason not to, just use Character
:
Character character = new Character();
Upvotes: 1